# 正文

_CompressLengthInY ("Compress Length",Range(0,0.5)) = 0

v2f vert(appdata v)
{
v2f o;
//根据抛物线公式(x + 0.5)^2 =2py而来，其中x为顶点y坐标，y为需要压缩的最大长度
v.vertex.y -= pow(v.vertex.y + 0.5,2) * _CompressLengthInY;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}

Pass
{
Tags
{
"LightMode" = "ForwardBase"//使用前向渲染路径
}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase//不能少

#include "UnityCg.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

sampler2D _MainTex;
fixed4 _Specular;
float _Gloss;
float _CompressLengthInY;

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};

struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float4 worldPos: TEXCOORD2;
};

v2f vert(appdata v)
{
v2f o;
//根据抛物线公式(x + 0.5)^2 =2py而来，其中x为顶点y坐标，y为需要压缩的最大长度
v.vertex.y -= pow(v.vertex.y + 0.5,2) * _CompressLengthInY;

o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}

fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv);
fixed4 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT;

float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
fixed4 diff = albedo * _LightColor0 * max(0, dot(i.worldNormal, worldLight));

float3 halfDir = normalize(worldView + worldLight);
fixed4 spec = albedo * _Specular * pow(max(0, dot(halfDir, i.worldNormal)), _Gloss);

fixed4 col = ambient + (diff + spec) * shadow;
return col;
}

ENDCG
}

Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCg.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

float _CompressLengthInY;

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;

};

v2f vert(appdata v)
{
v2f o;
//根据抛物线公式(x + 0.5)^2 =2py而来，其中x为顶点y坐标，y为需要压缩的最大长度
v.vertex.y -= pow(v.vertex.y + 0.5,2) * _CompressLengthInY;

o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}

fixed4 frag(v2f i) : SV_TARGET
{
return fixed4(0.5,0.5,0.5,1);
}

ENDCG

}

public class Test : MonoBehaviour {

private float touchStartTime;
private float touchTime;
private float targetCompressLength;
public float maxCompressLength = 0.5f;

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
touchStartTime = Time.time;
}
if (Input.GetMouseButton(0))
{
touchTime = Time.time - touchStartTime;
targetCompressLength = touchTime / 10;
targetCompressLength = Mathf.Clamp(targetCompressLength, 0, maxCompressLength);
GetComponent<MeshRenderer>().material.SetFloat("_CompressLengthInY", targetCompressLength);
}
if (Input.GetMouseButtonUp(0))
{
GetComponent<MeshRenderer>().material.DOFloat(0, "_CompressLengthInY", 1f).SetEase(Ease.OutElastic, 0.7f);
}
}
}