SceneGrid.bsl 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = "HLSL9";
  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. float4x4 matViewProj;
  86. void main(
  87. in float3 inPos : POSITION,
  88. out float4 oPosition : POSITION,
  89. out float4 oWorldPos : TEXCOORD0)
  90. {
  91. oPosition = mul(matViewProj, float4(inPos.xyz, 1));
  92. oWorldPos = float4(inPos.xyz, 1);
  93. }
  94. };
  95. Fragment =
  96. {
  97. float4 worldCameraPos;
  98. float gridSpacing;
  99. float gridBorderWidth;
  100. float4 gridColor;
  101. float gridFadeOutStart;
  102. float gridFadeOutEnd;
  103. float3 gridPlaneNormal; // Must be one of the basis vectors
  104. float3 sampleGrid(float3 position)
  105. {
  106. float3 count = round(position / float3(gridSpacing, gridSpacing, gridSpacing));
  107. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  108. // the sampled value would always be 1.
  109. float3 remainder = abs(position - count * float3(gridSpacing, gridSpacing, gridSpacing)) + gridPlaneNormal;
  110. float3 derivX = ddx(position);
  111. float3 derivY = ddy(position);
  112. float3 gradientLength = float3(length(float2(derivX.x, derivY.x)), length(float2(derivX.y, derivY.y)),
  113. length(float2(derivX.z, derivY.z)));
  114. float3 borderWidth = float3(gridBorderWidth, gridBorderWidth, gridBorderWidth);
  115. return 1.0f - smoothstep(borderWidth, borderWidth + gradientLength, remainder);
  116. }
  117. float4 main(in float4 inWorldPos : TEXCOORD0) : COLOR0
  118. {
  119. float3 sampValue = sampleGrid(inWorldPos.xyz);
  120. float4 outColor = gridColor;
  121. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  122. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(inWorldPos - worldCameraPos));
  123. return outColor;
  124. }
  125. };
  126. };
  127. };
  128. Technique =
  129. {
  130. Language = "GLSL";
  131. Pass =
  132. {
  133. DepthWrite = false;
  134. Cull = NOCULL;
  135. Target =
  136. {
  137. Blend = true;
  138. Color = { SRCA, SRCIA, ADD };
  139. WriteMask = RGB;
  140. };
  141. Vertex =
  142. {
  143. uniform mat4 matViewProj;
  144. in vec3 bs_position;
  145. out vec3 worldPos;
  146. out gl_PerVertex
  147. {
  148. vec4 gl_Position;
  149. };
  150. void main()
  151. {
  152. vec4 outPos = matViewProj * vec4(bs_position.xyz, 1);
  153. worldPos = bs_position;
  154. gl_Position = outPos;
  155. }
  156. };
  157. Fragment =
  158. {
  159. uniform vec4 worldCameraPos;
  160. uniform float gridSpacing;
  161. uniform float gridBorderWidth;
  162. uniform vec4 gridColor;
  163. uniform float gridFadeOutStart;
  164. uniform float gridFadeOutEnd;
  165. uniform vec3 gridPlaneNormal; // Must be one of the basis vectors
  166. in vec3 worldPos;
  167. out vec4 fragColor;
  168. vec3 sampleGrid(vec3 position)
  169. {
  170. vec3 count = round(position / vec3(gridSpacing, gridSpacing, gridSpacing));
  171. // Adding grid plane normal ensures that the remainer for the unused axis is not zero, otherwise
  172. // the sampled value would always be 1.
  173. vec3 remainder = abs(position - count * vec3(gridSpacing, gridSpacing, gridSpacing)) + gridPlaneNormal;
  174. vec3 derivX = dFdx(position);
  175. vec3 derivY = dFdy(position);
  176. vec3 gradientLength = vec3(length(vec2(derivX.x, derivY.x)), length(vec2(derivX.y, derivY.y)), length(vec2(derivX.z, derivY.z)));
  177. vec3 vecBorderWidth = vec3(gridBorderWidth, gridBorderWidth, gridBorderWidth);
  178. return 1.0f - smoothstep(vecBorderWidth, vecBorderWidth + gradientLength, remainder);
  179. }
  180. void main()
  181. {
  182. vec3 sampValue = sampleGrid(worldPos);
  183. vec4 outColor = gridColor;
  184. outColor.a = max(max(sampValue.x, sampValue.y), sampValue.z) *
  185. smoothstep(gridFadeOutEnd, gridFadeOutStart, length(worldPos - worldCameraPos.xyz));
  186. fragColor = outColor;
  187. }
  188. };
  189. };
  190. };