支持雾效的双面渲染

作者:追风剑情 发布于:2019-11-26 15:04 分类:Shader

示例

//功能:环境光遮蔽(AO)、Alpha测试、双面渲染、雾效
Shader "Custom/AlphaTestBothSided"
{
	Properties{
		_Color("Main Tint", Color) = (1, 1, 1, 1)
		_MainTex("Albedo", 2D) = "white" {}
		[NoScaleOffset]
		_OcclusionMap("Occlusion", 2D) = "white" {}
		_IntensityMultiplier("Intensity Multiplier", Range(0, 2)) = 1
		_Cutoff("Alpha Cutoff", Range(0, 1)) = 0.5
	}
	SubShader{
		//Unity5中Queue设置成AlphaTest
		Tags { "Queue" = "AlphaTest" "IgnoreProjector" = "true" "RenderType" = "TransparentCutout" }
		LOD 200
		Pass {
			Tags {"LightMode" = "ForwardBase"}

			//关闭剔除功能
			Cull Off

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			#pragma multi_compile_fwdbase
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			fixed4 _Color;
			sampler2D _MainTex;
			float4 _MainTex_ST;
			sampler2D _OcclusionMap;
			fixed _Cutoff;
			fixed _IntensityMultiplier;

			struct a2v {
				float4 vertex : POSITION;
				float3 normal : NORMAL;
				//Unity会将模型的第一组纹理坐标存储到该变量中
				float4 texcoord : TEXCOORD0;
			};

			struct v2f {
				float4 pos : SV_POSITION;
				float3 worldNormal : TEXCOORD0;
				float3 worldPos : TEXCOORD1;
				float2 uv : TEXCOORD2;
				UNITY_FOG_COORDS(3)
			};

			v2f vert(a2v v) {
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject);
				//Unity5中可用UnityObjectToWorldNormal()函数得到o.worldNormal
				//o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
				o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
				//或者调用Unity内建的函数计算o.uv
				//o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				UNITY_TRANSFER_FOG(o, o.pos);
				return o;
			}

			fixed4 frag(v2f i) : COLOR {
				fixed3 worldNormal = normalize(i.worldNormal);
				//fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
				fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);//只适合场景中仅有一个平行光
				fixed4 texColor = tex2D(_MainTex, i.uv);
				clip(texColor.a - _Cutoff);
				//clip函数相当于下面代码的功能
				//if((texColor.a - _Cutoff) < 0.0){
					//discard;
				//}
				half occlusion = tex2D(_OcclusionMap, i.uv).g;
				fixed3 albedo = texColor.rgb * _Color.rgb;
				//fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
				//考虑环境光遮蔽(Ambient Occlusion)
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * occlusion * _IntensityMultiplier * albedo;
				//fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
				//让背面支持光照
				fixed3 diffuse = _LightColor0.rgb * albedo * max(0, abs(dot(worldNormal, worldLightDir)));
				fixed4 col = fixed4(ambient + diffuse, 1.0);//gi.indirect.diffuse
				//自定义雾的颜色
				//UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(1, 0, 0, 0));
				//使用Lighting面板中设置的雾颜色
				UNITY_APPLY_FOG(i.fogCoord, col);
				//return col;
				return col;
			}
			ENDCG
		}
	}
	//确保我们编写的SubShader无法在当前显卡上工作时可以有合适的代替Shader,
	//还可以保证使用透明度测试的物体可以正确地向其他物体投射阴影
	FallBack "Transparent/Cutout/VertexLit"
}

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号