SceneGrid.bsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. Language = "HLSL11";
  16. Pass =
  17. {
  18. DepthWrite = false;
  19. Cull = NOCULL;
  20. Target =
  21. {
  22. Blend = true;
  23. Color = { SRCA, SRCIA, ADD };
  24. WriteMask = RGB;
  25. };
  26. Vertex =
  27. {
  28. float4x4 matViewProj;
  29. void main(
  30. in float3 inPos : POSITION,
  31. out float4 oPosition : SV_Position,
  32. out float3 oWorldPos : TEXCOORD0)
  33. {
  34. oPosition = mul(matViewProj, float4(inPos.xyz, 1));
  35. oWorldPos = inPos;
  36. }
  37. };
  38. Fragment =
  39. {
  40. float4 worldCameraPos;
  41. float gridSpacing;
  42. float gridBorderWidth;
  43. float4 gridColor;
  44. float gridFadeOutStart;
  45. float gridFadeOutEnd;
  46. float3 gridPlaneNormal; // Must be one of the basis vectors
  47. float3 sampleGrid(float3 position)
  48. {
  49. float3 count = round(position / gridSpacing);
  50. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  51. // the sampled value would always be 1.
  52. float3 remainder = abs(position - count * gridSpacing) + gridPlaneNormal;
  53. float3 derivX = ddx(position);
  54. float3 derivY = ddy(position);
  55. float3 gradientLength = float3(length(float2(derivX.x, derivY.x)),
  56. length(float2(derivX.y, derivY.y)), length(float2(derivX.z, derivY.z)));
  57. return 1.0f - smoothstep(gridBorderWidth, gridBorderWidth + gradientLength, remainder);
  58. }
  59. float4 main(in float4 inPos : SV_Position, in float3 inWorldPos : TEXCOORD0) : SV_Target
  60. {
  61. float3 sampValue = sampleGrid(inWorldPos);
  62. float4 outColor = gridColor;
  63. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  64. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(inWorldPos - worldCameraPos));
  65. return outColor;
  66. }
  67. };
  68. };
  69. };
  70. Technique =
  71. {
  72. Language = "GLSL";
  73. Pass =
  74. {
  75. DepthWrite = false;
  76. Cull = NOCULL;
  77. Target =
  78. {
  79. Blend = true;
  80. Color = { SRCA, SRCIA, ADD };
  81. WriteMask = RGB;
  82. };
  83. Vertex =
  84. {
  85. layout(location = 0) in vec3 bs_position;
  86. layout(location = 0) out vec3 worldPos;
  87. out gl_PerVertex
  88. {
  89. vec4 gl_Position;
  90. };
  91. layout(binding = 0) uniform VertUBO
  92. {
  93. mat4 matViewProj;
  94. };
  95. void main()
  96. {
  97. vec4 outPos = matViewProj * vec4(bs_position.xyz, 1);
  98. worldPos = bs_position;
  99. gl_Position = outPos;
  100. }
  101. };
  102. Fragment =
  103. {
  104. layout(location = 0) in vec3 worldPos;
  105. layout(location = 0) out vec4 fragColor;
  106. layout(binding = 1) uniform FragUBO
  107. {
  108. vec4 worldCameraPos;
  109. float gridSpacing;
  110. float gridBorderWidth;
  111. vec4 gridColor;
  112. float gridFadeOutStart;
  113. float gridFadeOutEnd;
  114. vec3 gridPlaneNormal; // Must be one of the basis vectors
  115. };
  116. vec3 sampleGrid(vec3 position)
  117. {
  118. vec3 count = round(position / vec3(gridSpacing, gridSpacing, gridSpacing));
  119. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  120. // the sampled value would always be 1.
  121. vec3 remainder = abs(position - count * vec3(gridSpacing, gridSpacing, gridSpacing)) + gridPlaneNormal;
  122. vec3 derivX = dFdx(position);
  123. vec3 derivY = dFdy(position);
  124. vec3 gradientLength = vec3(length(vec2(derivX.x, derivY.x)), length(vec2(derivX.y, derivY.y)), length(vec2(derivX.z, derivY.z)));
  125. vec3 vecBorderWidth = vec3(gridBorderWidth, gridBorderWidth, gridBorderWidth);
  126. return 1.0f - smoothstep(vecBorderWidth, vecBorderWidth + gradientLength, remainder);
  127. }
  128. void main()
  129. {
  130. vec3 sampValue = sampleGrid(worldPos);
  131. vec4 outColor = gridColor;
  132. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  133. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(worldPos - worldCameraPos.xyz));
  134. fragColor = outColor;
  135. }
  136. };
  137. };
  138. };