summaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-07-31 12:26:22 +1000
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-07-31 12:26:22 +1000
commitfa04ebc603a7bf742c64724ede23cfa010fb3c4c (patch)
tree59f000765e5528bda36194a91b21c187c9e988bd /assets
parentc2fec36353c29d31ed95451e42413983808fd62e (diff)
grid is not really working
Diffstat (limited to 'assets')
-rw-r--r--assets/shaders/grid.frag55
-rw-r--r--assets/shaders/grid.vert29
2 files changed, 81 insertions, 3 deletions
diff --git a/assets/shaders/grid.frag b/assets/shaders/grid.frag
index 06eebc3..6cf8a7c 100644
--- a/assets/shaders/grid.frag
+++ b/assets/shaders/grid.frag
@@ -2,6 +2,59 @@
out vec4 FragColor;
+in float near; //0.01
+in float far; //100
+in vec3 nearPoint;
+in vec3 farPoint;
+in mat4 fragView;
+in mat4 fragProj;
+
+// void main() {
+// // FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+// float t = -nearPoint.y / (farPoint.y - nearPoint.y);
+// if (t > 0) {
+// FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+// } else {
+// FragColor = vec4(0.0);
+// }
+// // FragColor = vec4(1.0, 0.0, 0.0, 1.0 * float(t > 0)); // opacity = 1 when t > 0, opacity = 0 otherwise
+// }
+
+vec4 grid(vec3 fragPos3D, float scale, bool drawAxis) {
+ vec2 coord = fragPos3D.xz * scale;
+ vec2 derivative = fwidth(coord);
+ vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative;
+ float line = min(grid.x, grid.y);
+ float minimumz = min(derivative.y, 1);
+ float minimumx = min(derivative.x, 1);
+ vec4 color = vec4(0.2, 0.2, 0.2, 1.0 - min(line, 1.0));
+ // z axis
+ if(fragPos3D.x > -0.1 * minimumx && fragPos3D.x < 0.1 * minimumx)
+ color.z = 1.0;
+ // x axis
+ if(fragPos3D.z > -0.1 * minimumz && fragPos3D.z < 0.1 * minimumz)
+ color.x = 1.0;
+ return color;
+}
+float computeDepth(vec3 pos) {
+ vec4 clip_space_pos = fragProj * fragView * vec4(pos.xyz, 1.0);
+ return (clip_space_pos.z / clip_space_pos.w);
+}
+float computeLinearDepth(vec3 pos) {
+ vec4 clip_space_pos = fragProj * fragView * vec4(pos.xyz, 1.0);
+ float clip_space_depth = (clip_space_pos.z / clip_space_pos.w) * 2.0 - 1.0; // put back between -1 and 1
+ float linearDepth = (2.0 * near * far) / (far + near - clip_space_depth * (far - near)); // get linear value between 0.01 and 100
+ return linearDepth / far; // normalize
+}
void main() {
- FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+ float t = -nearPoint.y / (farPoint.y - nearPoint.y);
+ vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint);
+
+ gl_FragDepth = computeDepth(fragPos3D);
+
+ float linearDepth = computeLinearDepth(fragPos3D);
+ float fading = max(0, (0.5 - linearDepth));
+
+ FragColor = (grid(fragPos3D, 10, true) + grid(fragPos3D, 1, true))* float(t > 0); // adding multiple resolution for the grid
+ FragColor.a *= fading;
} \ No newline at end of file
diff --git a/assets/shaders/grid.vert b/assets/shaders/grid.vert
index bdfa477..592cbfc 100644
--- a/assets/shaders/grid.vert
+++ b/assets/shaders/grid.vert
@@ -6,12 +6,37 @@ uniform Camera {
vec4 viewPos;
} cam;
+out vec3 nearPoint;
+out vec3 farPoint;
+out float near;
+out float far;
+out mat4 fragView;
+out mat4 fragProj;
+
// Grid position are in xy clipped space
vec3 gridPlane[6] = vec3[](
vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0)
);
-// normal vertice projection
+
+vec3 UnprojectPoint(float x, float y, float z, mat4 view, mat4 projection) {
+ mat4 viewInv = inverse(view);
+ mat4 projInv = inverse(projection);
+ vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0);
+ return unprojectedPoint.xyz / unprojectedPoint.w;
+}
+
+// normal vertex projection
void main() {
- gl_Position = cam.proj * cam.view * vec4(gridPlane[gl_VertexID].xyz, 1.0);
+ // gl_Position = cam.proj * cam.view * vec4(gridPlane[gl_VertexID].xyz, 1.0);
+ vec3 p = gridPlane[gl_VertexID].xyz;
+ nearPoint = UnprojectPoint(p.x, p.y, -1.0, cam.view, cam.proj).xyz; // unprojecting on the near plane
+ farPoint = UnprojectPoint(p.x, p.y, 1.0, cam.view, cam.proj).xyz; // unprojecting on the far plane
+
+ fragView = cam.view;
+ fragProj = cam.proj;
+ near = 0.01;
+ far = 100.0;
+
+ gl_Position = vec4(p, 1.0); // using directly the clipped coordinates
} \ No newline at end of file