自用代碼2
Shader "Custom/myShader10" //shader名稱 擺放位置 { ? //在這里添加自定義的變量,即shader的外部輸入變量 Properties { //ProPerties中的變量都可以在腳本代碼中動(dòng)態(tài)訪問及修改 //_變量名(“Inspector中的提示名字”,類型) = “默認(rèn)值”{} _MainColor("Main Color",Color) = (1.0,1.0,1.0,1.0) _MainTex("Base (RGB)",2D) = "White"{} _Bump("Bump",2D) = "bump"{} _Snow("Level of snow",Range(-1,1)) = 0 _SnowColor("Color of snow",Color) = (1.0,1.0,1.0,1.0) _SnowDirection("Direction of snow",Vector) = (0,1,0) _SnowDepth("Depth of snow",Range(0,0.1)) = 0? ? } SubShader { Tags { "RenderType"="Opaque" }?//說明性標(biāo)簽 LOD 200 ? CGPROGRAM //#pragma surface surf Lambert vertex:vert #pragma surface surf Standard vertex:vert ? sampler2D _MainTex; sampler2D _Bump; float _Snow; float4 _SnowColor; float4 _MainColor; float4 _SnowDirection; float _SnowDepth; ? struct Input{ ?float2 uv_MainTex; ?float2 uv_Bump; ?float3 worldNormal; ?INTERNAL_DATA }; ? ??//定點(diǎn)函數(shù)(Vertex) void vert(inout appdata_full v) { ???float4 sn = mul(transpose(unity_ObjectToWorld),_SnowDirection);//轉(zhuǎn)化成物理坐標(biāo) ???if (dot(v.normal, sn.xyz) >= _Snow) { ???//if (dot(v.normal, sn.xyz) >= lerp(1, -1, (_Snow * 2) / 3)) { ??v.vertex.xyz += (sn.xyz + v.normal) * _SnowDepth * abs(_Snow); ???} ????} ? //面元函數(shù)(Fragment) //面元函數(shù)的輸出,一定是fixed4(定常浮點(diǎn)數(shù))表示本物體在屏幕上的顯示顏色 void surf(Input IN, inout SurfaceOutputStandard o){ ?half4 c = tex2D(_MainTex,IN.uv_MainTex); ?o.Normal = UnpackNormal(tex2D(_Bump,IN.uv_Bump)); ?//轉(zhuǎn)化為世界坐標(biāo)系,實(shí)現(xiàn)向上的面,有積雪效果 ?if (dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) >= _Snow) { ?//if (dot(WorldNormalVector(IN, o.Normal), _SnowDirection.xyz) >= lerp(1, -1, _Snow)) { ??o.Albedo = _SnowColor.rgb; ???} ???else { ??o.Albedo = c.rgb * _MainColor; ???} ???//shader將向上的面處理為白色 ???//o.Albedo = 1; } ENDCG } }