From cb9f33b6f014bdd3741923a6ec0a993606974e28 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Thu, 27 May 2021 20:21:22 +0200 Subject: [PATCH] Add grid shader --- openVulkanoCpp/Shader/grid.frag | 70 +++++++++++++++++++++++++++++++++ openVulkanoCpp/Shader/grid.vert | 30 ++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 openVulkanoCpp/Shader/grid.frag create mode 100644 openVulkanoCpp/Shader/grid.vert diff --git a/openVulkanoCpp/Shader/grid.frag b/openVulkanoCpp/Shader/grid.frag new file mode 100644 index 0000000..5dddbfb --- /dev/null +++ b/openVulkanoCpp/Shader/grid.frag @@ -0,0 +1,70 @@ +#version 450 + +layout(location = 0) in vec3 nearPoint; +layout(location = 1) in vec3 farPoint; +layout(location = 0) out vec4 outColor; + +layout(std140, push_constant) uniform CameraData +{ + mat4 viewProjection; + mat4 view; + mat4 projection; + vec4 camPos; + float near; + float far; +} cam; + +vec4 grid(vec3 fragPos3D, float scale) +{ + 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 visible = 1.0 - min(line, 1); + vec4 color = vec4(0.2, 0.2, 0.2, visible); + // z axis + float minimumx = 2 * min(derivative.x, 1); + if(fragPos3D.x > -0.1 * minimumx && fragPos3D.x < 0.1 * minimumx) + { + color.xy *= 0.4; + color.z = 1.0; + color.a *= 2; + } + // x axis + float minimumz = 2 * min(derivative.y, 1); + if(fragPos3D.z > -0.1 * minimumz && fragPos3D.z < 0.1 * minimumz) + { + color.x = 1.0; + color.yz *= 0.4; + color.a *= 2; + } + return color; +} + +float computeDepth(vec3 pos) +{ + vec4 clip_space_pos = cam.viewProjection * vec4(pos.xyz, 1.0); + return (clip_space_pos.z / clip_space_pos.w); +} + +float computeLinearDepth(vec3 pos) +{ + vec4 clip_space_pos = cam.viewProjection * 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 * cam.near * cam.far) / (cam.far + cam.near - clip_space_depth * (cam.far - cam.near)); // get linear value between 0.01 and 100 + return linearDepth / cam.far; // normalize +} + +void main() +{ + 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)); + + outColor = (grid(fragPos3D, 1) + grid(fragPos3D, 4) / 2.0) * float(t > 0); // adding multiple resolution for the grid + outColor.a *= fading; +} diff --git a/openVulkanoCpp/Shader/grid.vert b/openVulkanoCpp/Shader/grid.vert new file mode 100644 index 0000000..998e589 --- /dev/null +++ b/openVulkanoCpp/Shader/grid.vert @@ -0,0 +1,30 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(std140, push_constant) uniform CameraData +{ + mat4 viewProjection; + mat4 view; + mat4 projection; + vec4 camPos; +} cam; + +layout(location = 0) out vec3 nearPoint; +layout(location = 1) out vec3 farPoint; + +// Grid position are in clipped space +vec3 gridPlane[4] = vec3[] ( + vec3(1, 1, 0), vec3(-1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0) +); + +vec3 UnprojectPoint(float x, float y, float z, mat4 view, mat4 projection) { + vec4 unprojectedPoint = inverse(cam.viewProjection) * vec4(x, y, z, 1.0); + return unprojectedPoint.xyz / unprojectedPoint.w; +} + +void main() { + vec3 p = gridPlane[gl_VertexIndex].xyz; + nearPoint = UnprojectPoint(p.x, p.y, 0.0, cam.view, cam.projection).xyz; // unprojecting on the near plane + farPoint = UnprojectPoint(p.x, p.y, 1.0, cam.view, cam.projection).xyz; // unprojecting on the far plane + gl_Position = vec4(p, 1.0); // using directly the clipped coordinates +}