Skip to content

Commit e39ac46

Browse files
authored
Added WGSL code for grid script (#7805)
1 parent 2e97e3c commit e39ac46

File tree

1 file changed

+117
-4
lines changed

1 file changed

+117
-4
lines changed

scripts/esm/grid.mjs

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const tmpVa = new Vec2();
1616

1717
const EPISILON = 1e-3;
1818

19-
const vertexCode = /* glsl */ `
19+
const vertexGLSL = /* glsl */ `
2020
attribute vec3 vertex_position;
2121
attribute vec2 aUv0;
2222
@@ -31,7 +31,7 @@ const vertexCode = /* glsl */ `
3131
}
3232
`;
3333

34-
const fragmentCode = /* glsl */ `
34+
const fragmentGLSL = /* glsl */ `
3535
uniform vec2 uHalfExtents;
3636
uniform vec3 uColorX;
3737
uniform vec3 uColorZ;
@@ -121,6 +121,117 @@ const fragmentCode = /* glsl */ `
121121
}
122122
`;
123123

124+
const vertexWGSL = /* wgsl */ `
125+
attribute vertex_position: vec3f;
126+
attribute aUv0: vec2f;
127+
128+
uniform matrix_model: mat4x4f;
129+
uniform matrix_viewProjection: mat4x4f;
130+
131+
varying uv0: vec2f;
132+
133+
@vertex
134+
fn vertexMain(input: VertexInput) -> VertexOutput {
135+
var output: VertexOutput;
136+
output.position = uniform.matrix_viewProjection * uniform.matrix_model * vec4f(input.vertex_position, 1.0);
137+
output.uv0 = input.aUv0;
138+
return output;
139+
}
140+
`;
141+
142+
const fragmentWGSL = /* wgsl */ `
143+
uniform uHalfExtents: vec2f;
144+
uniform uColorX: vec3f;
145+
uniform uColorZ: vec3f;
146+
uniform uResolution: u32;
147+
148+
varying uv0: vec2f;
149+
150+
// https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8#1e7c
151+
fn pristineGrid(uv: vec2f, ddx: vec2f, ddy: vec2f, lineWidth: vec2f) -> f32 {
152+
let uvDeriv = vec2f(length(vec2f(ddx.x, ddy.x)), length(vec2f(ddx.y, ddy.y)));
153+
let invertLine = vec2<bool>(lineWidth.x > 0.5, lineWidth.y > 0.5);
154+
let targetWidth = vec2f(
155+
select(lineWidth.x, 1.0 - lineWidth.x, invertLine.x),
156+
select(lineWidth.y, 1.0 - lineWidth.y, invertLine.y)
157+
);
158+
let drawWidth = clamp(targetWidth, uvDeriv, vec2f(0.5));
159+
let lineAA = uvDeriv * 1.5;
160+
var gridUV = abs(fract(uv) * 2.0 - 1.0);
161+
gridUV.x = select(1.0 - gridUV.x, gridUV.x, invertLine.x);
162+
gridUV.y = select(1.0 - gridUV.y, gridUV.y, invertLine.y);
163+
var grid2 = smoothstep(drawWidth + lineAA, drawWidth - lineAA, gridUV);
164+
165+
grid2 *= clamp(targetWidth / drawWidth, vec2f(0.0), vec2f(1.0));
166+
grid2 = mix(grid2, targetWidth, clamp(uvDeriv * 2.0 - 1.0, vec2f(0.0), vec2f(1.0)));
167+
grid2.x = select(grid2.x, 1.0 - grid2.x, invertLine.x);
168+
grid2.y = select(grid2.y, 1.0 - grid2.y, invertLine.y);
169+
170+
return mix(grid2.x, 1.0, grid2.y);
171+
}
172+
173+
@fragment
174+
fn fragmentMain(input: FragmentInput) -> FragmentOutput {
175+
var output: FragmentOutput;
176+
let uv: vec2f = input.uv0;
177+
178+
let pos: vec2f = (uv * 2.0 - 1.0) * uniform.uHalfExtents;
179+
let ddx: vec2f = dpdx(pos);
180+
let ddy: vec2f = dpdy(pos);
181+
182+
let epsilon: f32 = 1.0 / 255.0;
183+
184+
var levelPos: vec2f;
185+
var levelSize: f32;
186+
var levelAlpha: f32;
187+
188+
levelPos = pos * 0.1;
189+
levelSize = 2.0 / 1000.0;
190+
levelAlpha = pristineGrid(levelPos, ddx * 0.1, ddy * 0.1, vec2f(levelSize));
191+
if (levelAlpha > epsilon) {
192+
var color: vec3f;
193+
if (abs(levelPos.x) < levelSize) {
194+
if (abs(levelPos.y) < levelSize) {
195+
color = vec3f(1.0);
196+
} else {
197+
color = uniform.uColorZ;
198+
}
199+
} else if (abs(levelPos.y) < levelSize) {
200+
color = uniform.uColorX;
201+
} else {
202+
color = vec3f(0.9);
203+
}
204+
output.color = vec4f(color, levelAlpha);
205+
return output;
206+
}
207+
208+
levelPos = pos;
209+
levelSize = 1.0 / 100.0;
210+
levelAlpha = pristineGrid(levelPos, ddx, ddy, vec2f(levelSize));
211+
if (levelAlpha > epsilon) {
212+
if (uniform.uResolution < 1) {
213+
discard;
214+
}
215+
output.color = vec4f(vec3f(0.7), levelAlpha);
216+
return output;
217+
}
218+
219+
levelPos = pos * 10.0;
220+
levelSize = 1.0 / 100.0;
221+
levelAlpha = pristineGrid(levelPos, ddx * 10.0, ddy * 10.0, vec2f(levelSize));
222+
if (levelAlpha > epsilon) {
223+
if (uniform.uResolution < 2) {
224+
discard;
225+
}
226+
output.color = vec4f(vec3f(0.7), levelAlpha);
227+
return output;
228+
}
229+
230+
discard;
231+
return output;
232+
}
233+
`;
234+
124235
class Grid extends Script {
125236
static scriptName = 'grid';
126237

@@ -190,8 +301,10 @@ class Grid extends Script {
190301
// create shader material
191302
this._material = new ShaderMaterial({
192303
uniqueName: 'grid-shader',
193-
vertexGLSL: vertexCode,
194-
fragmentGLSL: fragmentCode,
304+
vertexGLSL: vertexGLSL,
305+
fragmentGLSL: fragmentGLSL,
306+
vertexWGSL: vertexWGSL,
307+
fragmentWGSL: fragmentWGSL,
195308
attributes: {
196309
vertex_position: SEMANTIC_POSITION,
197310
aUv0: SEMANTIC_TEXCOORD0

0 commit comments

Comments
 (0)