export const vertexGLSL: "\n attribute vec3 vertex_position;\n\n uniform mat4 matrix_model;\n uniform mat4 matrix_view;\n uniform mat4 matrix_viewProjection;\n uniform vec3 view_position;\n uniform vec4 camera_params;\n\n uniform vec3 uAxisU;\n uniform vec3 uAxisV;\n uniform float uInfinite;\n uniform vec2 uFade;\n uniform float uDepthOffset;\n\n varying vec2 vGridPos;\n varying vec3 vWorldPos;\n varying vec4 vClipPos;\n varying vec3 vToEye;\n varying vec3 vNormal;\n\n void main(void) {\n\n // the node's scale sizes a fixed grid, never its cells\n vec3 origin = matrix_model[3].xyz;\n vec3 axisU = mat3(matrix_model) * uAxisU;\n vec3 axisV = mat3(matrix_model) * uAxisV;\n vec3 u = normalize(axisU);\n vec3 v = normalize(axisV);\n\n // an infinite grid follows the camera and spans the fade distance\n float reach = 2.0 * min(uFade.y, camera_params.y * 0.9);\n vec2 center = vec2(dot(view_position - origin, u), dot(view_position - origin, v)) * uInfinite;\n vGridPos = center + vertex_position.xz * mix(vec2(length(axisU), length(axisV)), vec2(reach), uInfinite);\n vWorldPos = origin + u * vGridPos.x + v * vGridPos.y;\n\n // offset along the view ray, so only depth changes\n vec3 back = vec3(matrix_view[0][2], matrix_view[1][2], matrix_view[2][2]);\n vec3 toEye = view_position - vWorldPos;\n if (camera_params.w > 0.5) {\n toEye = back * dot(toEye, back);\n }\n gl_Position = matrix_viewProjection * vec4(vWorldPos + toEye * uDepthOffset, 1.0);\n vClipPos = gl_Position;\n vToEye = toEye;\n vNormal = cross(u, v);\n }\n"; export const fragmentGLSL: "\n #include \"gammaPS\"\n\n uniform vec3 view_position;\n uniform vec4 camera_params;\n\n uniform vec2 uFade;\n uniform float uSpacing;\n uniform float uSubdivisions;\n uniform vec3 uThickness;\n uniform float uPixelRatio;\n uniform float uShowAxes;\n uniform vec4 uColorU;\n uniform vec4 uColorV;\n uniform vec4 uMajorColor;\n uniform vec4 uMinorColor;\n\n #ifdef GRID_CONTRAST\n uniform sampler2D uSceneColorMap;\n #endif\n\n #ifdef GRID_DEPTH_DITHER\n uniform sampler2D blueNoiseTex32;\n #endif\n\n varying vec2 vGridPos;\n varying vec3 vWorldPos;\n varying vec4 vClipPos;\n varying vec3 vToEye;\n varying vec3 vNormal;\n\n // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8#1e7c, with lines in pixels\n float pristineGrid(vec2 uv, vec2 uvDeriv, float pixels, vec2 fade) {\n vec2 targetWidth = min(pixels * uvDeriv, vec2(min(pixels / (64.0 * uPixelRatio), 0.5)));\n vec2 drawWidth = clamp(targetWidth, uvDeriv, vec2(0.5));\n vec2 lineAA = uvDeriv * 1.5;\n vec2 gridUV = 1.0 - abs(fract(uv) * 2.0 - 1.0);\n vec2 grid2 = smoothstep(drawWidth + lineAA, drawWidth - lineAA, gridUV);\n grid2 *= clamp(targetWidth / drawWidth, 0.0, 1.0);\n grid2 = mix(grid2, targetWidth, clamp(uvDeriv * 2.0 - 1.0, 0.0, 1.0));\n grid2 *= fade;\n return mix(grid2.x, 1.0, grid2.y);\n }\n\n // drops the origin line when the axes draw it\n vec2 offAxis(vec2 uv) {\n return uShowAxes > 0.5 ? step(0.5, abs(uv)) : vec2(1.0);\n }\n\n void main(void) {\n vec2 pos = vGridPos / uSpacing;\n vec2 ddx = dFdx(pos);\n vec2 ddy = dFdy(pos);\n vec2 uvDeriv = vec2(length(vec2(ddx.x, ddy.x)), length(vec2(ddx.y, ddy.y)));\n float epsilon = 1.0 / 255.0;\n\n // the level fading in, by cell size on screen; one fixed level without subdivisions\n float lod = uSubdivisions > 1.0 ?\n log(4.0 * uPixelRatio * max(uvDeriv.x, uvDeriv.y)) / log(uSubdivisions) :\n -1.0;\n float finest = ceil(lod);\n\n // coarsest level first; the finest fades in and the coarser turn major\n vec4 color = uMinorColor;\n float line = 0.0;\n for (int i = 2; i >= 0; i--) {\n float k = finest + float(i);\n float scale = pow(uSubdivisions, -k);\n float c = k - lod;\n float major = clamp(c - 1.0, 0.0, 1.0);\n float l = pristineGrid(pos * scale, uvDeriv * scale, mix(uThickness.y, uThickness.x, major), offAxis(pos * scale));\n l *= clamp(c, 0.0, 1.0);\n if (l > epsilon) {\n color = mix(uMinorColor, uMajorColor, major);\n line = l;\n break;\n }\n }\n\n // axis lines, from the coarsest level\n float topScale = pow(uSubdivisions, -(finest + 2.0));\n vec2 top = pos * topScale;\n vec2 topDeriv = uvDeriv * topScale;\n float lineU = abs(top.y) < 0.5 ? pristineGrid(top, topDeriv, uThickness.z, vec2(0.0, 1.0)) : 0.0;\n float lineV = abs(top.x) < 0.5 ? pristineGrid(top, topDeriv, uThickness.z, vec2(1.0, 0.0)) : 0.0;\n float axis = max(lineU, lineV) * uShowAxes;\n if (axis > epsilon && axis >= line) {\n color = lineU >= lineV ? uColorU : uColorV;\n line = axis;\n }\n #ifdef GRID_CONTRAST\n else {\n // black over light, white over dark\n vec3 under = texture2DLod(uSceneColorMap, (vClipPos.xy / vClipPos.w) * 0.5 + 0.5, 0.0).rgb;\n color.rgb = vec3(1.0 - smoothstep(0.4, 0.6, dot(under, vec3(0.2126, 0.7152, 0.0722))));\n }\n #endif\n\n // distance fade\n float fadeEnd = min(uFade.y, camera_params.y * 0.9);\n float fadeStart = min(uFade.x, fadeEnd);\n float f = clamp((length(vWorldPos - view_position) - fadeStart) / max(fadeEnd - fadeStart, 1e-5), 0.0, 1.0);\n\n // grazing angle fade\n float grazing = 1.0 - pow(1.0 - abs(dot(normalize(vToEye), normalize(vNormal))), 3.0);\n float coverage = line * (1.0 - f * f * (3.0 - 2.0 * f)) * grazing;\n float alpha = coverage * color.a;\n if (alpha <= epsilon) {\n discard;\n }\n\n #ifdef GRID_DEPTH\n // write depth where coverage beats the noise, or half\n #ifdef GRID_DEPTH_DITHER\n float threshold = texture2DLod(blueNoiseTex32, fract(gl_FragCoord.xy / 32.0), 0.0).y;\n #else\n float threshold = 0.5;\n #endif\n if (coverage < threshold) {\n discard;\n }\n gl_FragColor = vec4(0.0);\n #else\n gl_FragColor = vec4(gammaCorrectOutput(decodeGamma(color.rgb)), alpha);\n #endif\n }\n"; export const vertexWGSL: "\n attribute vertex_position: vec3f;\n\n uniform matrix_model: mat4x4f;\n uniform matrix_view: mat4x4f;\n uniform matrix_viewProjection: mat4x4f;\n uniform view_position: vec3f;\n uniform camera_params: vec4f;\n\n uniform uAxisU: vec3f;\n uniform uAxisV: vec3f;\n uniform uInfinite: f32;\n uniform uFade: vec2f;\n uniform uDepthOffset: f32;\n\n varying vGridPos: vec2f;\n varying vWorldPos: vec3f;\n varying vClipPos: vec4f;\n varying vToEye: vec3f;\n varying vNormal: vec3f;\n\n @vertex\n fn vertexMain(input: VertexInput) -> VertexOutput {\n var output: VertexOutput;\n\n // the node's scale sizes a fixed grid, never its cells\n let model = uniform.matrix_model;\n let origin = model[3].xyz;\n let axisU = (model * vec4f(uniform.uAxisU, 0.0)).xyz;\n let axisV = (model * vec4f(uniform.uAxisV, 0.0)).xyz;\n let u = normalize(axisU);\n let v = normalize(axisV);\n\n // an infinite grid follows the camera and spans the fade distance\n let reach = 2.0 * min(uniform.uFade.y, uniform.camera_params.y * 0.9);\n let center = vec2f(dot(uniform.view_position - origin, u), dot(uniform.view_position - origin, v)) * uniform.uInfinite;\n let gridPos = center + input.vertex_position.xz * mix(vec2f(length(axisU), length(axisV)), vec2f(reach), uniform.uInfinite);\n let worldPos = origin + u * gridPos.x + v * gridPos.y;\n output.vGridPos = gridPos;\n output.vWorldPos = worldPos;\n\n // offset along the view ray, so only depth changes\n let view = uniform.matrix_view;\n let back = vec3f(view[0][2], view[1][2], view[2][2]);\n var toEye = uniform.view_position - worldPos;\n if (uniform.camera_params.w > 0.5) {\n toEye = back * dot(toEye, back);\n }\n output.position = uniform.matrix_viewProjection * vec4f(worldPos + toEye * uniform.uDepthOffset, 1.0);\n output.vClipPos = output.position;\n output.vToEye = toEye;\n output.vNormal = cross(u, v);\n return output;\n }\n"; export const fragmentWGSL: "\n #include \"gammaPS\"\n\n uniform view_position: vec3f;\n uniform camera_params: vec4f;\n\n uniform uFade: vec2f;\n uniform uSpacing: f32;\n uniform uSubdivisions: f32;\n uniform uThickness: vec3f;\n uniform uPixelRatio: f32;\n uniform uShowAxes: f32;\n uniform uColorU: vec4f;\n uniform uColorV: vec4f;\n uniform uMajorColor: vec4f;\n uniform uMinorColor: vec4f;\n\n #ifdef GRID_CONTRAST\n var uSceneColorMap: texture_2d;\n var uSceneColorMapSampler: sampler;\n #endif\n\n #ifdef GRID_DEPTH_DITHER\n var blueNoiseTex32: texture_2d;\n var blueNoiseTex32Sampler: sampler;\n #endif\n\n varying vGridPos: vec2f;\n varying vWorldPos: vec3f;\n varying vClipPos: vec4f;\n varying vToEye: vec3f;\n varying vNormal: vec3f;\n\n // https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8#1e7c, with lines in pixels\n fn pristineGrid(uv: vec2f, uvDeriv: vec2f, pixels: f32, fade: vec2f) -> f32 {\n let targetWidth = min(pixels * uvDeriv, vec2f(min(pixels / (64.0 * uniform.uPixelRatio), 0.5)));\n let drawWidth = clamp(targetWidth, uvDeriv, vec2f(0.5));\n let lineAA = uvDeriv * 1.5;\n let gridUV = 1.0 - abs(fract(uv) * 2.0 - 1.0);\n var grid2 = smoothstep(drawWidth + lineAA, drawWidth - lineAA, gridUV);\n grid2 *= clamp(targetWidth / drawWidth, vec2f(0.0), vec2f(1.0));\n grid2 = mix(grid2, targetWidth, clamp(uvDeriv * 2.0 - 1.0, vec2f(0.0), vec2f(1.0)));\n grid2 *= fade;\n return mix(grid2.x, 1.0, grid2.y);\n }\n\n // drops the origin line when the axes draw it\n fn offAxis(uv: vec2f) -> vec2f {\n return select(vec2f(1.0), step(vec2f(0.5), abs(uv)), uniform.uShowAxes > 0.5);\n }\n\n @fragment\n fn fragmentMain(input: FragmentInput) -> FragmentOutput {\n var output: FragmentOutput;\n\n // derivatives need uniform control flow\n let pos = input.vGridPos / uniform.uSpacing;\n let ddx = dpdx(pos);\n let ddy = dpdy(pos);\n let uvDeriv = vec2f(length(vec2f(ddx.x, ddy.x)), length(vec2f(ddx.y, ddy.y)));\n let epsilon = 1.0 / 255.0;\n\n // the level fading in, by cell size on screen; one fixed level without subdivisions\n let lod = select(\n -1.0,\n log(4.0 * uniform.uPixelRatio * max(uvDeriv.x, uvDeriv.y)) / log(uniform.uSubdivisions),\n uniform.uSubdivisions > 1.0\n );\n let finest = ceil(lod);\n\n // coarsest level first; the finest fades in and the coarser turn major\n var color = uniform.uMinorColor;\n var line = 0.0;\n for (var i: i32 = 2; i >= 0; i--) {\n let k = finest + f32(i);\n let scale = pow(uniform.uSubdivisions, -k);\n let c = k - lod;\n let major = clamp(c - 1.0, 0.0, 1.0);\n let thickness = mix(uniform.uThickness.y, uniform.uThickness.x, major);\n let l = pristineGrid(pos * scale, uvDeriv * scale, thickness, offAxis(pos * scale)) * clamp(c, 0.0, 1.0);\n if (l > epsilon) {\n color = mix(uniform.uMinorColor, uniform.uMajorColor, major);\n line = l;\n break;\n }\n }\n\n // axis lines, from the coarsest level\n let topScale = pow(uniform.uSubdivisions, -(finest + 2.0));\n let top = pos * topScale;\n let topDeriv = uvDeriv * topScale;\n let lineU = select(0.0, pristineGrid(top, topDeriv, uniform.uThickness.z, vec2f(0.0, 1.0)), abs(top.y) < 0.5);\n let lineV = select(0.0, pristineGrid(top, topDeriv, uniform.uThickness.z, vec2f(1.0, 0.0)), abs(top.x) < 0.5);\n let axis = max(lineU, lineV) * uniform.uShowAxes;\n if (axis > epsilon && axis >= line) {\n color = select(uniform.uColorV, uniform.uColorU, lineU >= lineV);\n line = axis;\n }\n #ifdef GRID_CONTRAST\n else {\n // black over light, white over dark\n var uv = (input.vClipPos.xy / input.vClipPos.w) * 0.5 + vec2f(0.5);\n uv.y = 1.0 - uv.y;\n let under = textureSampleLevel(uSceneColorMap, uSceneColorMapSampler, uv, 0.0).rgb;\n color = vec4f(vec3f(1.0 - smoothstep(0.4, 0.6, dot(under, vec3f(0.2126, 0.7152, 0.0722)))), color.a);\n }\n #endif\n\n // distance fade\n let fadeEnd = min(uniform.uFade.y, uniform.camera_params.y * 0.9);\n let fadeStart = min(uniform.uFade.x, fadeEnd);\n let f = clamp((length(input.vWorldPos - uniform.view_position) - fadeStart) / max(fadeEnd - fadeStart, 1e-5), 0.0, 1.0);\n\n // grazing angle fade\n let grazing = 1.0 - pow(1.0 - abs(dot(normalize(input.vToEye), normalize(input.vNormal))), 3.0);\n let coverage = line * (1.0 - f * f * (3.0 - 2.0 * f)) * grazing;\n let alpha = coverage * color.a;\n if (alpha <= epsilon) {\n discard;\n }\n\n #ifdef GRID_DEPTH\n // write depth where coverage beats the noise, or half\n #ifdef GRID_DEPTH_DITHER\n let threshold = textureSampleLevel(blueNoiseTex32, blueNoiseTex32Sampler, fract(pcPosition.xy / 32.0), 0.0).y;\n #else\n let threshold = 0.5;\n #endif\n if (coverage < threshold) {\n discard;\n }\n output.color = vec4f(0.0);\n #else\n output.color = vec4f(gammaCorrectOutput(decodeGamma3(color.rgb)), alpha);\n #endif\n return output;\n }\n";