SpritePixelLighting.cginc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #ifndef SPRITE_PIXEL_LIGHTING_INCLUDED
  2. #define SPRITE_PIXEL_LIGHTING_INCLUDED
  3. #include "ShaderShared.cginc"
  4. #include "SpriteLighting.cginc"
  5. #include "SpriteSpecular.cginc"
  6. #include "AutoLight.cginc"
  7. ////////////////////////////////////////
  8. // Defines
  9. //
  10. ////////////////////////////////////////
  11. // Vertex output struct
  12. //
  13. #if defined(_NORMALMAP)
  14. #define _VERTEX_LIGHTING_INDEX TEXCOORD5
  15. #define _LIGHT_COORD_INDEX_0 6
  16. #define _LIGHT_COORD_INDEX_1 7
  17. #define _FOG_COORD_INDEX 8
  18. #else
  19. #define _VERTEX_LIGHTING_INDEX TEXCOORD3
  20. #define _LIGHT_COORD_INDEX_0 4
  21. #define _LIGHT_COORD_INDEX_1 5
  22. #define _FOG_COORD_INDEX 6
  23. #endif // _NORMALMAP
  24. struct VertexOutput
  25. {
  26. float4 pos : SV_POSITION;
  27. fixed4 color : COLOR;
  28. float2 texcoord : TEXCOORD0;
  29. float4 posWorld : TEXCOORD1;
  30. half3 normalWorld : TEXCOORD2;
  31. #if defined(_NORMALMAP)
  32. half3 tangentWorld : TEXCOORD3;
  33. half3 binormalWorld : TEXCOORD4;
  34. #endif // _NORMALMAP
  35. fixed3 vertexLighting : _VERTEX_LIGHTING_INDEX;
  36. LIGHTING_COORDS(_LIGHT_COORD_INDEX_0, _LIGHT_COORD_INDEX_1)
  37. #if defined(_FOG)
  38. UNITY_FOG_COORDS(_FOG_COORD_INDEX)
  39. #endif // _FOG
  40. UNITY_VERTEX_OUTPUT_STEREO
  41. };
  42. ////////////////////////////////////////
  43. // Light calculations
  44. //
  45. uniform fixed4 _LightColor0;
  46. inline fixed3 calculateLightDiffuse(VertexOutput input, float3 normalWorld, inout fixed4 albedo)
  47. {
  48. //For directional lights _WorldSpaceLightPos0.w is set to zero
  49. float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
  50. float attenuation = LIGHT_ATTENUATION(input);
  51. float angleDot = max(0, dot(normalWorld, lightWorldDirection));
  52. #if defined(_DIFFUSE_RAMP)
  53. fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, attenuation, angleDot);
  54. #else
  55. fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot);
  56. #endif // _DIFFUSE_RAMP
  57. return lightDiffuse;
  58. }
  59. inline float3 calculateNormalWorld(VertexOutput input)
  60. {
  61. #if defined(_NORMALMAP)
  62. return calculateNormalFromBumpMap(input.texcoord, input.tangentWorld, input.binormalWorld, input.normalWorld);
  63. #else
  64. return input.normalWorld;
  65. #endif
  66. }
  67. fixed3 calculateVertexLighting(float3 posWorld, float3 normalWorld)
  68. {
  69. fixed3 vertexLighting = fixed3(0,0,0);
  70. #ifdef VERTEXLIGHT_ON
  71. //Get approximated illumination from non-important point lights
  72. vertexLighting = Shade4PointLights ( unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
  73. unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
  74. unity_4LightAtten0, posWorld, normalWorld) * 0.5;
  75. #endif
  76. return vertexLighting;
  77. }
  78. fixed3 calculateAmbientLight(half3 normalWorld)
  79. {
  80. #if defined(_SPHERICAL_HARMONICS)
  81. fixed3 ambient = ShadeSH9(half4(normalWorld, 1.0));
  82. #else
  83. fixed3 ambient = unity_AmbientSky.rgb;
  84. #endif
  85. return ambient;
  86. }
  87. #if defined(SPECULAR)
  88. fixed4 calculateSpecularLight(SpecularCommonData s, float3 viewDir, float3 normal, float3 lightDir, float3 lightColor, half3 ambient)
  89. {
  90. SpecularLightData data = calculatePhysicsBasedSpecularLight (s.specColor, s.oneMinusReflectivity, s.smoothness, normal, viewDir, lightDir, lightColor, ambient, unity_IndirectSpecColor.rgb);
  91. fixed4 pixel = calculateLitPixel(fixed4(s.diffColor, s.alpha), data.lighting);
  92. pixel.rgb += data.specular * s.alpha;
  93. return pixel;
  94. }
  95. fixed4 calculateSpecularLightAdditive(SpecularCommonData s, float3 viewDir, float3 normal, float3 lightDir, float3 lightColor)
  96. {
  97. SpecularLightData data = calculatePhysicsBasedSpecularLight (s.specColor, s.oneMinusReflectivity, s.smoothness, normal, viewDir, lightDir, lightColor, half3(0,0,0), half3(0,0,0));
  98. fixed4 pixel = calculateAdditiveLitPixel(fixed4(s.diffColor, s.alpha), data.lighting);
  99. pixel.rgb += data.specular * s.alpha;
  100. return pixel;
  101. }
  102. #endif //SPECULAR
  103. ////////////////////////////////////////
  104. // Vertex program
  105. //
  106. VertexOutput vert(VertexInput v)
  107. {
  108. VertexOutput output;
  109. UNITY_SETUP_INSTANCE_ID(input);
  110. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  111. output.pos = calculateLocalPos(v.vertex);
  112. output.color = calculateVertexColor(v.color);
  113. output.texcoord = calculateTextureCoord(v.texcoord);
  114. output.posWorld = calculateWorldPos(v.vertex);
  115. float backFaceSign = 1;
  116. #if defined(FIXED_NORMALS_BACKFACE_RENDERING)
  117. backFaceSign = calculateBackfacingSign(output.posWorld.xyz);
  118. #endif
  119. output.normalWorld = calculateSpriteWorldNormal(v, backFaceSign);
  120. output.vertexLighting = calculateVertexLighting(output.posWorld, output.normalWorld);
  121. #if defined(_NORMALMAP)
  122. output.tangentWorld = calculateWorldTangent(v.tangent);
  123. output.binormalWorld = calculateSpriteWorldBinormal(v, output.normalWorld, output.tangentWorld, backFaceSign);
  124. #endif
  125. TRANSFER_VERTEX_TO_FRAGMENT(output)
  126. #if defined(_FOG)
  127. UNITY_TRANSFER_FOG(output,output.pos);
  128. #endif // _FOG
  129. return output;
  130. }
  131. ////////////////////////////////////////
  132. // Fragment programs
  133. //
  134. fixed4 fragBase(VertexOutput input) : SV_Target
  135. {
  136. fixed4 texureColor = calculateTexturePixel(input.texcoord);
  137. RETURN_UNLIT_IF_ADDITIVE_SLOT(texureColor, input.color) // shall be called before ALPHA_CLIP
  138. ALPHA_CLIP(texureColor, input.color)
  139. //Get normal direction
  140. fixed3 normalWorld = calculateNormalWorld(input);
  141. //Get Ambient diffuse
  142. fixed3 ambient = calculateAmbientLight(normalWorld);
  143. #if defined(SPECULAR)
  144. //For directional lights _WorldSpaceLightPos0.w is set to zero
  145. float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
  146. float attenuation = LIGHT_ATTENUATION(input);
  147. //Returns pixel lit by light, texture color should inlcluded alpha
  148. half3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
  149. fixed4 pixel = calculateSpecularLight(getSpecularData(input.texcoord.xy, texureColor, input.color), viewDir, normalWorld, lightWorldDirection, _LightColor0.rgb * attenuation, ambient + input.vertexLighting);
  150. APPLY_EMISSION_SPECULAR(pixel, input.texcoord)
  151. #else
  152. //Get primary pixel light diffuse
  153. fixed3 diffuse = calculateLightDiffuse(input, normalWorld, texureColor);
  154. //Combine along with vertex lighting for the base lighting pass
  155. fixed3 lighting = ambient + diffuse + input.vertexLighting;
  156. APPLY_EMISSION(lighting, input.texcoord)
  157. fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting);
  158. #endif
  159. #if defined(_RIM_LIGHTING)
  160. pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel);
  161. #endif
  162. COLORISE(pixel)
  163. APPLY_FOG(pixel, input)
  164. return pixel;
  165. }
  166. fixed4 fragAdd(VertexOutput input) : SV_Target
  167. {
  168. fixed4 texureColor = calculateTexturePixel(input.texcoord);
  169. #if defined(_COLOR_ADJUST)
  170. texureColor = adjustColor(texureColor);
  171. #endif // _COLOR_ADJUST
  172. ALPHA_CLIP(texureColor, input.color)
  173. //Get normal direction
  174. fixed3 normalWorld = calculateNormalWorld(input);
  175. #if defined(SPECULAR)
  176. //For directional lights _WorldSpaceLightPos0.w is set to zero
  177. float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
  178. float attenuation = LIGHT_ATTENUATION(input);
  179. half3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
  180. fixed4 pixel = calculateSpecularLightAdditive(getSpecularData(input.texcoord.xy, texureColor, input.color), viewDir, normalWorld, lightWorldDirection, _LightColor0.rgb * attenuation);
  181. #else
  182. //Get light diffuse
  183. fixed3 lighting = calculateLightDiffuse(input, normalWorld, texureColor);
  184. fixed4 pixel = calculateAdditiveLitPixel(texureColor, input.color, lighting);
  185. #endif
  186. COLORISE_ADDITIVE(pixel)
  187. APPLY_FOG_ADDITIVE(pixel, input)
  188. return pixel;
  189. }
  190. #endif // SPRITE_PIXEL_LIGHTING_INCLUDED