SpritePixelLighting.cginc 8.3 KB

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