Unity-Shader

认识shader:

更改原始生成的shader代码,更改为:

Shader "CookbookShaders/BasicDiffuse" {
	Properties {		
		_EmissiveColor ("Emissive Color",Color) = (1,1,1,1)
		_AmbientColor ("Ambient Color",Color) = (1,1,1,1)
		_MySliderValue ("This is a Slider",Range(0,10)) = 2.5
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM		
		#pragma surface surf Lambert

		float4 _EmissiveColor;
		float4 _AmbientColor;
		float _MySliderValue;
		

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			float4 c;
			c = pow( (_EmissiveColor + _AmbientColor), _MySliderValue);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}



创建自定义光照模型,创建各种效果:

Shader "CookbookShaders/BasicDiffuse" {
	Properties {		
		_EmissiveColor ("Emissive Color",Color) = (1,1,1,1)
		_AmbientColor ("Ambient Color",Color) = (1,1,1,1)
		_MySliderValue ("This is a Slider",Range(0,10)) = 2.5
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM		
		#pragma surface surf BasicDiffuse

		float4 _EmissiveColor;
		float4 _AmbientColor;
		float _MySliderValue;
		
		inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
		lightDir, fixed atten)
		{
			float difLight = max(0, dot (s.Normal , lightDir));
			
			float4 col;
			col.rgb = s.Albedo * _LightColor0.rgb * (difLight * atten * 2);
			col.a = s.Alpha;
			return col;
		}
		

		struct Input {
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			float4 c;
			c = pow( (_EmissiveColor + _AmbientColor), _MySliderValue);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

创建一个Half Lambert光照模型
将上述例子的inline float4 LightingBasicDiffuse 
更改为:

		inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
		lightDir, fixed atten)
		{
			//float difLight = max(0, dot (s.Normal , lightDir));
			float difLight = dot(s.Normal,lightDir);
			float hLambert = difLight * 0.5 + 0.5;
			
			float4 col;
			col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
			col.a = s.Alpha;
			return col;
		}




创建渐变纹理控制漫反射着色
1、在Propertites中,增加

	_RampTex("MyTexture",2D) = "white"{}

2、在CGPROGRAM 中增加_RampTex变量

        sampler2D _RampTex;

3、将纹理texture类型图片与_RampTex变量相关联;
4、更改光照函数LightingBasicDiffuse为:
		inline float4 LightingBasicDiffuse (SurfaceOutput s,fixed3
		lightDir, fixed atten)
		{
			float difLight = max(0, dot (s.Normal , lightDir));
			
			float hLambert = difLight * 0.5 + 0.5;
			float3 ramp = tex2D(_RampTex,float2(hLambert)).rgb;
			
			float4 col;
			col.rgb = s.Albedo * _LightColor0.rgb * (ramp);
			//col.rgb = s.Albedo * _LightColor0.rgb * (hLambert * atten * 2);
			col.a = s.Alpha;
			return col;
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值