SDFFunctions.hlsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. float2 UnpackUV(float uv)
  2. {
  3. float2 output;
  4. output.x = floor(uv / 4096.0);
  5. output.y = uv - 4096.0 * output.x;
  6. return output * 0.001953125;
  7. }
  8. float4 BlendARGB(float4 overlying, float4 underlying)
  9. {
  10. overlying.rgb *= overlying.a;
  11. underlying.rgb *= underlying.a;
  12. float3 blended = overlying.rgb + ((1 - overlying.a) * underlying.rgb);
  13. float alpha = underlying.a + (1 - underlying.a) * overlying.a;
  14. return float4(blended / alpha, alpha);
  15. }
  16. float3 GetSpecular(float3 n, float3 l)
  17. {
  18. float spec = pow(max(0.0, dot(n, l)), _Reflectivity);
  19. return _SpecularColor.rgb * spec * _SpecularPower;
  20. }
  21. void GetSurfaceNormal_float(texture2D atlas, float textureWidth, float textureHeight, float2 uv, bool isFront, out float3 nornmal)
  22. {
  23. float3 delta = float3(1.0 / textureWidth, 1.0 / textureHeight, 0.0);
  24. // Read "height field"
  25. float4 h = float4(
  26. SAMPLE_TEXTURE2D(atlas, SamplerState_Linear_Clamp, uv - delta.xz).a,
  27. SAMPLE_TEXTURE2D(atlas, SamplerState_Linear_Clamp, uv + delta.xz).a,
  28. SAMPLE_TEXTURE2D(atlas, SamplerState_Linear_Clamp, uv - delta.zy).a,
  29. SAMPLE_TEXTURE2D(atlas, SamplerState_Linear_Clamp, uv + delta.zy).a);
  30. bool raisedBevel = _BevelType;
  31. h += _BevelOffset;
  32. float bevelWidth = max(.01, _BevelWidth);
  33. // Track outline
  34. h -= .5;
  35. h /= bevelWidth;
  36. h = saturate(h + .5);
  37. if (raisedBevel) h = 1 - abs(h * 2.0 - 1.0);
  38. h = lerp(h, sin(h * 3.141592 / 2.0), float4(_BevelRoundness, _BevelRoundness, _BevelRoundness, _BevelRoundness));
  39. h = min(h, 1.0 - float4(_BevelClamp, _BevelClamp, _BevelClamp, _BevelClamp));
  40. h *= _BevelAmount * bevelWidth * _GradientScale * -2.0;
  41. float3 va = normalize(float3(-1.0, 0.0, h.y - h.x));
  42. float3 vb = normalize(float3(0.0, 1.0, h.w - h.z));
  43. float3 f = float3(1, 1, 1);
  44. if (isFront) f = float3(1, 1, -1);
  45. nornmal = cross(va, vb) * f;
  46. }
  47. void EvaluateLight_float(float4 faceColor, float3 n, out float4 color)
  48. {
  49. n.z = abs(n.z);
  50. float3 light = normalize(float3(sin(_LightAngle), cos(_LightAngle), 1.0));
  51. float3 col = max(faceColor.rgb, 0) + GetSpecular(n, light)* faceColor.a;
  52. //faceColor.rgb += col * faceColor.a;
  53. col *= 1 - (dot(n, light) * _Diffuse);
  54. col *= lerp(_Ambient, 1, n.z * n.z);
  55. //fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDir, -n));
  56. //faceColor.rgb += reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  57. color = float4(col, faceColor.a);
  58. }
  59. // Add custom function to handle time in HDRP
  60. //
  61. void GenerateUV_float(float2 inUV, float4 transform, float2 animSpeed, out float2 outUV)
  62. {
  63. outUV = inUV * transform.xy + transform.zw + (animSpeed * _Time.y);
  64. }
  65. void ComputeUVOffset_float(float texWidth, float texHeight, float2 offset, float SDR, out float2 uvOffset)
  66. {
  67. uvOffset = float2(-offset.x * SDR / texWidth, -offset.y * SDR / texHeight);
  68. }
  69. void ScreenSpaceRatio2_float(float4x4 projection, float4 position, float2 objectScale, float screenWidth, float screenHeight, float fontScale, out float SSR)
  70. {
  71. float2 pixelSize = position.w;
  72. pixelSize /= (objectScale * mul((float2x2)projection, float2(screenWidth, screenHeight)));
  73. SSR = rsqrt(dot(pixelSize, pixelSize)*2) * fontScale;
  74. }
  75. // UV : Texture coordinate of the source distance field texture
  76. // TextureSize : Size of the source distance field texture
  77. // Filter : Enable perspective filter (soften)
  78. void ScreenSpaceRatio_float(float2 UV, float TextureSize, bool Filter, out float SSR)
  79. {
  80. if(Filter)
  81. {
  82. float2 a = float2(ddx(UV.x), ddy(UV.x));
  83. float2 b = float2(ddx(UV.y), ddy(UV.y));
  84. float s = lerp(dot(a,a), dot(b,b), 0.5);
  85. SSR = rsqrt(s) / TextureSize;
  86. }
  87. else
  88. {
  89. float s = rsqrt(abs(ddx(UV.x) * ddy(UV.y) - ddy(UV.x) * ddx(UV.y)));
  90. SSR = s / TextureSize;
  91. }
  92. }
  93. // SSR : Screen Space Ratio
  94. // SD : Signed Distance (encoded : Distance / SDR + .5)
  95. // SDR : Signed Distance Ratio
  96. //
  97. // IsoPerimeter : Dilate / Contract the shape
  98. void ComputeSDF_float(float SSR, float SD, float SDR, float isoPerimeter, float softness, out float outAlpha)
  99. {
  100. softness *= SSR * SDR;
  101. float d = (SD - 0.5) * SDR; // Signed distance to edge, in Texture space
  102. outAlpha = saturate((d * 2.0 * SSR + 0.5 + isoPerimeter * SDR * SSR + softness * 0.5) / (1.0 + softness)); // Screen pixel coverage (alpha)
  103. }
  104. void ComputeSDF2_float(float SSR, float SD, float SDR, float2 isoPerimeter, float2 softness, out float2 outAlpha)
  105. {
  106. softness *= SSR * SDR;
  107. float d = (SD - 0.5f) * SDR;
  108. outAlpha = saturate((d * 2.0f * SSR + 0.5f + isoPerimeter * SDR * SSR + softness * 0.5) / (1.0 + softness));
  109. }
  110. void ComputeSDF4_float(float SSR, float SD, float SDR, float4 isoPerimeter, float4 softness, out float4 outAlpha)
  111. {
  112. softness *= SSR * SDR;
  113. float d = (SD - 0.5f) * SDR;
  114. outAlpha = saturate((d * 2.0f * SSR + 0.5f + isoPerimeter * SDR * SSR + softness * 0.5) / (1.0 + softness));
  115. }
  116. void ComputeSDF44_float(float SSR, float4 SD, float SDR, float4 isoPerimeter, float4 softness, bool outline, out float4 outAlpha)
  117. {
  118. softness *= SSR * SDR;
  119. float4 d = (SD - 0.5f) * SDR;
  120. if(outline) d.w = max(max(d.x, d.y), d.z);
  121. outAlpha = saturate((d * 2.0f * SSR + 0.5f + isoPerimeter * SDR * SSR + softness * 0.5) / (1.0 + softness));
  122. }
  123. void Composite_float(float4 overlying, float4 underlying, out float4 outColor)
  124. {
  125. outColor = BlendARGB(overlying, underlying);
  126. }
  127. // Face only
  128. void Layer1_float(float alpha, float4 color0, out float4 outColor)
  129. {
  130. color0.a *= alpha;
  131. outColor = color0;
  132. }
  133. // Face + 1 Outline
  134. void Layer2_float(float2 alpha, float4 color0, float4 color1, out float4 outColor)
  135. {
  136. color1.a *= alpha.y;
  137. color0.rgb *= color0.a; color1.rgb *= color1.a;
  138. outColor = lerp(color1, color0, alpha.x);
  139. outColor.rgb /= outColor.a;
  140. }
  141. // Face + 3 Outline
  142. void Layer4_float(float4 alpha, float4 color0, float4 color1, float4 color2, float4 color3, out float4 outColor)
  143. {
  144. color3.a *= alpha.w;
  145. color0.rgb *= color0.a; color1.rgb *= color1.a; color2.rgb *= color2.a; color3.rgb *= color3.a;
  146. outColor = lerp(lerp(lerp(color3, color2, alpha.z), color1, alpha.y), color0, alpha.x);
  147. outColor.rgb /= outColor.a;
  148. }