diff --git a/assets/shaders/_builtins/PBR.glsl b/assets/shaders/_builtins/PBR.glsl index 7edcb6d..9693405 100644 --- a/assets/shaders/_builtins/PBR.glsl +++ b/assets/shaders/_builtins/PBR.glsl @@ -37,4 +37,9 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0) return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); } +vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness) +{ + return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0); +} + #endif diff --git a/assets/shaders/deferred.frag b/assets/shaders/deferred.frag index ae1d282..6772896 100644 --- a/assets/shaders/deferred.frag +++ b/assets/shaders/deferred.frag @@ -41,16 +41,16 @@ void main() vec3 normal = gnormal.xyz; vec3 albedo = galbedo.xyz; vec3 spec = gspec.xyz; + float specFactor = gspec.w; - float metallicFactor = gspec.w; + float metallicFactor = gposition.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 F0 = mix(vec3(0.04), albedo, metallicFactor); + vec3 F = fresnelSchlickRoughness(max(dot(normal, V), 0.0), F0, roughnessFactor); vec3 Lo = vec3(0); for(int i = 0; i < PushConstants.lightCount; ++i) @@ -63,12 +63,12 @@ void main() float attenuation = 1.0 / (distance * distance); vec3 radiance = l.color.xyz * attenuation * l.intensity; - vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); float NDF = DistributionGGX(normal, H, roughnessFactor); float G = GeometrySmith(normal, V, L, roughnessFactor); vec3 numerator = NDF * G * F; float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, L), 0.0); + vec3 specular = numerator / max(denominator, 0.001); vec3 kS = F; @@ -83,11 +83,13 @@ void main() vec3 color = ambient + Lo; - //color = color + spec * reflectance; - color = mix(color, spec, reflectance); + color += spec * specFactor; + // color = mix(color, spec, specFactor); //color = color / (color + vec3(1.0)); + // float reflectance = max(max(F.x, F.y), F.z); + // float reflectance = max(max(F0.x, F0.y), F0.z); - //outColor = vec4(color, 1.0); outColor = vec4(color, 1.0); + // outColor = vec4(vec3(reflectance), 1.0); } diff --git a/assets/shaders/fxaa.frag b/assets/shaders/fxaa.frag index c1ee4f1..6b2a934 100644 --- a/assets/shaders/fxaa.frag +++ b/assets/shaders/fxaa.frag @@ -1,13 +1,69 @@ #version 450 -layout(set = 0, binding = 0) uniform sampler2D ppIN; +layout(set = 0, binding = 0) uniform sampler2D ppIn; -layout (location = 0) in vec2 outUV; +// layout (location = 0) in vec2 uv; layout(location = 0) out vec4 outColor; +const vec2 resolution = vec2(1920, 1080); + +#define FXAA_SUBPIX_SHIFT (1.0/4.0) +#define FXAA_SPAN_MAX 32.0 +#define FXAA_REDUCE_MUL (1.0/FXAA_SPAN_MAX) +#define FXAA_REDUCE_MIN (1.0/256.0) + void main() { - vec3 color = texture(ppIN, outUV).rgb; - outColor = vec4(color, 1.0); + vec2 rcpFrame = 1./resolution.xy; + vec2 uv2 = gl_FragCoord.xy / resolution.xy; + + vec4 uv = vec4(uv2, uv2 - (rcpFrame * (0.5 + FXAA_SUBPIX_SHIFT))); + + vec3 luma = vec3(0.299, 0.587, 0.114); + + vec3 rgbNW = texture(ppIn, uv.zw).xyz; + vec3 rgbNE = texture(ppIn, uv.zw + vec2(1,0)*rcpFrame.xy).xyz; + vec3 rgbSW = texture(ppIn, uv.zw + vec2(0,1)*rcpFrame.xy).xyz; + vec3 rgbSE = texture(ppIn, uv.zw + vec2(1,1)*rcpFrame.xy).xyz; + vec3 rgbM = texture(ppIn, uv.xy).xyz; + + float lumaNW = dot(rgbNW, luma); + float lumaNE = dot(rgbNE, luma); + float lumaSW = dot(rgbSW, luma); + float lumaSE = dot(rgbSE, luma); + float lumaM = dot(rgbM, luma); + + float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); + float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); + + vec2 dir; + dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); + dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); + + float dirReduce = max( + (lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), + FXAA_REDUCE_MIN); + float rcpDirMin = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce); + + dir = min(vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX), + max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), + dir * rcpDirMin)) * rcpFrame.xy; + + vec3 rgbA = (1.0/2.0) * ( + texture(ppIn, uv.xy + dir * (1.0/3.0 - 0.5)).xyz + + texture(ppIn, uv.xy + dir * (2.0/3.0 - 0.5)).xyz); + vec3 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * ( + texture(ppIn, uv.xy + dir * (0.0/3.0 - 0.5)).xyz + + texture(ppIn, uv.xy + dir * (3.0/3.0 - 0.5)).xyz); + + float lumaB = dot(rgbB, luma); + + if((lumaB < lumaMin) || (lumaB > lumaMax)) + outColor = vec4(rgbA, 1.0); + else + outColor = vec4(rgbB, 1.0); + + if(uv.x < 0.5) + outColor = vec4(rgbM, 1.0); } diff --git a/assets/shaders/fxaa.vert b/assets/shaders/fxaa.vert index 56b6b6b..d36373d 100644 --- a/assets/shaders/fxaa.vert +++ b/assets/shaders/fxaa.vert @@ -1,9 +1,9 @@ #version 450 -layout (location = 0) out vec2 outUV; +/* layout (location = 0) out vec2 outUV; */ void main() { - outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + vec2 outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f); } diff --git a/assets/shaders/mrt.frag b/assets/shaders/mrt.frag index bcd5ae1..9d83e7d 100755 --- a/assets/shaders/mrt.frag +++ b/assets/shaders/mrt.frag @@ -61,15 +61,6 @@ 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; - -float LinearizeDepth(float depth) -{ - float z = depth * 2.0 - 1.0; // back to NDC - return (2.0 * near * far) / (far + near - z * (far - near)); -} - void main() { Material material = MaterialBuffers.materials[ PushConstants.materialBufferIndex ]; @@ -99,9 +90,17 @@ void main() { } else { outSpecular = LinearToSRGB(texture(texSampler[material.metallicRoughnessTexture], uv)).xyz; + float metallicFactor = outSpecular.z; + float roughnessFactor = outSpecular.y; + // vec3 F0 = mix(vec3(0.04), outColor, (metallicFactor - roughnessFactor)); + // float reflectance = max(max(F0.x, F0.y), F0.z); + // float reflectance = (metallicFactor + roughnessFactor) * 0.5; + float reflectance = 1.0 - roughnessFactor; + gNormal.w = outSpecular.x; - gAlbedo.w = outSpecular.y; - gSpecular.w = outSpecular.z; + gAlbedo.w = roughnessFactor; + gPosition.w = metallicFactor; + gSpecular.w = reflectance; } vec3 I = normalize(outpos.xyz - cameraPos); @@ -109,6 +108,7 @@ void main() { //R = R * -1; outSpecular = texture(skybox, R).rgb; + gPosition = outpos; gNormal.xyz = outNormal; gAlbedo.xyz = outColor; diff --git a/game.proj b/game.proj index e37a917..a916f14 100755 --- a/game.proj +++ b/game.proj @@ -45,5 +45,5 @@ "path": "scenes://CesiumMilkTruck.evsc" } ], - "activeScene": "CesiumMilkTruck" + "activeScene": "DamagedHelmetScene" } diff --git a/scenes/DamagedHelmet.evsc b/scenes/DamagedHelmet.evsc index 20d22c9..f864e6e 100644 --- a/scenes/DamagedHelmet.evsc +++ b/scenes/DamagedHelmet.evsc @@ -66,9 +66,9 @@ }, { "color": [1.0, 1.0, 1.0, 1.0], - "intensity": 200, + "intensity": 20, "type": "LightComponent" - }, + } ] }, { diff --git a/scenes/Sponza.evsc b/scenes/Sponza.evsc index afaba67..f773aae 100644 --- a/scenes/Sponza.evsc +++ b/scenes/Sponza.evsc @@ -3260,7 +3260,7 @@ }, { "color": [1.0, 1.0, 1.0, 1.0], - "intensity": 200, + "intensity": 10, "type": "LightComponent" } ], @@ -3338,7 +3338,7 @@ }, { "color": [1.0, 1.0, 1.0, 1.0], - "intensity": 2, + "intensity": 10, "type": "LightComponent" } ], diff --git a/scripts/MainScene/Camera.lua b/scripts/MainScene/Camera.lua index 7d643bc..f1e028f 100755 --- a/scripts/MainScene/Camera.lua +++ b/scripts/MainScene/Camera.lua @@ -1,5 +1,5 @@ this.on_init = function() - this.speed = 0.1 + this.speed = 0.01 this.angles = Vec3:new() this.mouse_sens = 0.01 end diff --git a/scripts/MainScene/objectRotation.lua b/scripts/MainScene/objectRotation.lua index 52489bd..dfb3065 100644 --- a/scripts/MainScene/objectRotation.lua +++ b/scripts/MainScene/objectRotation.lua @@ -5,5 +5,5 @@ end this.on_update = function() rotationSpeed = 0.001 this.angles.y = this.angles.y - rotationSpeed - this.eulerAngles = this.angles + -- this.eulerAngles = this.angles end