ShaderShared.cginc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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(_TINT_BLACK_ON)
  117. const bool applyPMA = false;
  118. #else
  119. const bool applyPMA = true;
  120. #endif
  121. #if defined(_ALPHABLEND_ON)
  122. //Normal Alpha
  123. if (applyPMA) finalPixel.rgb *= finalPixel.a;
  124. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)
  125. //PMA vertex, straight texture
  126. if (applyPMA) finalPixel.rgb *= textureAlpha;
  127. #elif defined(_ALPHAPREMULTIPLY_ON)
  128. //Pre multiplied alpha, both vertex and texture
  129. // texture and vertex colors are premultiplied already
  130. #elif defined(_MULTIPLYBLEND)
  131. //Multiply
  132. finalPixel = lerp(fixed4(1,1,1,1), finalPixel, finalPixel.a);
  133. #elif defined(_MULTIPLYBLEND_X2)
  134. //Multiply x2
  135. finalPixel.rgb *= 2.0f;
  136. finalPixel = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), finalPixel, finalPixel.a);
  137. #elif defined(_ADDITIVEBLEND)
  138. //Additive
  139. finalPixel *= 2.0f;
  140. if (applyPMA) finalPixel.rgb *= colorAlpha;
  141. #elif defined(_ADDITIVEBLEND_SOFT)
  142. //Additive soft
  143. if (applyPMA) finalPixel.rgb *= finalPixel.a;
  144. #else
  145. //Opaque
  146. finalPixel.a = 1;
  147. #endif
  148. return finalPixel;
  149. }
  150. inline fixed4 calculateLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target
  151. {
  152. #if !defined(_TINT_BLACK_ON)
  153. fixed4 finalPixel = texureColor * color * fixed4(lighting, 1);
  154. #else
  155. fixed4 finalPixel = texureColor * fixed4(lighting, 1);
  156. #endif
  157. finalPixel = prepareLitPixelForOutput(finalPixel, texureColor.a, color.a);
  158. return finalPixel;
  159. }
  160. inline fixed4 calculateLitPixel(fixed4 texureColor, fixed3 lighting) : SV_Target
  161. {
  162. // note: we let the optimizer work, removed duplicate code.
  163. return calculateLitPixel(texureColor, fixed4(1, 1, 1, 1), lighting);
  164. }
  165. inline fixed4 calculateAdditiveLitPixel(fixed4 texureColor, fixed4 color, fixed3 lighting) : SV_Target
  166. {
  167. fixed4 finalPixel;
  168. #if defined(_ALPHABLEND_ON) || defined(_MULTIPLYBLEND) || defined(_MULTIPLYBLEND_X2) || defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  169. //Normal Alpha, Additive and Multiply modes
  170. finalPixel.rgb = (texureColor.rgb * lighting * color.rgb) * (texureColor.a * color.a);
  171. finalPixel.a = 1.0;
  172. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)
  173. //PMA vertex, straight texture
  174. finalPixel.rgb = texureColor.rgb * lighting * color.rgb * texureColor.a;
  175. finalPixel.a = 1.0;
  176. #elif defined(_ALPHAPREMULTIPLY_ON)
  177. //Pre multiplied alpha, both vertex and texture
  178. finalPixel.rgb = texureColor.rgb * lighting * color.rgb;
  179. finalPixel.a = 1.0;
  180. #else
  181. //Opaque
  182. finalPixel.rgb = texureColor.rgb * lighting * color.rgb;
  183. finalPixel.a = 1.0;
  184. #endif
  185. return finalPixel;
  186. }
  187. inline fixed4 calculateAdditiveLitPixel(fixed4 texureColor, fixed3 lighting) : SV_Target
  188. {
  189. fixed4 finalPixel;
  190. #if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) || defined(_MULTIPLYBLEND) || defined(_MULTIPLYBLEND_X2) || defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  191. //Normal Alpha, Additive and Multiply modes
  192. finalPixel.rgb = (texureColor.rgb * lighting) * texureColor.a;
  193. finalPixel.a = 1.0;
  194. #else
  195. //Pre multiplied alpha and Opaque
  196. finalPixel.rgb = texureColor.rgb * lighting;
  197. finalPixel.a = 1.0;
  198. #endif
  199. return finalPixel;
  200. }
  201. inline fixed4 calculatePixel(fixed4 texureColor, fixed4 color) : SV_Target
  202. {
  203. // note: we let the optimizer work, removed duplicate code.
  204. return calculateLitPixel(texureColor, color, fixed3(1, 1, 1));
  205. }
  206. inline fixed4 calculatePixel(fixed4 texureColor) : SV_Target
  207. {
  208. // note: we let the optimizer work, removed duplicate code.
  209. return calculateLitPixel(texureColor, fixed4(1, 1, 1, 1), fixed3(1, 1, 1));
  210. }
  211. ////////////////////////////////////////
  212. // Alpha Clipping
  213. //
  214. #if defined(_ALPHA_CLIP)
  215. #if !defined(USE_LWRP) && !defined(USE_URP)
  216. uniform fixed _Cutoff;
  217. #endif
  218. #define ALPHA_CLIP(pixel, color) clip((pixel.a * color.a) - _Cutoff);
  219. #else
  220. #define ALPHA_CLIP(pixel, color)
  221. #endif
  222. ////////////////////////////////////////
  223. // Additive Slot blend mode
  224. // return unlit textureColor, alpha clip textureColor.a only
  225. //
  226. // [Deprecated] RETURN_UNLIT_IF_ADDITIVE_SLOT macro will be removed in future versions.
  227. // Use RETURN_UNLIT_IF_ADDITIVE_SLOT_TINT instead.
  228. #if defined(_ALPHAPREMULTIPLY_ON) && !defined(_LIGHT_AFFECTS_ADDITIVE)
  229. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor) \
  230. if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
  231. ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
  232. return texureColor * vertexColor;\
  233. }
  234. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) && !defined(_LIGHT_AFFECTS_ADDITIVE)
  235. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor) \
  236. if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
  237. ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
  238. return texureColor * texureColor.a * vertexColor;\
  239. }
  240. #else
  241. #define RETURN_UNLIT_IF_ADDITIVE_SLOT(textureColor, vertexColor)
  242. #endif
  243. // Replacement for deprecated RETURN_UNLIT_IF_ADDITIVE_SLOT macro.
  244. #if (defined(_ALPHAPREMULTIPLY_ON) || defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)) && !defined(_LIGHT_AFFECTS_ADDITIVE)
  245. #if defined(_TINT_BLACK_ON)
  246. #define TINTED_RESULT_PIXEL(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA) fragTintedColor(texureColor, darkVertexColor, vertexColor, lightColorA, darkColorA)
  247. #elif defined(_ALPHAPREMULTIPLY_VERTEX_ONLY)
  248. #define TINTED_RESULT_PIXEL(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA) (texureColor * texureColor.a * vertexColor)
  249. #else
  250. #define TINTED_RESULT_PIXEL(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA) (texureColor * vertexColor)
  251. #endif
  252. #define RETURN_UNLIT_IF_ADDITIVE_SLOT_TINT(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA) \
  253. if (vertexColor.a == 0 && (vertexColor.r || vertexColor.g || vertexColor.b)) {\
  254. ALPHA_CLIP(texureColor, fixed4(1, 1, 1, 1))\
  255. return TINTED_RESULT_PIXEL(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA);\
  256. }
  257. #else
  258. #define RETURN_UNLIT_IF_ADDITIVE_SLOT_TINT(textureColor, vertexColor, darkVertexColor, lightColorA, darkColorA)
  259. #endif
  260. ////////////////////////////////////////
  261. // Color functions
  262. //
  263. #if !defined(USE_LWRP) && !defined(USE_URP)
  264. uniform fixed4 _Color;
  265. #if defined(_TINT_BLACK_ON)
  266. uniform fixed4 _Black;
  267. #endif
  268. #endif
  269. inline fixed4 calculateVertexColor(fixed4 color)
  270. {
  271. #if defined(_ALPHAPREMULTIPLY_ON) || _ALPHAPREMULTIPLY_VERTEX_ONLY
  272. return PMAGammaToTargetSpace(color) * _Color;
  273. #elif !defined (UNITY_COLORSPACE_GAMMA)
  274. return fixed4(GammaToLinearSpace(color.rgb), color.a) * _Color;
  275. #else
  276. return color * _Color;
  277. #endif
  278. }
  279. #if defined(_COLOR_ADJUST)
  280. #if !defined(USE_LWRP) && !defined(USE_URP)
  281. uniform float _Hue;
  282. uniform float _Saturation;
  283. uniform float _Brightness;
  284. uniform fixed4 _OverlayColor;
  285. #endif
  286. float3 rgb2hsv(float3 c)
  287. {
  288. float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
  289. float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
  290. float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
  291. float d = q.x - min(q.w, q.y);
  292. float e = 1.0e-10;
  293. return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
  294. }
  295. float3 hsv2rgb(float3 c)
  296. {
  297. c = float3(c.x, clamp(c.yz, 0.0, 1.0));
  298. float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
  299. float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
  300. return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
  301. }
  302. inline fixed4 adjustColor(fixed4 color)
  303. {
  304. float3 hsv = rgb2hsv(color.rgb);
  305. hsv.x += _Hue;
  306. hsv.y *= _Saturation;
  307. hsv.z *= _Brightness;
  308. color.rgb = hsv2rgb(hsv);
  309. return color;
  310. }
  311. #define COLORISE(pixel) pixel.rgb = lerp(pixel.rgb, _OverlayColor.rgb, _OverlayColor.a * pixel.a);
  312. #define COLORISE_ADDITIVE(pixel) pixel.rgb = ((1.0-_OverlayColor.a) * pixel.rgb);
  313. #else // !_COLOR_ADJUST
  314. #define COLORISE(pixel)
  315. #define COLORISE_ADDITIVE(pixel)
  316. #endif // !_COLOR_ADJUST
  317. ////////////////////////////////////////
  318. // Fog
  319. //
  320. #if defined(_FOG) && (defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2))
  321. inline fixed4 applyFog(fixed4 pixel, float fogCoordOrFactorAtLWRP)
  322. {
  323. #if defined(_ADDITIVEBLEND) || defined(_ADDITIVEBLEND_SOFT)
  324. //In additive mode blend from clear to black based on luminance
  325. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  326. fixed4 fogColor = lerp(fixed4(0,0,0,0), fixed4(0,0,0,1), luminance);
  327. #elif defined(_MULTIPLYBLEND)
  328. //In multiplied mode fade to white based on inverse luminance
  329. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  330. fixed4 fogColor = lerp(fixed4(1,1,1,1), fixed4(0,0,0,0), luminance);
  331. #elif defined(_MULTIPLYBLEND_X2)
  332. //In multipliedx2 mode fade to grey based on inverse luminance
  333. float luminance = pixel.r * 0.3 + pixel.g * 0.59 + pixel.b * 0.11;
  334. fixed4 fogColor = lerp(fixed4(0.5f,0.5f,0.5f,0.5f), fixed4(0,0,0,0), luminance);
  335. #elif defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_VERTEX_ONLY) || defined(_ALPHAPREMULTIPLY_ON)
  336. //In alpha blended modes blend to fog color based on pixel alpha
  337. fixed4 fogColor = lerp(fixed4(0,0,0,0), unity_FogColor, pixel.a);
  338. #else
  339. //In opaque mode just return fog color;
  340. fixed4 fogColor = unity_FogColor;
  341. #endif
  342. #if defined(USE_LWRP) || defined(USE_URP)
  343. pixel.rgb = MixFogColor(pixel.rgb, fogColor.rgb, fogCoordOrFactorAtLWRP);
  344. #else
  345. UNITY_APPLY_FOG_COLOR(fogCoordOrFactorAtLWRP, pixel, fogColor);
  346. #endif
  347. return pixel;
  348. }
  349. #define APPLY_FOG(pixel, input) pixel = applyFog(pixel, input.fogCoord);
  350. #define APPLY_FOG_LWRP(pixel, fogFactor) pixel = applyFog(pixel, fogFactor);
  351. #define APPLY_FOG_ADDITIVE(pixel, input) \
  352. UNITY_APPLY_FOG_COLOR(input.fogCoord, pixel.rgb, fixed4(0,0,0,0)); // fog towards black in additive pass
  353. #else
  354. #define APPLY_FOG(pixel, input)
  355. #define APPLY_FOG_LWRP(pixel, fogFactor)
  356. #define APPLY_FOG_ADDITIVE(pixel, input)
  357. #endif
  358. ////////////////////////////////////////
  359. // Texture functions
  360. //
  361. uniform sampler2D _MainTex;
  362. #if _TEXTURE_BLEND
  363. uniform sampler2D _BlendTex;
  364. #if !defined(USE_LWRP) && !defined(USE_URP)
  365. uniform float _BlendAmount;
  366. #endif
  367. inline fixed4 calculateBlendedTexturePixel(float2 texcoord)
  368. {
  369. return (1.0-_BlendAmount) * tex2D(_MainTex, texcoord) + _BlendAmount * tex2D(_BlendTex, texcoord);
  370. }
  371. #endif // _TEXTURE_BLEND
  372. inline fixed4 calculateTexturePixel(float2 texcoord)
  373. {
  374. fixed4 pixel;
  375. #if _TEXTURE_BLEND
  376. pixel = calculateBlendedTexturePixel(texcoord);
  377. #else
  378. pixel = tex2D(_MainTex, texcoord);
  379. #endif // !_TEXTURE_BLEND
  380. #if defined(_COLOR_ADJUST)
  381. pixel = adjustColor(pixel);
  382. #endif // _COLOR_ADJUST
  383. return pixel;
  384. }
  385. #if !defined(USE_LWRP) && !defined(USE_URP)
  386. uniform fixed4 _MainTex_ST;
  387. #endif
  388. inline float2 calculateTextureCoord(float4 texcoord)
  389. {
  390. return TRANSFORM_TEX(texcoord, _MainTex);
  391. }
  392. #endif // SHADER_SHARED_INCLUDED