diff --git a/assets/shaders/deferred.frag b/assets/shaders/deferred.frag index 1f52344..ae1d282 100644 --- a/assets/shaders/deferred.frag +++ b/assets/shaders/deferred.frag @@ -32,17 +32,26 @@ layout(location = 0) out vec4 outColor; void main() { - vec3 position = texture(positionTexture, inUV).rgb; - vec3 normal = texture(normalTexture, inUV).rgb; - vec3 albedo = texture(albedoTexture, inUV).rgb; + vec4 gposition = texture(positionTexture, inUV); + vec4 gnormal = texture(normalTexture, inUV); + vec4 galbedo = texture(albedoTexture, inUV); + vec4 gspec = texture(specularTexture, inUV); - vec3 spec = texture(specularTexture, inUV).rgb; - float metallicFactor = spec.b; - float roughnessFactor = spec.g; - float ao = spec.r; + vec3 position = gposition.xyz; + vec3 normal = gnormal.xyz; + vec3 albedo = galbedo.xyz; + vec3 spec = gspec.xyz; + + float metallicFactor = gspec.w; + float roughnessFactor = galbedo.w; + float ao = gnormal.w; vec3 V = normalize(cameraPos - position); + vec3 F0 = vec3(0.04); + F0 = mix(F0, albedo, metallicFactor); + float reflectance = max(max(F0.x,F0.y),F0.z); + vec3 Lo = vec3(0); for(int i = 0; i < PushConstants.lightCount; ++i) { @@ -54,9 +63,6 @@ void main() float attenuation = 1.0 / (distance * distance); vec3 radiance = l.color.xyz * attenuation * l.intensity; - vec3 F0 = vec3(0.04); - F0 = mix(F0, albedo, metallicFactor); - vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); float NDF = DistributionGGX(normal, H, roughnessFactor); float G = GeometrySmith(normal, V, L, roughnessFactor); @@ -70,14 +76,18 @@ void main() kD *= 1.0 - metallicFactor; float NdotL = max(dot(normal, L), 0.0); - Lo += (kD * albedo / PI + specular) * radiance * NdotL; + Lo += clamp((kD * albedo / PI + specular) * radiance * NdotL, vec3(0), vec3(1)); } - vec3 ambient = vec3(0.2) * albedo * ao; + vec3 ambient = vec3(0.2) * albedo; - vec3 color = ambient + Lo; - // color += vec3(); - color = color / (color + vec3(1.0)); + vec3 color = ambient + Lo; + //color = color + spec * reflectance; + color = mix(color, spec, reflectance); + + //color = color / (color + vec3(1.0)); + + //outColor = vec4(color, 1.0); outColor = vec4(color, 1.0); } diff --git a/assets/shaders/mrt.frag b/assets/shaders/mrt.frag index 17ba776..bcd5ae1 100755 --- a/assets/shaders/mrt.frag +++ b/assets/shaders/mrt.frag @@ -43,6 +43,8 @@ layout(set = 0, binding = 1) uniform LightBuffer { layout(align = 16) Light lights[]; } LightsBuffers; +layout (set = 0, binding = 2) uniform samplerCube skybox; + layout(set = 2, binding = 3) buffer MaterialBuffer { layout(align = 16) Material materials[]; } MaterialBuffers; @@ -52,11 +54,12 @@ layout(set = 2, binding = 4) uniform sampler2D texSampler[]; layout(location = 0) in vec2 uv; layout(location = 1) smooth in mat3 TBN; layout(location = 4) in vec4 outpos; +layout(location = 5) in vec3 cameraPos; layout (location = 0) out vec4 gPosition; -layout (location = 1) out vec3 gNormal; -layout (location = 2) out vec3 gAlbedo; -layout (location = 3) out vec3 gSpecular; +layout (location = 1) out vec4 gNormal; +layout (location = 2) out vec4 gAlbedo; +layout (location = 3) out vec4 gSpecular; float near = 0.001; float far = 50.0; @@ -95,12 +98,19 @@ void main() { outSpecular.r = 1; } else { outSpecular = LinearToSRGB(texture(texSampler[material.metallicRoughnessTexture], uv)).xyz; + + gNormal.w = outSpecular.x; + gAlbedo.w = outSpecular.y; + gSpecular.w = outSpecular.z; } - float depth = LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration + vec3 I = normalize(outpos.xyz - cameraPos); + vec3 R = reflect(I, normalize(outNormal)); + //R = R * -1; + outSpecular = texture(skybox, R).rgb; gPosition = outpos; - gNormal = outNormal; - gAlbedo = outColor; - gSpecular = outSpecular; + gNormal.xyz = outNormal; + gAlbedo.xyz = outColor; + gSpecular.xyz = outSpecular; } diff --git a/assets/shaders/mrt.vert b/assets/shaders/mrt.vert index dc96acb..36a06f6 100755 --- a/assets/shaders/mrt.vert +++ b/assets/shaders/mrt.vert @@ -64,6 +64,7 @@ layout(location = 0) out vec2 uv; layout(location = 1) smooth out mat3 TBN; layout(location = 4) out vec4 outpos; +layout(location = 5) out vec3 cameraPos; void main() { @@ -83,4 +84,6 @@ void main() gl_Position = Camera.projection * Camera.view * PushConstants.render_matrix * vec4(vertex.position.xyz, 1.0); outpos = PushConstants.render_matrix * vec4(vertex.position.xyz, 1.0); + + cameraPos = inverse(Camera.view)[3].xyz; }