SceneGrid.bsl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Priority = -10000;
  2. Parameters =
  3. {
  4. mat4x4 matViewProj;
  5. float4 worldCameraPos;
  6. color gridColor;
  7. float3 gridPlaneNormal;
  8. float gridSpacing;
  9. float gridBorderWidth;
  10. float gridFadeOutStart;
  11. float gridFadeOutEnd;
  12. };
  13. Technique =
  14. {
  15. Pass =
  16. {
  17. DepthWrite = false;
  18. Cull = NOCULL;
  19. Target =
  20. {
  21. Blend = true;
  22. Color = { SRCA, SRCIA, ADD };
  23. WriteMask = RGB;
  24. };
  25. Vertex =
  26. {
  27. cbuffer VertParams
  28. {
  29. float4x4 matViewProj;
  30. };
  31. void main(
  32. in float3 inPos : POSITION,
  33. out float4 oPosition : SV_Position,
  34. out float3 oWorldPos : TEXCOORD0)
  35. {
  36. oPosition = mul(matViewProj, float4(inPos.xyz, 1));
  37. oWorldPos = inPos;
  38. }
  39. };
  40. Fragment =
  41. {
  42. cbuffer FragParams
  43. {
  44. float4 worldCameraPos;
  45. float gridSpacing;
  46. float gridBorderWidth;
  47. float4 gridColor;
  48. float gridFadeOutStart;
  49. float gridFadeOutEnd;
  50. float3 gridPlaneNormal; // Must be one of the basis vectors
  51. };
  52. float3 sampleGrid(float3 position)
  53. {
  54. float3 count = round(position / gridSpacing);
  55. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  56. // the sampled value would always be 1.
  57. float3 remainder = abs(position - count * gridSpacing) + gridPlaneNormal;
  58. float3 derivX = ddx(position);
  59. float3 derivY = ddy(position);
  60. float3 gradientLength = float3(length(float2(derivX.x, derivY.x)),
  61. length(float2(derivX.y, derivY.y)), length(float2(derivX.z, derivY.z)));
  62. return 1.0f - smoothstep(gridBorderWidth, gridBorderWidth + gradientLength, remainder);
  63. }
  64. float4 main(in float4 inPos : SV_Position, in float3 inWorldPos : TEXCOORD0) : SV_Target
  65. {
  66. float3 sampValue = sampleGrid(inWorldPos);
  67. float4 outColor = gridColor;
  68. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  69. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(inWorldPos - worldCameraPos));
  70. return outColor;
  71. }
  72. };
  73. };
  74. };