1.原理
通过模型坐标判断,离中心越近越高,否则越低。
2.shader代码
Shader "Custom/Terrian"
{
properties{
_Radius("Radius",range(0,5)) = 1.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed4 vcol : COLOR;
};
float _Radius;
v2f vert (appdata v)
{
v2f o;
float dis = length(v.vertex.xz);
dis = _Radius - dis;
dis = dis < 0 ? 0 : dis;
float upY = clamp(dis * 0.5,0,5);
float4 uppos = float4(v.vertex.x,v.vertex.y + upY,v.vertex.z,v.vertex.w);
o.vertex = UnityObjectToClipPos(uppos);
o.uv = v.uv;
o.vcol = fixed4(0,1,0,1) + fixed4(uppos.y,uppos.y,uppos.y,0);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.vcol;
}
ENDCG
}
}
}
3.效果
4.设置中心
Shader "Custom/Terrian"
{
properties{
_Radius("Radius",range(0,5)) = 1.0
_Center("Center",Vector) = (0,0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed4 vcol : COLOR;
};
float _Radius;
float4 _Center;
v2f vert (appdata v)
{
v2f o;
float dis = length(v.vertex.xz - _Center.xy);
dis = _Radius - dis;
dis = dis < 0 ? 0 : dis;
float upY = clamp(dis * 0.5,0,5);
float4 uppos = float4(v.vertex.x,v.vertex.y + upY,v.vertex.z,v.vertex.w);
o.vertex = UnityObjectToClipPos(uppos);
o.uv = v.uv;
o.vcol = fixed4(0,1,0,1) + fixed4(uppos.y,uppos.y,uppos.y,0);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.vcol;
}
ENDCG
}
}
}
效果: