#version 300 es precision highp float; in vec3 fragPosition; in vec2 fragTexCoord; in vec4 fragColor; in vec3 fragNormal; uniform vec4 colDiffuse; uniform float specularity; uniform float glossiness; uniform float camClipNear; uniform float camClipFar; layout (location = 0) out vec4 gbufferColor; layout (location = 1) out vec4 gbufferNormal; float Grid(in vec2 uv, in float lineWidth) { vec4 uvDDXY = vec4(dFdx(uv), dFdy(uv)); vec2 uvDeriv = vec2(length(uvDDXY.xz), length(uvDDXY.yw)); float targetWidth = lineWidth > 0.5 ? 1.0 - lineWidth : lineWidth; vec2 drawWidth = clamp( vec2(targetWidth, targetWidth), uvDeriv, vec2(0.5, 0.5)); vec2 lineAA = uvDeriv * 1.5; vec2 gridUV = abs(fract(uv) * 2.0 - 1.0); gridUV = lineWidth > 0.5 ? gridUV : 1.0 - gridUV; vec2 g2 = smoothstep(drawWidth + lineAA, drawWidth - lineAA, gridUV); g2 *= clamp(targetWidth / drawWidth, 0.0, 1.0); g2 = mix(g2, vec2(targetWidth, targetWidth), clamp(uvDeriv * 2.0 - 1.0, 0.0, 1.0)); g2 = lineWidth > 0.5 ? 1.0 - g2 : g2; return mix(g2.x, 1.0, g2.y); } float Checker(in vec2 uv) { vec4 uvDDXY = vec4(dFdx(uv), dFdy(uv)); vec2 w = vec2(length(uvDDXY.xz), length(uvDDXY.yw)); vec2 i = 2.0*(abs(fract((uv-0.5*w)*0.5)-0.5)- abs(fract((uv+0.5*w)*0.5)-0.5))/w; return 0.5 - 0.5*i.x*i.y; } vec3 FromGamma(in vec3 col) { return vec3(pow(col.x, 1.0/2.2), pow(col.y, 1.0/2.2), pow(col.z, 1.0/2.2)); } float LinearDepth(float depth, float near, float far) { return (2.0 * near) / (far + near - depth * (far - near)); } void main() { float gridFine = Grid(20.0 * 10.0 * fragTexCoord, 0.025); float gridCoarse = Grid(2.0 * 10.0 * fragTexCoord, 0.02); float check = Checker(2.0 * 10.0 * fragTexCoord); vec3 albedo = FromGamma(fragColor.xyz * colDiffuse.xyz) * mix(mix(mix(0.9, 0.95, check), 0.85, gridFine), 1.0, gridCoarse); float spec = specularity * mix(mix(0.5, 0.75, check), 1.0, gridCoarse); gbufferColor = vec4(albedo, spec); gbufferNormal = vec4(fragNormal * 0.5f + 0.5f, glossiness / 100.0f); gl_FragDepth = LinearDepth(gl_FragCoord.z, camClipNear, camClipFar); }