SceneGrid.bsl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. options
  2. {
  3. priority = -10000;
  4. };
  5. technique SceneGrid
  6. {
  7. depth
  8. {
  9. write = false;
  10. };
  11. raster
  12. {
  13. cull = none;
  14. };
  15. blend
  16. {
  17. target
  18. {
  19. enabled = true;
  20. color = { srcA, srcIA, add };
  21. writemask = RGB;
  22. };
  23. };
  24. code
  25. {
  26. cbuffer VertParams
  27. {
  28. float4x4 matViewProj;
  29. };
  30. void vsmain(
  31. in float3 inPos : POSITION,
  32. out float4 oPosition : SV_Position,
  33. out float3 oWorldPos : TEXCOORD0)
  34. {
  35. oPosition = mul(matViewProj, float4(inPos.xyz, 1));
  36. oWorldPos = inPos;
  37. }
  38. cbuffer FragParams
  39. {
  40. float4 worldCameraPos;
  41. float gridSpacing;
  42. float gridBorderWidth;
  43. [color]
  44. float4 gridColor;
  45. float gridFadeOutStart;
  46. float gridFadeOutEnd;
  47. float3 gridPlaneNormal; // Must be one of the basis vectors
  48. };
  49. float3 sampleGrid(float3 position)
  50. {
  51. float3 count = round(position / gridSpacing);
  52. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  53. // the sampled value would always be 1.
  54. float3 remainder = abs(position - count * gridSpacing) + gridPlaneNormal;
  55. float3 derivX = ddx(position);
  56. float3 derivY = ddy(position);
  57. float3 gradientLength = float3(length(float2(derivX.x, derivY.x)),
  58. length(float2(derivX.y, derivY.y)), length(float2(derivX.z, derivY.z)));
  59. return 1.0f - smoothstep(gridBorderWidth, gridBorderWidth + gradientLength, remainder);
  60. }
  61. float4 fsmain(in float4 inPos : SV_Position, in float3 inWorldPos : TEXCOORD0) : SV_Target
  62. {
  63. float3 sampValue = sampleGrid(inWorldPos);
  64. float4 outColor = gridColor;
  65. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  66. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(inWorldPos - worldCameraPos));
  67. return outColor;
  68. }
  69. };
  70. };