Unity ShaderLab 实现交互地毯
实现思路:
将一个位置坐标值传入到shader的顶点着色器中,和这个值位置相同的顶点沿着法线的y轴方向偏移,然后计算这个值与顶点的距离,在这个范围内的顶点,和凸起的点的位置做插值操作。
Shader Graph实现如下:
Shader Lab实现如下:
Shader "Custom/CustomAlfombraInteractiva"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Position("Position", Vector) = (0, 0, 0, 0)
_radius("radius", Float) = 0
_edge("edge", Float) = 0
_blur("blur", Float) = 0
_intensity("intensity", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Position;
float _radius;
float _blur;
float _edge;
float _intensity;
v2f vert (appdata v)
{
v2f o;
float4 ver = mul(unity_ObjectToWorld, v.vertex);
float range = distance(_Position,ver) - _radius;
float4 color = smoothstep(_edge,_blur,range);
float lerpVale = lerp(v.normal.y * color * _intensity,ver.y,color);
o.vertex = UnityObjectToClipPos(float4(v.vertex.x,lerpVale,v.vertex.z,v.vertex.w));
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
实现效果如下:
参考链接:
Alfombra interactiva Tutorial | deformación de vértices | Unity Shader Graph (youtube.com)