From 5c1c28c53696da21b89b476288e9f416ab638516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Garc=C3=ADa=20Li=C3=B1=C3=A1n?= Date: Sat, 31 Jul 2021 12:59:16 +0200 Subject: [PATCH] HDR: Improve PBR Effect --- Effects/model-pbr.eff | 72 +++++++++++++++++++++--------- Shaders/HDR/gbuffer-include.frag | 12 ++++- Shaders/HDR/geometry-combined.vert | 5 +-- Shaders/HDR/geometry-pbr.frag | 25 +++++++---- Shaders/HDR/geometry-pbr.vert | 5 +-- 5 files changed, 83 insertions(+), 36 deletions(-) diff --git a/Effects/model-pbr.eff b/Effects/model-pbr.eff index ba984ded7..d97e806e4 100644 --- a/Effects/model-pbr.eff +++ b/Effects/model-pbr.eff @@ -3,41 +3,39 @@ Effects/model-pbr - + white + 1.0 1.0 1.0 1.0 Aircraft/Generic/Effects/null_bumpspec.png 2d linear-mipmap-linear - clamp - clamp + repeat + repeat normalized - + - Aircraft/Generic/Effects/ReflectMaps/null_reflectdirt.png - 2d - linear-mipmap-linear - clamp - clamp - normalized + white + 1.0 - Aircraft/Generic/Effects/greymap.png - 2d - linear-mipmap-linear - clamp - clamp - normalized + white - + 1.0 + white + + + white + + 0.0 0.0 0.0 @@ -93,6 +91,15 @@ texture[4]/wrap-t texture[4]/internal-format + + 5 + texture[5]/type + texture[5]/image + texture[5]/filter + texture[5]/wrap-s + texture[5]/wrap-t + texture[5]/internal-format + back Shaders/HDR/geometry-pbr.vert @@ -108,7 +115,7 @@ - albedo_tex + base_color_tex sampler-2d 0 @@ -118,7 +125,7 @@ 1 - metalness_tex + metallic_tex sampler-2d 2 @@ -128,10 +135,35 @@ 3 - cavity_tex + occlusion_tex sampler-2d 4 + + emissive_tex + sampler-2d + 5 + + + base_color_factor + float-vec4 + base-color-factor + + + metallic_factor + float + metallic-factor + + + roughness_factor + float + roughness-factor + + + emissive_factor + float-vec3 + emissive-factor + diff --git a/Shaders/HDR/gbuffer-include.frag b/Shaders/HDR/gbuffer-include.frag index 4753a1ff1..77f1085ae 100644 --- a/Shaders/HDR/gbuffer-include.frag +++ b/Shaders/HDR/gbuffer-include.frag @@ -16,10 +16,10 @@ vec3 decodeNormal(vec2 enc) { vec2 fenc = enc * 4.0 - 2.0; float f = dot(fenc, fenc); - float g = sqrt(1.0 - f / 4.0); + float g = sqrt(1.0 - f * 0.25); vec3 n; n.xy = fenc * g; - n.z = 1.0 - f / 2.0; + n.z = 1.0 - f * 0.5; return n; } @@ -38,3 +38,11 @@ float linearizeDepth(float depth) return (2.0 * fg_NearFar.x) / ( fg_NearFar.y + fg_NearFar.x - depth * (fg_NearFar.y - fg_NearFar.x)); } + +vec3 decodeSRGB(vec3 screenRGB) +{ + vec3 a = screenRGB / 12.92; + vec3 b = pow((screenRGB + 0.055) / 1.055, vec3(2.4)); + vec3 c = step(vec3(0.04045), screenRGB); + return mix(a, b, c); +} diff --git a/Shaders/HDR/geometry-combined.vert b/Shaders/HDR/geometry-combined.vert index c3cdc6555..f1ad780f5 100644 --- a/Shaders/HDR/geometry-combined.vert +++ b/Shaders/HDR/geometry-combined.vert @@ -3,6 +3,8 @@ layout(location = 0) in vec4 pos; layout(location = 1) in vec3 normal; layout(location = 3) in vec4 multiTexCoord0; +layout(location = 6) in vec3 tangent; +layout(location = 7) in vec3 binormal; out vec2 texCoord; out mat3 TBN; @@ -10,9 +12,6 @@ out mat3 TBN; uniform mat4 osg_ModelViewProjectionMatrix; uniform mat3 osg_NormalMatrix; -attribute vec3 tangent; -attribute vec3 binormal; - void main() { gl_Position = osg_ModelViewProjectionMatrix * pos; diff --git a/Shaders/HDR/geometry-pbr.frag b/Shaders/HDR/geometry-pbr.frag index ca5b10190..c00d4a589 100644 --- a/Shaders/HDR/geometry-pbr.frag +++ b/Shaders/HDR/geometry-pbr.frag @@ -7,25 +7,34 @@ layout(location = 2) out vec4 gbuffer2; in vec2 texCoord; in mat3 TBN; -uniform sampler2D albedo_tex; +uniform sampler2D base_color_tex; uniform sampler2D normal_tex; -uniform sampler2D metalness_tex; +uniform sampler2D metallic_tex; uniform sampler2D roughness_tex; -uniform sampler2D cavity_tex; +uniform sampler2D occlusion_tex; +uniform sampler2D emissive_tex; +uniform vec4 base_color_factor; +uniform float metallic_factor; +uniform float roughness_factor; +uniform vec3 emissive_factor; vec2 encodeNormal(vec3 n); +vec3 decodeSRGB(vec3 screenRGB); void main() { - gbuffer0.rgb = pow(texture(albedo_tex, texCoord).rgb, vec3(2.2)); // Gamma correction - gbuffer0.a = texture(cavity_tex, texCoord).r; + vec4 base_color_texel = texture(base_color_tex, texCoord); + gbuffer0.rgb = decodeSRGB(base_color_texel.rgb); // Ignore alpha + float occlusion = texture(occlusion_tex, texCoord).r; + gbuffer0.a = occlusion; vec3 normal = texture(normal_tex, texCoord).rgb * 2.0 - 1.0; normal = normalize(TBN * normal); gbuffer1 = encodeNormal(normal); - float metalness = texture(metalness_tex, texCoord).r; - float roughness = texture(roughness_tex, texCoord).r; + float metallic = texture(metallic_tex, texCoord).r * metallic_factor; + float roughness = texture(roughness_tex, texCoord).r * roughness_factor; + gbuffer2 = vec4(metallic, roughness, 0.0, 0.0); - gbuffer2 = vec4(metalness, roughness, 0.0, 0.0); + vec3 emissive = texture(emissive_tex, texCoord).rgb * emissive_factor; } diff --git a/Shaders/HDR/geometry-pbr.vert b/Shaders/HDR/geometry-pbr.vert index c3cdc6555..f1ad780f5 100644 --- a/Shaders/HDR/geometry-pbr.vert +++ b/Shaders/HDR/geometry-pbr.vert @@ -3,6 +3,8 @@ layout(location = 0) in vec4 pos; layout(location = 1) in vec3 normal; layout(location = 3) in vec4 multiTexCoord0; +layout(location = 6) in vec3 tangent; +layout(location = 7) in vec3 binormal; out vec2 texCoord; out mat3 TBN; @@ -10,9 +12,6 @@ out mat3 TBN; uniform mat4 osg_ModelViewProjectionMatrix; uniform mat3 osg_NormalMatrix; -attribute vec3 tangent; -attribute vec3 binormal; - void main() { gl_Position = osg_ModelViewProjectionMatrix * pos;