Shader "R1/Scene/Scene_DiffuseBump_Clip" { Properties { [NoScaleOffset]_MainTex ("主帖图", 2D) = "white" {} [NoScaleOffset]_FunctionMap("法线贴图", 2D) = "black" {} _Clip("裁剪范围",Range(0 , 1)) = 0.5 _Emmision("自发光强度", Range(0, 5)) = 0 [HDR]_EmmisionColor("自发光颜色",Color) = (1,1,1,0) [Space(40)] [Header(Use vertex color control animation)] [Space] [Toggle(_WIND_ON)]_windOn("开启顶点动画",float) = 0 _Frequency("摇晃频率", Range(0 , 1)) = 0.2 _Strength("摇晃幅度", Range(0 , 1)) = 0.2 _Scale("摇晃范围",Range(0 , 5)) = 1 [Toggle] _ZWrite("写入深度", Float) = 1 } SubShader { Tags {"RenderType" = "TransparentCutout" "Queue" = "AlphaTest"} CGINCLUDE #pragma multi_compile __ _WIND_ON uniform sampler2D _MainTex; uniform float _Clip; uniform fixed4 _EmmisionColor; ENDCG Pass { Tags {"LightMode" = "ForwardBase"} Cull Off ZWrite [_ZWrite] CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 3.0 #pragma skip_variants LIGHTPROBE_SH LIGHTMAP_ON DYNAMICLIGHTMAP_ON DIRLIGHTMAP_COMBINED LIGHTMAP_SHADOW_MIXING SHADOWS_SHADOWMASK VERTEXLIGHT_ON #pragma multi_compile_instancing #pragma multi_compile_fwdbase #pragma multi_compile_fog #pragma multi_compile __ _USEPOINTLIGHT_ON #include "UnityCG.cginc" #include "AutoLight.cginc" #include "Lighting.cginc" #include "CloudInclude.cginc" #include "DiffuseInclude.cginc" #include "SideInclude.cginc" #ifdef _USEPOINTLIGHT_ON #include "PointlightInclude.cginc" #endif #ifdef _WIND_ON #include "NoiseInclude.cginc" #endif uniform sampler2D _FunctionMap; uniform float _Emmision; VertexOutput vert (VertexInput v) { VertexOutput o = (VertexOutput)0; UNITY_SETUP_INSTANCE_ID(v); //UNITY_TRANSFER_INSTANCE_ID(v, o); o.uv.xy = v.uv; o.normalDir.xyz = normalize(UnityObjectToWorldNormal(v.normal)); o.tangentDir = normalize(mul(unity_ObjectToWorld, v.tangent.xyz).xyz); o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w); o.posWorld = mul(unity_ObjectToWorld, v.vertex); //风 #ifdef _WIND_ON v.vertex.xyz += FinalPerlin(v.vertex,v.color,_Time.y);//风,有include #endif o.pos = UnityObjectToClipPos(v.vertex); o.uv.zw = 0;//没有用到的分量 UNITY_TRANSFER_FOG(o,o.pos); TRANSFER_VERTEX_TO_FRAGMENT(o) return o; } float4 frag(VertexOutput i) : COLOR{ //UNITY_SETUP_INSTANCE_ID(i); fixed2 _uv = i.uv.xy;//UV fixed4 _mainTex = tex2D(_MainTex, _uv);//采样主帖图 fixed4 _normalTex = tex2D(_FunctionMap, _uv);//采样法线贴图 fixed3 _normalMap = UnpackNormal(_normalTex);//解包法线 float3x3 _tangentTransform = float3x3(i.tangentDir, i.bitangentDir, i.normalDir.xyz);//切线空间到世界空间矩阵 _normalMap = normalize(mul(_normalMap, _tangentTransform));//法线转到世界空间 _normalMap = _normalTex.x != 0 ? _normalMap : i.normalDir;//如果没有法线贴图,则使用模型自身法线 fixed3 _finalRGB = Diffuse(_normalMap,_mainTex,i);//光照,有include _finalRGB *= _mainTex.rgb;//乘以主帖图 fixed3 _emission = _mainTex.rgb * _Emmision * _normalTex.a;//自发光,基于主帖图颜色 _finalRGB += _emission;//输出颜色加上自发光 _finalRGB = AddCloud(i.posWorld,_uv,_finalRGB);//云投影,有include _finalRGB *= _EmmisionColor; //点光源,有include #ifdef _USEPOINTLIGHT_ON _finalRGB += PointLight(_normalMap,i.posWorld); #endif UNITY_APPLY_FOG(i.fogCoord, _finalRGB);//雾 //_finalRGB = AddSide(i.posWorld.xz, _finalRGB);//暗边 clip(_mainTex.a - _Clip);//裁剪 return fixed4(_finalRGB,1); } ENDCG } //shadow Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 2.0 #pragma multi_compile_shadowcaster #pragma multi_compile_instancing #include "UnityCG.cginc" #ifdef _WIND_ON #include "NoiseInclude.cginc" #endif struct VertexInput { float4 vertex : POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct VertexOutput { V2F_SHADOW_CASTER; float2 uv : TEXCOORD1; //UNITY_VERTEX_INPUT_INSTANCE_ID }; VertexOutput vert(VertexInput v) { VertexOutput o = (VertexOutput)0; UNITY_SETUP_INSTANCE_ID(v); //UNITY_TRANSFER_INSTANCE_ID(v, o); o.uv = v.uv.xy; //风 #ifdef _WIND_ON v.vertex.xyz += FinalPerlin(v.vertex,v.color,_Time.y);//风,有include #endif TRANSFER_SHADOW_CASTER(o) return o; } float4 frag(VertexOutput i) : SV_Target { //UNITY_SETUP_INSTANCE_ID(i); float4 _mainTex = tex2D(_MainTex, i.uv); clip(_mainTex.a - _Clip); SHADOW_CASTER_FRAGMENT(i) } ENDCG } } }