mirror of
https://github.com/barkeser2002/flower.git
synced 2026-09-25 14:16:14 +03:00
164 lines
4.2 KiB
GLSL
164 lines
4.2 KiB
GLSL
#version 460
|
|
|
|
#extension GL_EXT_nonuniform_qualifier : enable
|
|
#extension GL_GOOGLE_include_directive : enable
|
|
#extension GL_EXT_samplerless_texture_functions : enable
|
|
|
|
#include "common/shared_struct.glsl"
|
|
#include "common/shared_functions.glsl"
|
|
#include "common/shared_shading_model.glsl"
|
|
|
|
vec3 kNdcPoints[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)
|
|
);
|
|
|
|
layout (set = 0, binding = 0) uniform UniformFrameData { PerFrameData frameData; };
|
|
layout (set = 0, binding = 1) uniform texture2D inDepth;
|
|
|
|
#define SHARED_SAMPLER_SET 1
|
|
#include "common/shared_sampler.glsl"
|
|
|
|
#ifdef VERTEX_SHADER ///////////// vertex shader start
|
|
|
|
layout(location = 0) out vec3 nearPoint;
|
|
layout(location = 1) out vec3 farPoint;
|
|
|
|
vec3 deprojectNDC2World(vec2 pos, float z)
|
|
{
|
|
vec4 worldH = frameData.camInvertViewProjNoJitter * vec4(pos, z, 1.0);
|
|
return worldH.xyz / worldH.w;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
nearPoint = deprojectNDC2World(kNdcPoints[gl_VertexIndex].xy, 1.0);
|
|
farPoint = deprojectNDC2World(kNdcPoints[gl_VertexIndex].xy, 0.0);
|
|
|
|
gl_Position = vec4(kNdcPoints[gl_VertexIndex].xyz, 1.0);
|
|
}
|
|
|
|
#endif /////////////////////////// vertex shader end
|
|
|
|
#ifdef PIXEL_SHADER ////////////// pixel shader start
|
|
|
|
layout(location = 0) in vec3 nearPoint;
|
|
layout(location = 1) in vec3 farPoint;
|
|
|
|
layout(location = 0) out vec4 outColor;
|
|
|
|
vec4 grid(vec3 fragPos3D)
|
|
{
|
|
float gray0 = 0.30;
|
|
float scale0 = 1.0;
|
|
|
|
float gray1 = 0.60;
|
|
float scale1 = 0.1;
|
|
|
|
vec2 coord0 = fragPos3D.xz * scale0;
|
|
vec2 derivative0 = fwidth(coord0);
|
|
vec2 grid0 = abs(fract(coord0 - 0.5) - 0.5) / fwidth(coord0 * 2.0);
|
|
float line0 = min(grid0.x, grid0.y);
|
|
vec4 color = vec4(gray0, gray0, gray0, 1.0 - min(line0, 1.0));
|
|
|
|
vec2 coord1 = fragPos3D.xz * scale1;
|
|
vec2 grid1 = abs(fract(coord1 - 0.5) - 0.5) / fwidth(coord1 * 2.0);
|
|
float line1 = min(grid1.x, grid1.y);
|
|
float a1 = 1.0 - min(line1, 1.0);
|
|
if(a1 > 0.0f)
|
|
{
|
|
color = mix(color, vec4(gray1,gray1,gray1, a1), a1);
|
|
}
|
|
|
|
///////////////////
|
|
vec2 coord = fragPos3D.xz;
|
|
bool xDraw = onRange(coord.x, -0.5f, 0.5f);
|
|
bool yDraw = onRange(coord.y, -0.5f, 0.5f);
|
|
if(xDraw || yDraw)
|
|
{
|
|
vec2 grid = abs(fract(coord - 0.5) - 0.5) / fwidth(coord * 2.0);
|
|
vec2 a = vec2(1.0) - min(grid, vec2(1.0));
|
|
|
|
if(xDraw)
|
|
{
|
|
color.xyz = mix(color.xyz, vec3(0.93f, 0.22f, 0.31f), a.x);
|
|
}
|
|
if(yDraw)
|
|
{
|
|
color.xyz = mix(color.xyz, vec3(0.184f, 0.51f, 0.87f), a.y);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
vec2 derivative = fwidth(coord * 2.0);
|
|
float minimumz = min(derivative.y, 1);
|
|
float minimumx = min(derivative.x, 1);
|
|
|
|
float lineColorWidth = 0.4;
|
|
|
|
// z axis
|
|
bool bZAxis = false;
|
|
if(fragPos3D.x > -lineColorWidth * minimumx && fragPos3D.x < lineColorWidth * minimumx)
|
|
{
|
|
color.xyz = vec3(0.0);
|
|
bZAxis = true;
|
|
color.xyz += vec3(0.184f, 0.51f, 0.87f);
|
|
color.a = 1.0;
|
|
}
|
|
// x axis
|
|
if(fragPos3D.z > -lineColorWidth * minimumz && fragPos3D.z < lineColorWidth * minimumz)
|
|
{
|
|
if(bZAxis)
|
|
{
|
|
color.xyz += vec3(0.93f, 0.22f, 0.31f);
|
|
}
|
|
else
|
|
{
|
|
color.xyz = vec3(0.93f, 0.22f, 0.31f);
|
|
}
|
|
|
|
color.a = 1.0;
|
|
}
|
|
|
|
return color;
|
|
}
|
|
|
|
float computeDepth(vec3 pos)
|
|
{
|
|
vec4 posH = frameData.camViewProjNoJitter * vec4(pos.xyz, 1.0);
|
|
return posH.z / posH.w;
|
|
}
|
|
|
|
vec4 getColor(vec3 fragPos3D, float t)
|
|
{
|
|
float deviceZ = computeDepth(fragPos3D);
|
|
|
|
vec2 uv = vec2(gl_FragCoord.xy) / vec2(frameData.displayWidth, frameData.displayHeight);
|
|
|
|
float sceneZ = texture(sampler2D(inDepth, pointClampEdgeSampler), uv).r;
|
|
|
|
float linearDepth = linearizeDepth(deviceZ, frameData);
|
|
float fading = exp2(-linearDepth * 0.05);
|
|
|
|
vec4 result = grid(fragPos3D) * float(t > 0);
|
|
result.a = (deviceZ > sceneZ) ? result.a : 0.0;
|
|
result.a *= fading * 0.75;
|
|
|
|
return result;
|
|
}
|
|
|
|
void main()
|
|
{
|
|
float t = -nearPoint.y / (farPoint.y - nearPoint.y);
|
|
vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint);
|
|
outColor = getColor(fragPos3D, t);
|
|
}
|
|
|
|
#endif //////////////////////////// pixel shader end |