Advertisement
Guest User

Lighting.cginc Unity shader 4.6.7

a guest
Oct 4th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef LIGHTING_INCLUDED
  2. #define LIGHTING_INCLUDED
  3.  
  4. struct SurfaceOutput {
  5.     fixed3 Albedo;
  6.     fixed3 Normal;
  7.     fixed3 Emission;
  8.     half Specular;
  9.     fixed Gloss;
  10.     fixed Alpha;
  11. };
  12.  
  13. #ifndef USING_DIRECTIONAL_LIGHT
  14. #if defined (DIRECTIONAL_COOKIE) || defined (DIRECTIONAL)
  15. #define USING_DIRECTIONAL_LIGHT
  16. #endif
  17. #endif
  18.  
  19.  
  20. // NOTE: you would think using half is fine here, but that would make
  21. // Cg apply some precision emulation, making the constants in the shader
  22. // much different; and do some other stupidity that actually increases ALU
  23. // count on d3d9 at least. So we use float.
  24. //
  25. // Also, the numbers in many components should be the same, but are changed
  26. // very slightly in the last digit, to prevent Cg from mis-optimizing
  27. // the shader (it tried to be super clever at saving one shader constant
  28. // at expense of gazillion extra scalar moves). Saves about 6 ALU instructions
  29. // on d3d9 SM2.
  30. //
  31. // Metal is very picky about precision, and this triggers float3x3->half3x3 casts later on (which has perf implications)
  32. // NB: it is fixed already in 5.0
  33. #if defined(SHADER_API_METAL)
  34.     #define UNITY_DIRBASIS \
  35.     const half3x3 unity_DirBasis = half3x3( \
  36.       half3( 0.81649658,  0.0,        0.57735028), \
  37.       half3(-0.40824830,  0.70710679, 0.57735027), \
  38.       half3(-0.40824829, -0.70710678, 0.57735026) \
  39.     );
  40. #else
  41.     #define UNITY_DIRBASIS \
  42.     const float3x3 unity_DirBasis = float3x3( \
  43.       float3( 0.81649658,  0.0,        0.57735028), \
  44.       float3(-0.40824830,  0.70710679, 0.57735027), \
  45.       float3(-0.40824829, -0.70710678, 0.57735026) \
  46.     );
  47. #endif
  48.  
  49.  
  50. inline half3 DirLightmapDiffuse(in half3x3 dirBasis, fixed4 color, fixed4 scale, half3 normal, bool surfFuncWritesNormal, out half3 scalePerBasisVector)
  51. {
  52.     half3 lm = DecodeLightmap (color);
  53.    
  54.     // will be compiled out (and so will the texture sample providing the value)
  55.     // if it's not used in the lighting function, like in LightingLambert
  56.     scalePerBasisVector = DecodeLightmap (scale);
  57.  
  58.     // will be compiled out when surface function does not write into o.Normal
  59.     if (surfFuncWritesNormal)
  60.     {
  61.         half3 normalInRnmBasis = saturate (mul (dirBasis, normal));
  62.         lm *= dot (normalInRnmBasis, scalePerBasisVector);
  63.     }
  64.  
  65.     return lm;
  66. }
  67.  
  68.  
  69. fixed4 _LightColor0;
  70. fixed4 _SpecColor;
  71.  
  72. inline fixed4 LightingLambert (SurfaceOutput s, fixed3 lightDir, fixed atten)
  73. {
  74.     fixed diff = max (0, dot (s.Normal, lightDir));
  75.    
  76.     fixed4 c;
  77.     c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
  78.     c.a = s.Alpha;
  79.     return c;
  80. }
  81.  
  82.  
  83. inline fixed4 LightingLambert_PrePass (SurfaceOutput s, half4 light)
  84. {
  85.     fixed4 c;
  86.     c.rgb = s.Albedo * light.rgb;
  87.     c.a = s.Alpha;
  88.     return c;
  89. }
  90.  
  91. inline half4 LightingLambert_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, bool surfFuncWritesNormal)
  92. {
  93.     UNITY_DIRBASIS
  94.     half3 scalePerBasisVector;
  95.    
  96.     half3 lm = DirLightmapDiffuse (unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
  97.    
  98.     return half4(lm, 0);
  99. }
  100.  
  101.  
  102. // NOTE: some intricacy in shader compiler on some GLES2.0 platforms (iOS) needs 'viewDir' & 'h'
  103. // to be mediump instead of lowp, otherwise specular highlight becomes too bright.
  104. inline fixed4 LightingBlinnPhong (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
  105. {
  106.     half3 h = normalize (lightDir + viewDir);
  107.    
  108.     fixed diff = max (0, dot (s.Normal, lightDir));
  109.    
  110.     float nh = max (0, dot (s.Normal, h));
  111.     float spec = pow (nh, s.Specular*128.0) * s.Gloss;
  112.    
  113.     fixed4 c;
  114.     c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
  115.     c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
  116.     return c;
  117. }
  118.  
  119. inline fixed4 LightingBlinnPhong_PrePass (SurfaceOutput s, half4 light)
  120. {
  121.     fixed spec = light.a * s.Gloss;
  122.    
  123.     fixed4 c;
  124.     c.rgb = (s.Albedo * light.rgb + light.rgb * _SpecColor.rgb * spec);
  125.     c.a = s.Alpha + spec * _SpecColor.a;
  126.     return c;
  127. }
  128.  
  129. inline half4 LightingBlinnPhong_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
  130. {
  131.     UNITY_DIRBASIS
  132.     half3 scalePerBasisVector;
  133.    
  134.     half3 lm = DirLightmapDiffuse (unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
  135.    
  136.     half3 lightDir = normalize (scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
  137.     half3 h = normalize (lightDir + viewDir);
  138.  
  139.     float nh = max (0, dot (s.Normal, h));
  140.     float spec = pow (nh, s.Specular * 128.0);
  141.    
  142.     // specColor used outside in the forward path, compiled out in prepass
  143.     specColor = lm * _SpecColor.rgb * s.Gloss * spec;
  144.    
  145.     // spec from the alpha component is used to calculate specular
  146.     // in the Lighting*_Prepass function, it's not used in forward
  147.     return half4(lm, spec);
  148. }
  149.  
  150.  
  151.  
  152. #ifdef UNITY_CAN_COMPILE_TESSELLATION
  153. struct UnityTessellationFactors {
  154.     float edge[3] : SV_TessFactor;
  155.     float inside : SV_InsideTessFactor;
  156. };
  157. #endif // UNITY_CAN_COMPILE_TESSELLATION
  158.  
  159. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement