创建CgInclude文件来存储光照模型

作者:追风剑情 发布于:2016-3-5 19:52 分类:Shader

一、创建MyCgInclude.cginc文件

#ifndef MY_CG_INCLUDE
#define MY_CG_INCLUDE

fixed4 _MyColor;

//半兰伯特光照模型
inline fixed4 LightingHalfLambert (SurfaceOutput s, fixed3 lightDir, fixed atten)
{
	fixed diff = max (0, dot(s.Normal, lightDir));
	diff = (diff + 0.5)*0.5;
	fixed4 c;
	c.rgb = s.Albedo*_LightColor0.rgb*((diff*_MyColor.rgb)*atten*2);
	c.a = s.Alpha;
	return c;
}

#endif

二、使用MyCgInclude.cginc文件

Shader "Custom/HalfLambertShader" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_DesatValue ("Desaturate", Range(0, 1)) = 0.5
		_MyColor ("My Color", Color) = (1,1,1,1)
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#include "MyCgInclude.cginc"
		#pragma surface surf HalfLambert

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
		};

		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"
}

效果

111111.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号