SpriteLighting.cginc 6.3 KB

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