本系列主要参考《Unity Shaders and Effects Cookbook》一书(感谢原书作者),同时会加上一点个人理解或拓展。
这里是本书所有的插图。这里是本书所需的代码和资源(当然你也可以从官网下载)。
========================================== 分割线 ==========================================
Properties { _MainTint ("Diffuse Tint", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _NormalMap ("Normal Map", 2D) = "bump" {} }
CGPROGRAM #pragma surface surf Unlit vertex:vert
float4 _MainTint; sampler2D _MainTex; sampler2D _NormalMap;
inline half4 LightingUnlit (SurfaceOutput s, fixed3 lightDir, fixed atten) { half4 c = half4(1,1,1,1); c.rgb = s.Albedo; c.a = s.Alpha; return c; }
struct Input { float2 uv_MainTex; float2 uv_NormalMap; float3 tan1; float3 tan2; };
void vert (inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); TANGENT_SPACE_ROTATION; o.tan1 = mul(rotation, UNITY_MATRIX_IT_MV[0].xyz); o.tan2 = mul(rotation, UNITY_MATRIX_IT_MV[1].xyz); }
void surf (Input IN, inout SurfaceOutput o) { float3 normals = UnpackNormal(tex2D(_NormalMap, IN.uv_NormalMap)); o.Normal = normals; float2 litSphereUV; litSphereUV.x = dot(IN.tan1, o.Normal); litSphereUV.y = dot(IN.tan2, o.Normal); half4 c = tex2D (_MainTex, litSphereUV*0.5+0.5); o.Albedo = c.rgb * _MainTint; o.Alpha = c.a; }
Shader "Custom/LitSphere" { Properties { _MainTint ("Diffuse Tint", Color) = (1,1,1,1) _MainTex ("Base (RGB)", 2D) = "white" {} _NormalMap ("Normal Map", 2D) = "bump" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Unlit vertex:vert float4 _MainTint; sampler2D _MainTex; sampler2D _NormalMap; inline half4 LightingUnlit (SurfaceOutput s, fixed3 lightDir, fixed atten) { half4 c = half4(1,1,1,1); c.rgb = s.Albedo; c.a = s.Alpha; return c; } struct Input { float2 uv_MainTex; float2 uv_NormalMap; float3 tan1; float3 tan2; }; void vert (inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); TANGENT_SPACE_ROTATION; o.tan1 = mul(rotation, UNITY_MATRIX_IT_MV[0].xyz); o.tan2 = mul(rotation, UNITY_MATRIX_IT_MV[1].xyz); } void surf (Input IN, inout SurfaceOutput o) { float3 normals = UnpackNormal(tex2D(_NormalMap, IN.uv_NormalMap)); o.Normal = normals; float2 litSphereUV; litSphereUV.x = dot(IN.tan1, o.Normal); litSphereUV.y = dot(IN.tan2, o.Normal); half4 c = tex2D (_MainTex, litSphereUV*0.5+0.5); o.Albedo = c.rgb * _MainTint; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
【Unity Shaders】Lighting Models —— 光照模型之Lit Sphere,布布扣,bubuko.com
【Unity Shaders】Lighting Models —— 光照模型之Lit Sphere
原文:http://blog.csdn.net/candycat1992/article/details/26387471