-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
include "pipelines/common.glsl" | ||
|
||
------------------ | ||
|
||
vertex_shader [[ | ||
layout(std140, binding = 4) uniform Model { | ||
mat4 u_model; | ||
}; | ||
|
||
out vec4 v_lpos; | ||
|
||
void main() { | ||
vec3 local_pos = vec3(gl_VertexID & 1, 0, gl_VertexID >> 1) * 1000 - 500; | ||
local_pos.y = 0; | ||
v_lpos = vec4(local_pos, 1); | ||
vec4 p = vec4(local_pos - Global.camera_world_pos.xyz, 1); | ||
gl_Position = Global.projection_no_jitter * Global.view * p; | ||
} | ||
]] | ||
|
||
--------------------- | ||
|
||
fragment_shader [[ | ||
layout(location = 0) out vec4 o_color; | ||
|
||
// https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8 | ||
// https://gist.github.com/bgolus/d49651f52b1dcf82f70421ba922ed064 | ||
float PristineGrid(vec2 uv, vec2 lineWidth) { | ||
vec4 uvDDXY = vec4(dFdx(uv), dFdy(uv)); | ||
vec2 uvDeriv = vec2(length(uvDDXY.xz), length(uvDDXY.yw)); | ||
bvec2 invertLine = lessThan(lineWidth, vec2(0.5)); | ||
vec2 targetWidth = mix(1.0 - lineWidth, lineWidth, invertLine); | ||
vec2 drawWidth = clamp(targetWidth, uvDeriv, vec2(0.5)); | ||
vec2 lineAA = uvDeriv * 1.5; | ||
vec2 gridUV = abs(fract(uv) * 2.0 - 1.0); | ||
gridUV = mix(gridUV, 1.0 - gridUV, invertLine); | ||
vec2 grid2 = smoothstep(drawWidth + lineAA, drawWidth - lineAA, gridUV); | ||
grid2 *= saturate(targetWidth / drawWidth); | ||
grid2 = mix(grid2, targetWidth, saturate(uvDeriv * 2.0 - 1.0)); | ||
grid2 = mix(1.0 - grid2, grid2, invertLine); | ||
return mix(grid2.x, 1.0, grid2.y); | ||
} | ||
|
||
in vec4 v_lpos; | ||
|
||
void main() { | ||
vec2 uv = v_lpos.xz / v_lpos.w; | ||
float grid = PristineGrid(uv, vec2(0.005)); | ||
if (grid < 0.0) discard; | ||
o_color = vec4(vec3(0.0), grid); | ||
} | ||
]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters