From 60a8cca957f01ffd0998547f49b03d839a85f429 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Thu, 17 Oct 2019 18:06:48 -0400 Subject: [PATCH] Improve bfres shaders --- File_Format_Library/GL/BFRES_Render.cs | 9 ++++++--- Toolbox/Shader/Bfres/BFRES.frag | 6 +++--- Toolbox/Shader/Bfres/BFRES_PBR.frag | 18 ++++++++---------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/File_Format_Library/GL/BFRES_Render.cs b/File_Format_Library/GL/BFRES_Render.cs index 0c611ba1..017c3848 100644 --- a/File_Format_Library/GL/BFRES_Render.cs +++ b/File_Format_Library/GL/BFRES_Render.cs @@ -367,6 +367,12 @@ namespace FirstPlugin sphereMatrix = invertedCamera; sphereMatrix.Transpose(); + invertedCamera = mvpMat.Inverted(); + Vector3 lightDirection = new Vector3(0f, 0f, -1f); + + shader.SetVector3("specLightDirection", Vector3.TransformNormal(lightDirection, invertedCamera).Normalized()); + shader.SetVector3("difLightDirection", Vector3.TransformNormal(lightDirection, invertedCamera).Normalized()); + shader.SetMatrix4x4("sphereMatrix", ref sphereMatrix); shader.SetMatrix4x4("mtxCam", ref computedCamMtx); @@ -380,9 +386,6 @@ namespace FirstPlugin shader.SetVector3("difLightColor", new Vector3(1)); shader.SetVector3("ambLightColor", new Vector3(1)); - - Vector3 lightDirection = new Vector3(0f, 0f, -1f); - GL.Enable(EnableCap.AlphaTest); GL.AlphaFunc(AlphaFunction.Gequal, 0.1f); diff --git a/Toolbox/Shader/Bfres/BFRES.frag b/Toolbox/Shader/Bfres/BFRES.frag index f2c0eda0..aa7b7b21 100644 --- a/Toolbox/Shader/Bfres/BFRES.frag +++ b/Toolbox/Shader/Bfres/BFRES.frag @@ -38,6 +38,7 @@ uniform mat4 mtxMdl; uniform vec3 cameraPosition; // Viewport Settings +uniform vec3 difLightDirection; uniform int uvChannel; uniform int renderType; uniform int useNormalMap; @@ -187,8 +188,7 @@ void main() vec3 I = vec3(0,0,-1) * mat3(mtxCam); vec3 V = normalize(I); // view vec3 R = reflect(-I, N); // reflection - - float light = max(dot(N, V), 0.0); + float halfLambert = dot(difLightDirection, N) * 0.5 + 0.5; // Light Map vec4 LightMapColor = texture(BakeLightMap, f_texcoord2); @@ -220,7 +220,7 @@ void main() alpha *= 0.5; } - albedo *= light; + albedo *= halfLambert; vec3 LightingDiffuse = vec3(0); if (HasLightMap == 1) diff --git a/Toolbox/Shader/Bfres/BFRES_PBR.frag b/Toolbox/Shader/Bfres/BFRES_PBR.frag index 612bc1db..7f4eac1d 100644 --- a/Toolbox/Shader/Bfres/BFRES_PBR.frag +++ b/Toolbox/Shader/Bfres/BFRES_PBR.frag @@ -35,6 +35,8 @@ uniform vec3 light1Pos; const float levels = 3.0; // Viewport Settings +uniform vec3 specLightDirection; + uniform int uvChannel; uniform int renderType; uniform int useNormalMap; @@ -306,17 +308,14 @@ void main() N = CalcBumpedNormal(normal, NormalMap, vert, 0); vec3 V = normalize(I); // view - vec3 L = normalize(objectPosition - I); // Light - vec3 H = normalize(objectPosition + I); // half angle - vec3 R = reflect(-I, N); // reflection + vec3 L = normalize(specLightDirection ); // Light + vec3 H = normalize(specLightDirection + I); // half angle + vec3 R = reflect(I, N); // reflection vec3 f0 = mix(vec3(0.04), albedo, metallic); // dialectric - vec3 kS = FresnelSchlickRoughness(max(dot(N, V), 0.0), f0, roughness); + vec3 kS = FresnelSchlickRoughness(max(dot(N, H), 0.0), f0, roughness); - vec3 kD = 1.0 - kS; - kD *= 1.0 - metallic; - BakedData ShadowBake = ShadowMapBaked(BakeShadowMap,BakeLightMap, f_texcoord1, f_texcoord2, int(bake_shadow_type),int(bake_light_type), int(bake_calc_type), N ); vec3 LightingDiffuse = vec3(0); @@ -332,7 +331,6 @@ void main() // Diffuse pass vec3 diffuseIblColor = texture(irradianceMap, N).rgb; vec3 diffuseTerm = diffuseIblColor * albedo; - diffuseTerm *= kD; diffuseTerm *= cavity; diffuseTerm *= ao; diffuseTerm *= shadow; @@ -350,7 +348,8 @@ void main() vec2 envBRDF = texture(brdfLUT, vec2(max(dot(N, V), 0.0), roughness)).rg; vec3 brdfTerm = (kS * envBRDF.x + envBRDF.y); // vec3 specularTerm = specularIblColor * (kS * brdfTerm.x + brdfTerm.y) * specIntensity; - vec3 specularTerm = specularIblColor * brdfTerm * specIntensity; + // vec3 specularTerm = specularIblColor * brdfTerm * specIntensity; + vec3 specularTerm = specularIblColor * kS; // Add render passes. @@ -386,7 +385,6 @@ void main() { diffuseIblColor = texture(irradianceMap, N).rgb; diffuseTerm = diffuseIblColor * vec3(0.5); - diffuseTerm *= kD; diffuseTerm *= cavity; diffuseTerm *= ao; diffuseTerm *= shadow;