ShaderShared.cginc 11 KB

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