SpriteLighting.cginc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifndef SPRITE_LIGHTING_INCLUDED
  2. #define SPRITE_LIGHTING_INCLUDED
  3. //Check for using mesh normals
  4. #if !defined(_FIXED_NORMALS_VIEWSPACE) && !defined(_FIXED_NORMALS_VIEWSPACE_BACKFACE) && !defined(_FIXED_NORMALS_MODELSPACE) && !defined(_FIXED_NORMALS_MODELSPACE_BACKFACE) && !defined(_FIXED_NORMALS_WORLDSPACE)
  5. #define MESH_NORMALS
  6. #endif
  7. //Check for fixing backfacing tangents
  8. #if defined(_FIXED_NORMALS_VIEWSPACE_BACKFACE) || defined(_FIXED_NORMALS_MODELSPACE_BACKFACE)
  9. #define FIXED_NORMALS_BACKFACE_RENDERING
  10. #endif
  11. ////////////////////////////////////////
  12. // Vertex structs
  13. //
  14. struct VertexInput
  15. {
  16. float4 vertex : POSITION;
  17. float4 texcoord : TEXCOORD0;
  18. float4 color : COLOR;
  19. #if defined(MESH_NORMALS)
  20. float3 normal : NORMAL;
  21. #endif // _FIXED_NORMALS
  22. #if defined(_NORMALMAP)
  23. float4 tangent : TANGENT;
  24. #endif // _NORMALMAP
  25. UNITY_VERTEX_INPUT_INSTANCE_ID
  26. };
  27. ////////////////////////////////////////
  28. // Normal functions
  29. //
  30. #if !defined(USE_LWRP) && !defined(USE_URP)
  31. uniform float4 _FixedNormal = float4(0, 0, 1, 1);
  32. #endif
  33. inline float3 getFixedNormal()
  34. {
  35. return _FixedNormal.xyz;
  36. }
  37. inline float calculateBackfacingSign(float3 worldPos)
  38. {
  39. //If we're using fixed normals and mesh is facing away from camera, flip tangentSign
  40. //Unity uses a left handed coordinate system so camera always looks down the negative z axis
  41. float3 cameraForward = float3(0,0,-1);
  42. float3 meshWorldForward = mul((float3x3)unity_ObjectToWorld, cameraForward);
  43. float3 toCamera = _WorldSpaceCameraPos - worldPos;
  44. return sign(dot(toCamera, meshWorldForward));
  45. }
  46. inline half3 calculateSpriteWorldNormal(VertexInput vertex, float backFaceSign)
  47. {
  48. #if defined(MESH_NORMALS)
  49. return calculateWorldNormal(vertex.normal);
  50. #else // !MESH_NORMALS
  51. float3 normal = getFixedNormal();
  52. #if defined(_FIXED_NORMALS_VIEWSPACE) || defined(_FIXED_NORMALS_VIEWSPACE_BACKFACE)
  53. //View space fixed normal
  54. //Rotate fixed normal by inverse view matrix to convert the fixed normal into world space
  55. float3x3 invView = transpose((float3x3)UNITY_MATRIX_V);
  56. return normalize(mul(invView, normal));
  57. #elif defined (_FIXED_NORMALS_WORLDSPACE)
  58. //World space fixed normal
  59. return normal;
  60. #else
  61. //Model space fixed normal.
  62. #if defined(FIXED_NORMALS_BACKFACE_RENDERING)
  63. //If back face rendering is enabled and the sprite is facing away from the camera (ie we're rendering the backface) then need to flip the normal
  64. normal *= backFaceSign;
  65. #endif
  66. return calculateWorldNormal(normal);
  67. #endif
  68. #endif // !MESH_NORMALS
  69. }
  70. inline half3 calculateSpriteViewNormal(VertexInput vertex, float backFaceSign)
  71. {
  72. #if defined(MESH_NORMALS)
  73. return normalize(mul((float3x3)UNITY_MATRIX_IT_MV, vertex.normal));
  74. #else // !MESH_NORMALS
  75. float3 normal = getFixedNormal();
  76. #if defined(_FIXED_NORMALS_VIEWSPACE) || defined(_FIXED_NORMALS_VIEWSPACE_BACKFACE)
  77. //View space fixed normal
  78. return normal;
  79. #elif defined (_FIXED_NORMALS_WORLDSPACE)
  80. //World space fixed normal
  81. return normalize(mul((float3x3)UNITY_MATRIX_V, normal));
  82. #else
  83. //Model space fixed normal
  84. #if defined(FIXED_NORMALS_BACKFACE_RENDERING)
  85. //If back face rendering is enabled and the sprite is facing away from the camera (ie we're rendering the backface) then need to flip the normal
  86. normal *= backFaceSign;
  87. #endif
  88. return normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));
  89. #endif
  90. #endif // !MESH_NORMALS
  91. }
  92. ////////////////////////////////////////
  93. // Normal map functions
  94. //
  95. #if defined(_NORMALMAP)
  96. inline half3 calculateSpriteWorldBinormal(VertexInput vertex, half3 normalWorld, half3 tangentWorld, float backFaceSign)
  97. {
  98. float tangentSign = vertex.tangent.w;
  99. #if defined(FIXED_NORMALS_BACKFACE_RENDERING)
  100. tangentSign *= backFaceSign;
  101. #endif
  102. return calculateWorldBinormal(normalWorld, tangentWorld, tangentSign);
  103. }
  104. #endif // _NORMALMAP
  105. #if defined(_DIFFUSE_RAMP)
  106. ////////////////////////////////////////
  107. // Diffuse ramp functions
  108. //
  109. uniform sampler2D _DiffuseRamp;
  110. inline fixed3 calculateDiffuseRamp(float ramp)
  111. {
  112. return tex2D(_DiffuseRamp, float2(ramp, ramp)).rgb;
  113. }
  114. inline fixed3 calculateRampedDiffuse(fixed3 lightColor, float attenuation, float angleDot)
  115. {
  116. #if defined(_FULLRANGE_HARD_RAMP)
  117. float d = angleDot;
  118. half3 ramp = calculateDiffuseRamp(d);
  119. return lightColor * ramp * attenuation;
  120. #elif defined(_FULLRANGE_SOFT_RAMP)
  121. float d = angleDot;
  122. half3 ramp = calculateDiffuseRamp(d * attenuation);
  123. return lightColor * ramp;
  124. #elif defined(_OLD_SOFT_RAMP)
  125. // for unmodified behaviour with existing projects when
  126. // the HARD_DIFFUSE_RAMP define was disabled in this file.
  127. // uses only the right half of the ramp texture, as
  128. // negative angleDot is clamped to [0,1] before.
  129. float d = angleDot * 0.5 + 0.5;
  130. half3 ramp = calculateDiffuseRamp(d);
  131. return lightColor * ramp * (attenuation * 2);
  132. #else // _OLD_HARD_RAMP
  133. // old default, for unmodified behaviour with existing projects,
  134. // uses only the right half of the ramp texture, as
  135. // negative angleDot is clamped to [0,1] before.
  136. float d = angleDot * 0.5 + 0.5;
  137. half3 ramp = calculateDiffuseRamp(d * attenuation * 2);
  138. return lightColor * ramp;
  139. #endif
  140. }
  141. #endif // _DIFFUSE_RAMP
  142. ////////////////////////////////////////
  143. // Rim Lighting functions
  144. //
  145. #ifdef _RIM_LIGHTING
  146. #if !defined(USE_LWRP) && !defined(USE_URP)
  147. uniform float _RimPower;
  148. uniform fixed4 _RimColor;
  149. #endif
  150. inline fixed3 applyRimLighting(fixed3 posWorld, fixed3 normalWorld, fixed4 pixel) : SV_Target
  151. {
  152. fixed3 viewDir = normalize(_WorldSpaceCameraPos - posWorld);
  153. float invDot = 1.0 - saturate(dot(normalWorld, viewDir));
  154. float rimPower = pow(invDot, _RimPower);
  155. float rim = saturate(rimPower * _RimColor.a);
  156. #if defined(_DIFFUSE_RAMP)
  157. rim = calculateDiffuseRamp(rim).r;
  158. #endif
  159. return lerp(pixel.rgb, _RimColor.xyz * pixel.a, rim);
  160. }
  161. #endif //_RIM_LIGHTING
  162. ////////////////////////////////////////
  163. // Emission functions
  164. //
  165. #ifdef _EMISSION
  166. uniform sampler2D _EmissionMap;
  167. #if !defined(USE_LWRP) && !defined(USE_URP)
  168. uniform fixed4 _EmissionColor;
  169. uniform float _EmissionPower;
  170. #endif
  171. #define APPLY_EMISSION(diffuse, uv) diffuse += tex2D(_EmissionMap, uv).rgb * _EmissionColor.rgb * _EmissionPower;
  172. #define APPLY_EMISSION_SPECULAR(pixel, uv) pixel.rgb += (tex2D(_EmissionMap, uv).rgb * _EmissionColor.rgb * _EmissionPower) * pixel.a;
  173. #else //!_EMISSION
  174. #define APPLY_EMISSION(diffuse, uv)
  175. #define APPLY_EMISSION_SPECULAR(pixel, uv)
  176. #endif //!_EMISSION
  177. #endif // SPRITE_LIGHTING_INCLUDED