Unity3d《Shader篇》漫反射
Unity3d《Shader篇》漫反射
Shader "Custom/Ambient" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Color("Diffuse Material Color", Color) = (1,1,1,1) _SpecColor("Specular Color", Color) = (1,1,1,1) _Shininess("Shininess", Float) = 10 } SubShader { Pass { Tags{ "LightMode" = "ForwardBase" } CGPROGRAM #pragma exclude_renderers gles #pragma vertex vert #pragma fragment frag sampler2D _MainTex; uniform float4 _Color; uniform float4 _SpecColor; uniform float _Shininess; uniform float4 _LightColor0; struct vertexInput { float2 texCoord : TEXCOORD0; float4 vertex : POSITION; float3 normal : NORMAL; }; struct vertexOutput { float4 pos : SV_POSITION; float4 color : COLOR; float2 texCoord : TEXCOORD0; }; vertexOutput vert( vertexInput input ) { vertexOutput output; float4x4 modelMatrix = _Object2World; float3 normalDir = normalize(float3(mul(_Object2World, float4(input.normal,0.0)))); float3 lightDir = normalize(float3(_WorldSpaceLightPos0)); float3 viewDir = normalize(_WorldSpaceCameraPos - float3(mul(_Object2World,input.vertex))); float specularRefection = float3(_LightColor0)*float3(_SpecColor)*pow(max(0.0,dot(reflect(-lightDir,normalDir),viewDir)),_Shininess); float3 diffuseReflection = float3(_LightColor0)*float3(_Color)*max(0.0,dot(normalDir,lightDir)); float3 ambientLighting = float3(UNITY_LIGHTMODEL_AMBIENT)*float3(_Color); output.color = float4(ambientLighting+diffuseReflection+specularRefection,1.0); output.pos = mul(UNITY_MATRIX_MVP,input.vertex); output.texCoord = input.texCoord; return output; } float4 frag(vertexOutput input) : COLOR { float4 col = tex2D(_MainTex,input.texCoord)*_Color; return input.color*col; } ENDCG } } FallBack "Diffuse" }