ShaderShared.cginc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Upgrade NOTE: upgraded instancing buffer 'PerDrawSprite' to new syntax.
  2. #ifndef SHADER_SHARED_INCLUDED
  3. #define SHADER_SHARED_INCLUDED
  4. #if defined(USE_LWRP)
  5. #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
  6. #include "Packages/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/SpineCoreShaders/Spine-Common.cginc"
  7. #elif defined(USE_URP)
  8. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  9. #include "Packages/com.esotericsoftware.spine.urp-shaders/Shaders/Include/SpineCoreShaders/Spine-Common.cginc"
  10. #else
  11. #include "UnityCG.cginc"
  12. #include "../../CGIncludes/Spine-Common.cginc"
  13. #endif
  14. ////////////////////////////////////////
  15. // Space functions
  16. //
  17. inline float4 calculateWorldPos(float4 vertex)
  18. {
  19. return mul(unity_ObjectToWorld, vertex);
  20. }
  21. #if defined(USE_LWRP) || defined(USE_URP)
  22. // snaps post-transformed position to screen pixels
  23. inline float4 UnityPixelSnap(float4 pos)
  24. {
  25. float2 hpc = _ScreenParams.xy * 0.5f;
  26. #if SHADER_API_PSSL
  27. // sdk 4.5 splits round into v_floor_f32(x+0.5) ... sdk 5.0 uses v_rndne_f32, for compatabilty we use the 4.5 version
  28. float2 temp = ((pos.xy / pos.w) * hpc) + float2(0.5f, 0.5f);
  29. float2 pixelPos = float2(__v_floor_f32(temp.x), __v_floor_f32(temp.y));
  30. #else
  31. float2 pixelPos = round((pos.xy / pos.w) * hpc);
  32. #endif
  33. pos.xy = pixelPos / hpc * pos.w;
  34. return pos;
  35. }
  36. #endif
  37. inline float4 calculateLocalPos(float4 vertex)
  38. {
  39. #if !defined(USE_LWRP) && !defined(USE_URP)
  40. #ifdef UNITY_INSTANCING_ENABLED
  41. vertex.xy *= _Flip.xy;
  42. #endif
  43. #endif
  44. #if defined(USE_LWRP) || defined(USE_URP)
  45. float4 pos = TransformObjectToHClip(vertex.xyz);
  46. #else
  47. float4 pos = UnityObjectToClipPos(vertex);
  48. #endif
  49. #ifdef PIXELSNAP_ON
  50. pos = UnityPixelSnap(pos);
  51. #endif
  52. return pos;
  53. }
  54. inline half3 calculateWorldNormal(float3 normal)
  55. {
  56. #if defined(USE_LWRP) || defined(USE_URP)
  57. return TransformObjectToWorldNormal(normal);
  58. #else
  59. return UnityObjectToWorldNormal(normal);
  60. #endif
  61. }
  62. ////////////////////////////////////////
  63. // Normal map functions
  64. //
  65. #if defined(_NORMALMAP)
  66. uniform sampler2D _BumpMap;
  67. #if !defined(USE_LWRP) && !defined(USE_URP)
  68. uniform half _BumpScale;
  69. #endif
  70. half3 UnpackScaleNormal(half4 packednormal, half bumpScale)
  71. {
  72. #if defined(UNITY_NO_DXT5nm)
  73. return packednormal.xyz * 2 - 1;
  74. #else
  75. half3 normal;
  76. normal.xy = (packednormal.wy * 2 - 1);
  77. // Note: we allow scaled normals in LWRP since we might be using fewer instructions.
  78. #if (SHADER_TARGET >= 30) || defined(USE_LWRP) || defined(USE_URP)
  79. // SM2.0: instruction count limitation
  80. // SM2.0: normal scaler is not supported
  81. normal.xy *= bumpScale;
  82. #endif
  83. normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
  84. return normal;
  85. #endif
  86. }
  87. inline half3 calculateWorldTangent(float4 tangent)
  88. {
  89. #if defined(USE_LWRP) || defined(USE_URP)
  90. return TransformObjectToWorldDir(tangent.xyz);
  91. #else
  92. return UnityObjectToWorldDir(tangent);
  93. #endif
  94. }
  95. inline half3 calculateWorldBinormal(half3 normalWorld, half3 tangentWorld, float tangentSign)
  96. {
  97. //When calculating the binormal we have to flip it when the mesh is scaled negatively.
  98. //Normally this would just be unity_WorldTransformParams.w but this isn't set correctly by Unity for its SpriteRenderer meshes so get from objectToWorld matrix scale instead.
  99. half worldTransformSign = sign(unity_ObjectToWorld[0][0] * unity_ObjectToWorld[1][1] * unity_ObjectToWorld[2][2]);
  100. half sign = tangentSign * worldTransformSign;
  101. return cross(normalWorld, tangentWorld) * sign;
  102. }
  103. inline half3 calculateNormalFromBumpMap(float2 texUV, half3 tangentWorld, half3 binormalWorld, half3 normalWorld)
  104. {
  105. half3 localNormal = UnpackScaleNormal(tex2D(_BumpMap, texUV), _BumpScale);
  106. half3x3 rotation = half3x3(tangentWorld, binormalWorld, normalWorld);
  107. half3 normal = normalize(mul(localNormal, rotation));
  108. return normal;
  109. }
  110. #endif // _NORMALMAP
  111. ////////////////////////////////////////
  112. // Blending functions
  113. //
  114. inline fixed4 prepareLitPixelForOutput(fixed4 finalPixel, fixed textureAlpha, fixed colorAlpha) : SV_Target
  115. {
  116. #if defined(_ALPHABLEND_ON)
  117. //Normal Alpha
  118. finalPixel.rgb *= finalPixel.a;
  119. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)
  120. //PMA vertex, straight texture
  121. finalPixel.rgb *= textureAlpha;
  122. #elif defined(_ALPHAPREMULTIPLY_ON)
  123. //Pre multiplied alpha, both vertex and texture
  124. // texture and vertex colors are premultiplied already
  125. #elif defined(_MULTIPLYBLEND)
  126. //Multiply
  127. finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a);
  128. #elif defined(_MULTIPLYBLEND_X2)
  129. //Multiply x2
  130. finalPixel.rgb *= 2.0f;
  131. finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a);
  132. #elif defined(_ADDITIVEBLEND)
  133. //Additive
  134. finalPixel *= 2.0f;
  135. finalPixel.rgb *= colorAlpha;
  136. #elif defined(_ADDITIVEBLEND_SOFT)
  137. //Additive soft
  138. finalPixel.rgb *= finalPixel.a;
  139. #else
  140. //Opaque
  141. finalPixel.a = 1;
  142. #endif
  143. return finalPixel;
  144. }
  145. inline fixed4 calculateLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target
  146. {
  147. fixed4 finalPixel = texureColor * color * fixed4(lighting, 1);
  148. finalPixel = prepareLitPixelForOutput(finalPixel, texureColor.a, color.a);
  149. return finalPixel;
  150. }
  151. inline fixed4 calculateLitPixel(fixed4 texureColor, fixed3 lighting) : SV_Target
  152. {
  153. // note: we let the optimizer work, removed duplicate code.
  154. return calculateLitPixel(texureColor, fixed4(1, 1, 1, 1), lighting);
  155. }
  156. inline fixed4 calculateAdditiveLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target
  157. {
  158. fixed4 finalPixel;
  159. #if defined(_ALPHABLEND_ON) || defined(_MULTIPLYBLEND) || defined(_MULTIPLYBLEND_X2) || defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  160. //Normal Alpha, Additive and Multiply modes
  161. finalPixel.rgb = (texureColor.rgb * lighting * color.rgb) * (texureColor.a * color.a);
  162. finalPixel.a = 1.0;
  163. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)
  164. //PMA vertex, straight texture
  165. finalPixel.rgb = texureColor.rgb * lighting * color.rgb * texureColor.a;
  166. finalPixel.a = 1.0;
  167. #elif defined(_ALPHAPREMULTIPLY_ON)
  168. //Pre multiplied alpha, both vertex and texture
  169. finalPixel.rgb = texureColor.rgb * lighting * color.rgb;
  170. finalPixel.a = 1.0;
  171. #else
  172. //Opaque
  173. finalPixel.rgb = texureColor.rgb * lighting * color.rgb;
  174. finalPixel.a = 1.0;
  175. #endif
  176. return finalPixel;
  177. }
  178. inline fixed4 calculateAdditiveLitPixel(fixed4 texureColor, fixed3 lighting) : SV_Target
  179. {
  180. fixed4 finalPixel;
  181. #if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) || defined(_MULTIPLYBLEND) || defined(_MULTIPLYBLEND_X2) || defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  182. //Normal Alpha, Additive and Multiply modes
  183. finalPixel.rgb = (texureColor.rgb * lighting) * texureColor.a;
  184. finalPixel.a = 1.0;
  185. #else
  186. //Pre multiplied alpha and Opaque
  187. finalPixel.rgb = texureColor.rgb * lighting;
  188. finalPixel.a = 1.0;
  189. #endif
  190. return finalPixel;
  191. }
  192. inline fixed4 calculatePixel(fixed4 texureColor, fixed4 color) : SV_Target
  193. {
  194. // note: we let the optimizer work, removed duplicate code.
  195. return calculateLitPixel(texureColor, color, fixed3(1, 1, 1));
  196. }
  197. inline fixed4 calculatePixel(fixed4 texureColor) : SV_Target
  198. {
  199. // note: we let the optimizer work, removed duplicate code.
  200. return calculateLitPixel(texureColor, fixed4(1, 1, 1, 1), fixed3(1, 1, 1));
  201. }
  202. ////////////////////////////////////////
  203. // Alpha Clipping
  204. //
  205. #if defined(_ALPHA_CLIP)
  206. #if !defined(USE_LWRP) && !defined(USE_URP)
  207. uniform fixed _Cutoff;
  208. #endif
  209. #define ALPHA_CLIP(pixel, color) clip((pixel.a * color.a) - _Cutoff);
  210. #else
  211. #define ALPHA_CLIP(pixel, color)
  212. #endif
  213. ////////////////////////////////////////
  214. // Additive Slot blend mode
  215. // return unlit textureColor, alpha clip textureColor.a only
  216. //
  217. #if defined(_ALPHAPREMULTIPLY_ON) && !defined(_LIGHT_AFFECTS_ADDITIVE)
  218. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor) \
  219. if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
  220. ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
  221. return texureColor * vertexColor;\
  222. }
  223. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) && !defined(_LIGHT_AFFECTS_ADDITIVE)
  224. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor) \
  225. if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
  226. ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
  227. return texureColor * texureColor.a * vertexColor;\
  228. }
  229. #else
  230. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor)
  231. #endif
  232. ////////////////////////////////////////
  233. // Color functions
  234. //
  235. #if !defined(USE_LWRP) && !defined(USE_URP)
  236. uniform fixed4 _Color;
  237. #endif
  238. inline fixed4 calculateVertexColor(fixed4 color)
  239. {
  240. #if defined(_ALPHAPREMULTIPLY_ON) || _ALPHAPREMULTIPLY_VERTEX_ONLY
  241. return PMAGammaToTargetSpace(color) * _Color;
  242. #elif !defined (UNITY_COLORSPACE_GAMMA)
  243. return fixed4(GammaToLinearSpace(color.rgb), color.a) * _Color;
  244. #else
  245. return color * _Color;
  246. #endif
  247. }
  248. #if defined(_COLOR_ADJUST)
  249. #if !defined(USE_LWRP) && !defined(USE_URP)
  250. uniform float _Hue;
  251. uniform float _Saturation;
  252. uniform float _Brightness;
  253. uniform fixed4 _OverlayColor;
  254. #endif
  255. float3 rgb2hsv(float3 c)
  256. {
  257. float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  258. float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
  259. float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
  260. float d = q.x - min(q.w, q.y);
  261. float e = 1.0e-10;
  262. return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  263. }
  264. float3 hsv2rgb(float3 c)
  265. {
  266. c = float3(c.x, clamp(c.yz, 0.0, 1.0));
  267. float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  268. float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
  269. return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  270. }
  271. inline fixed4 adjustColor(fixed4 color)
  272. {
  273. float3 hsv = rgb2hsv(color.rgb);
  274. hsv.x += _Hue;
  275. hsv.y *= _Saturation;
  276. hsv.z *= _Brightness;
  277. color.rgb = hsv2rgb(hsv);
  278. return color;
  279. }
  280. #define COLORISE(pixel) pixel.rgb = lerp(pixel.rgb, _OverlayColor.rgb, _OverlayColor.a * pixel.a);
  281. #define COLORISE_ADDITIVE(pixel) pixel.rgb = ((1.0-_OverlayColor.a) * pixel.rgb);
  282. #else // !_COLOR_ADJUST
  283. #define COLORISE(pixel)
  284. #define COLORISE_ADDITIVE(pixel)
  285. #endif // !_COLOR_ADJUST
  286. ////////////////////////////////////////
  287. // Fog
  288. //
  289. #if defined(_FOG) && (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  290. inline fixed4 applyFog(fixed4 pixel, float fogCoordOrFactorAtLWRP)
  291. {
  292. #if defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  293. //In additive mode blend from clear to black based on luminance
  294. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  295. fixed4 fogColor = lerp(fixed4(0,0,0,0), fixed4(0,0,0,1), luminance);
  296. #elif defined(_MULTIPLYBLEND)
  297. //In multiplied mode fade to white based on inverse luminance
  298. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  299. fixed4 fogColor = lerp(fixed4(1,1,1,1), fixed4(0,0,0,0), luminance);
  300. #elif defined(_MULTIPLYBLEND_X2)
  301. //In multipliedx2 mode fade to grey based on inverse luminance
  302. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  303. fixed4 fogColor = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), fixed4(0,0,0,0), luminance);
  304. #elif defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) || defined(_ALPHAPREMULTIPLY_ON)
  305. //In alpha blended modes blend to fog color based on pixel alpha
  306. fixed4 fogColor = lerp(fixed4(0,0,0,0), unity_FogColor, pixel.a);
  307. #else
  308. //In opaque mode just return fog color;
  309. fixed4 fogColor = unity_FogColor;
  310. #endif
  311. #if defined(USE_LWRP) || defined(USE_URP)
  312. pixel.rgb = MixFogColor(pixel.rgb, fogColor.rgb, fogCoordOrFactorAtLWRP);
  313. #else
  314. UNITY_APPLY_FOG_COLOR(fogCoordOrFactorAtLWRP, pixel, fogColor);
  315. #endif
  316. return pixel;
  317. }
  318. #define APPLY_FOG(pixel, input) pixel = applyFog(pixel, input.fogCoord);
  319. #define APPLY_FOG_LWRP(pixel, fogFactor) pixel = applyFog(pixel, fogFactor);
  320. #define APPLY_FOG_ADDITIVE(pixel, input) \
  321. UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel.rgb, fixed4(0,0,0,0)); // fog towards black in additive pass
  322. #else
  323. #define APPLY_FOG(pixel, input)
  324. #define APPLY_FOG_LWRP(pixel, fogFactor)
  325. #define APPLY_FOG_ADDITIVE(pixel, input)
  326. #endif
  327. ////////////////////////////////////////
  328. // Texture functions
  329. //
  330. uniform sampler2D _MainTex;
  331. #if _TEXTURE_BLEND
  332. uniform sampler2D _BlendTex;
  333. #if !defined(USE_LWRP) && !defined(USE_URP)
  334. uniform float _BlendAmount;
  335. #endif
  336. inline fixed4 calculateBlendedTexturePixel(float2 texcoord)
  337. {
  338. return (1.0-_BlendAmount) * tex2D(_MainTex, texcoord) + _BlendAmount * tex2D(_BlendTex, texcoord);
  339. }
  340. #endif // _TEXTURE_BLEND
  341. inline fixed4 calculateTexturePixel(float2 texcoord)
  342. {
  343. fixed4 pixel;
  344. #if _TEXTURE_BLEND
  345. pixel = calculateBlendedTexturePixel(texcoord);
  346. #else
  347. pixel = tex2D(_MainTex, texcoord);
  348. #endif // !_TEXTURE_BLEND
  349. #if defined(_COLOR_ADJUST)
  350. pixel = adjustColor(pixel);
  351. #endif // _COLOR_ADJUST
  352. return pixel;
  353. }
  354. #if !defined(USE_LWRP) && !defined(USE_URP)
  355. uniform fixed4 _MainTex_ST;
  356. #endif
  357. inline float2 calculateTextureCoord(float4 texcoord)
  358. {
  359. return TRANSFORM_TEX(texcoord, _MainTex);
  360. }
  361. #endif // SHADER_SHARED_INCLUDED