Shader-(顶点实现)环境光与自定义颜色

  • UNITY_LIGHTMODEL_AMBIENT    用来获取环境光
     
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'

Shader "LXC/04pp Diffuse"
{
	Properties{
		_Diffuse("Diffuse Color",Color) = (1,1,1,1)	
	}
		SubShader
	{
		Pass{
		Tags{ "LightMode" = "ForwardBase" }
		CGPROGRAM
#include "Lighting.cginc"
#pragma vertex vert		
#pragma fragment frag
		fixed4 _Diffuse;	

		struct a2v {
		float4 vertex:POSITION;
		float3 normal:NORMAL;
	};

	struct v2f {
		float4 position:SV_POSITION;
		fixed3 color : COLOR;
	};

	v2f vert(a2v v) {
		v2f f;
		f.position = UnityObjectToClipPos(v.vertex);
		//环境光
		fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
		fixed3 normalDir = normalize(mul(v.normal, (float3x3) unity_WorldToObject));	
		fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);					
		fixed3 diffuse = _LightColor0.rgb * max(dot(normalDir,lightDir),0)*_Diffuse.rgb;	
		f.color = diffuse + ambient;	//光是综合作用所以叠加,叠加+会越来越亮,融合*
		return f;
	}


	fixed4 frag(v2f f) :SV_Target{
		return fixed4(f.color,1);
	}
		ENDCG
	}
	}
		Fallback "VertexLit"
}

你可能感兴趣的:(Shader,学习记录,Unity)