Lighting Model函数

作者:追风剑情 发布于:2014-7-28 22:38 分类:Shader

Shader "Custom/LightingMyForwardLit" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		//.1
		Pass {
			Tags {"LightMode"="ForwardBase"}
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"
			#include "Lighting.cginc"
			struct vertOut {
				float4 pos:SV_POSITION;
				float4 color:COLOR;
			};
			vertOut vert(appdata_base v)
			{
				vertOut o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.color = float4(0, 1, 0, 1);//输出绿色
				return o;
			}
			float4 frag(vertOut i):COLOR
			{
				return i.color;
			}
			ENDCG
		}
		//.2
		Blend One One
		CGPROGRAM
		#pragma surface surf MyForwardLit
		
		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};
		//为Forward模式提供Lighting Model函数
		half4 LightingMyForwardLit (SurfaceOutput s, half3 lightDir, half atten)
		{
			//以下为漫反射公式
			//NdotL值越大受环境光影响越大
			half NdotL = dot (s.Normal, lightDir);
			half4 c;
			c.rgb = s.Albedo * _LightColor0 * (NdotL * atten *2);
			c.a = s.Alpha;
			return c;
		}

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

运行效果(设置像机渲染路径为Deferred Lighting)

Lighting.png

上图输出了绿色说明Unity在Deferred渲染路径下没找到适合Deferred模式的Pass,从而执行了适合Forward模式的Pass

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号