修仙笔记Shader篇之着色器变体以及实现UV流动Shader
本帖最后由 SmilingFace 于 2023-4-18 20:24 编辑```
Shader "multi_comple"
{
Properties
{
_MainTex ("MainTex", 2D) = "white" {}
_Noise ("Noise", 2D) = "white" {}
_Color ("Color",Color) = (1.0,1.0,1.0,1.0)
//控制渐变区域以及硬度
_GradientTill ("GradientTill",float) = 1
_GradientOffset("GradientOffset",float) = 0
_timeSpeed("timeSpeed",float) = 1
//变体开关
_FLOWEnable("FLOW Enable",int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//变体,请注意_FLOWENABLE,要与属性设置中的名称 一致,并且需要全大写
#pragma multi_compile _ _FLOWENABLE_ON
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldPos:TEXCOORD1;
float3 relativePos:TEXCOORD2;
};
sampler2D _MainTex;
sampler2D _Noise;
float4 _Noise_ST;
float4 _Color;
float _GradientTill;
float _GradientOffset;
float _timeSpeed;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy =v.texcoord;
o.uv.zw = v.texcoord* _Noise_ST.zw+_Noise_ST.xy;
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
o.relativePos = mul(unity_ObjectToWorld,(v.vertex-float3(0.0,0.0,0.0)));
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float2 uv1 = i.uv.xy;
float2 uv2 = i.uv.zw;
//通过开关变体,实现uv流动
#if _FLOWENABLE_ON
uv2 = i.uv.zw-float2(0.5,_Time.y)*_timeSpeed;
//此处最好加一个Fract函数钳制uv的大小,否则运行时间长了,可能会出现uv数值大于Float允许的精度范围,导致出问题。
#endif
float3 worldPos = i.worldPos;
float3 relativePos = i.relativePos;
half baseColor = tex2D(_MainTex, uv1);
half noise = tex2D(_Noise,uv2);
half gradient = relativePos.y*_GradientTill+_GradientOffset;
gradient = saturate(1-gradient);
half3 finalColor = _Color*noise*gradient+baseColor;
return half4(finalColor,1.0);
}
ENDCG
}
}
}
```
那个relativePos不是很理解
页:
[1]