From 726450bcbbfa110eaaf0eaecf5c4de23b4cdd9c8 Mon Sep 17 00:00:00 2001 From: "shensi.zxd" Date: Mon, 8 Nov 2021 12:33:28 +0800 Subject: [PATCH 1/2] fix: color space correction --- packages/core/src/shader/ShaderUniform.ts | 2 +- .../core/src/shaderlib/begin_mobile_frag.glsl | 21 +++++++++++++------ .../src/shaderlib/extra/blinn-phong.fs.glsl | 10 +++++++++ .../core/src/shaderlib/extra/unlit.fs.glsl | 19 ++++++++++++++++- .../src/shaderlib/pbr/ibl_frag_define.glsl | 2 +- packages/core/src/shaderlib/pbr/pbr_frag.glsl | 8 +++---- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/packages/core/src/shader/ShaderUniform.ts b/packages/core/src/shader/ShaderUniform.ts index e8b9448770..d696f9955a 100644 --- a/packages/core/src/shader/ShaderUniform.ts +++ b/packages/core/src/shader/ShaderUniform.ts @@ -120,7 +120,7 @@ export class ShaderUniform { Color.gammaToLinearSpace((value).r), Color.gammaToLinearSpace((value).g), Color.gammaToLinearSpace((value).b), - Color.gammaToLinearSpace((value).a) + (value).a ); } else { this._gl.uniform4f( diff --git a/packages/core/src/shaderlib/begin_mobile_frag.glsl b/packages/core/src/shaderlib/begin_mobile_frag.glsl index 795544d844..ba9742a5f9 100644 --- a/packages/core/src/shaderlib/begin_mobile_frag.glsl +++ b/packages/core/src/shaderlib/begin_mobile_frag.glsl @@ -6,14 +6,20 @@ #ifdef O3_EMISSIVE_TEXTURE - - emission *= texture2D(u_emissiveTexture, v_uv); + vec4 emissiveTextureColor = texture2D(u_emissiveTexture, v_uv); + #ifndef OASIS_COLORSPACE_GAMMA + emissiveTextureColor = gammaToLinear(emissiveTextureColor); + #endif + emission *= emissiveTextureColor; #endif #ifdef O3_DIFFUSE_TEXTURE - - diffuse *= texture2D(u_diffuseTexture, v_uv); + vec4 diffuseTextureColor = texture2D(u_diffuseTexture, v_uv); + #ifndef OASIS_COLORSPACE_GAMMA + diffuseTextureColor = gammaToLinear(diffuseTextureColor); + #endif + diffuse *= diffuseTextureColor; #endif @@ -24,8 +30,11 @@ #endif #ifdef O3_SPECULAR_TEXTURE - - specular *= texture2D(u_specularTexture, v_uv); + vec4 specularTextureColor = texture2D(u_specularTexture, v_uv); + #ifndef OASIS_COLORSPACE_GAMMA + specularTextureColor = gammaToLinear(specularTextureColor); + #endif + specular *= specularTextureColor; #endif diff --git a/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl b/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl index 9362a385db..295fba6466 100644 --- a/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl +++ b/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl @@ -12,6 +12,13 @@ #include #include +vec4 gammaToLinear(vec4 srgbIn){ + return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); +} + +vec4 linearToGamma(vec4 linearIn){ + return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); +} void main() { @@ -22,6 +29,9 @@ void main() { gl_FragColor = emission + ambient + diffuse + specular; gl_FragColor.a = diffuse.a; + #ifndef OASIS_COLORSPACE_GAMMA + gl_FragColor = linearToGamma(gl_FragColor); + #endif #include } diff --git a/packages/core/src/shaderlib/extra/unlit.fs.glsl b/packages/core/src/shaderlib/extra/unlit.fs.glsl index f108847dc7..88c42b61d5 100644 --- a/packages/core/src/shaderlib/extra/unlit.fs.glsl +++ b/packages/core/src/shaderlib/extra/unlit.fs.glsl @@ -8,11 +8,23 @@ uniform float u_alphaCutoff; uniform sampler2D u_baseTexture; #endif +vec4 gammaToLinear(vec4 srgbIn){ + return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); +} + +vec4 linearToGamma(vec4 linearIn){ + return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); +} + void main() { vec4 baseColor = u_baseColor; #ifdef O3_BASE_TEXTURE - baseColor *= texture2D(u_baseTexture, v_uv); + vec4 textureColor = texture2D(u_baseTexture, v_uv); + #ifndef OASIS_COLORSPACE_GAMMA + textureColor = gammaToLinear(textureColor); + #endif + baseColor *= textureColor; #endif #ifdef ALPHA_CUTOFF @@ -21,6 +33,11 @@ void main() { } #endif + + #ifndef OASIS_COLORSPACE_GAMMA + baseColor = linearToGamma(baseColor); + #endif + gl_FragColor = baseColor; #include diff --git a/packages/core/src/shaderlib/pbr/ibl_frag_define.glsl b/packages/core/src/shaderlib/pbr/ibl_frag_define.glsl index 5b5b822679..bdc757aa98 100644 --- a/packages/core/src/shaderlib/pbr/ibl_frag_define.glsl +++ b/packages/core/src/shaderlib/pbr/ibl_frag_define.glsl @@ -62,7 +62,7 @@ vec3 getLightProbeRadiance(GeometricContext geometry, float roughness, int maxMI envMapColor.rgb = RGBMToLinear(envMapColor, 5.0).rgb; #ifdef OASIS_COLORSPACE_GAMMA - envMapColor = linearTogamma(envMapColor); + envMapColor = linearToGamma(envMapColor); #endif return envMapColor.rgb * specularIntensity; diff --git a/packages/core/src/shaderlib/pbr/pbr_frag.glsl b/packages/core/src/shaderlib/pbr/pbr_frag.glsl index d9cb3ce77d..5391c69dc8 100644 --- a/packages/core/src/shaderlib/pbr/pbr_frag.glsl +++ b/packages/core/src/shaderlib/pbr/pbr_frag.glsl @@ -10,7 +10,7 @@ addTotalDirectRadiance(geometry, material, reflectedLight); #ifdef O3_USE_SH vec3 irradiance = getLightProbeIrradiance(u_env_sh, geometry.normal); #ifdef OASIS_COLORSPACE_GAMMA - irradiance = linearTogamma(irradiance); + irradiance = linearToGamma(vec4(irradiance, 1.0)).rgb; #endif irradiance *= u_envMapLight.diffuseIntensity; #else @@ -50,8 +50,8 @@ vec3 totalRadiance = reflectedLight.directDiffuse + reflectedLight.indirectSpecular + emissiveRadiance; -vec4 tagetColor =vec4(totalRadiance, u_baseColor.a); +vec4 targetColor =vec4(totalRadiance, u_baseColor.a); #ifndef OASIS_COLORSPACE_GAMMA - tagetColor = linearToGamma (tagetColor); + targetColor = linearToGamma(targetColor); #endif -gl_FragColor = tagetColor; +gl_FragColor = targetColor; From 753491a5c5b0099a2a38201d0559ce64257dc131 Mon Sep 17 00:00:00 2001 From: "shensi.zxd" Date: Mon, 8 Nov 2021 16:19:46 +0800 Subject: [PATCH 2/2] feat: split encode/decode glsl --- packages/core/src/shaderlib/common.glsl | 14 +++++++++++++- .../core/src/shaderlib/extra/blinn-phong.fs.glsl | 8 -------- packages/core/src/shaderlib/extra/unlit.fs.glsl | 9 +-------- packages/core/src/shaderlib/pbr/pbr_helper.glsl | 12 ------------ 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/packages/core/src/shaderlib/common.glsl b/packages/core/src/shaderlib/common.glsl index 50e05a012b..026ff80b34 100644 --- a/packages/core/src/shaderlib/common.glsl +++ b/packages/core/src/shaderlib/common.glsl @@ -4,4 +4,16 @@ #define LOG2 1.442695 #define saturate( a ) clamp( a, 0.0, 1.0 ) -#define whiteCompliment(a) ( 1.0 - saturate( a ) ) \ No newline at end of file +#define whiteCompliment(a) ( 1.0 - saturate( a ) ) + +vec4 RGBMToLinear(vec4 value, float maxRange ) { + return vec4( value.rgb * value.a * maxRange, 1.0 ); +} + +vec4 gammaToLinear(vec4 srgbIn){ + return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); +} + +vec4 linearToGamma(vec4 linearIn){ + return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); +} \ No newline at end of file diff --git a/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl b/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl index 295fba6466..4c52c888d2 100644 --- a/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl +++ b/packages/core/src/shaderlib/extra/blinn-phong.fs.glsl @@ -12,14 +12,6 @@ #include #include -vec4 gammaToLinear(vec4 srgbIn){ - return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); -} - -vec4 linearToGamma(vec4 linearIn){ - return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); -} - void main() { #include diff --git a/packages/core/src/shaderlib/extra/unlit.fs.glsl b/packages/core/src/shaderlib/extra/unlit.fs.glsl index 88c42b61d5..4bb203a846 100644 --- a/packages/core/src/shaderlib/extra/unlit.fs.glsl +++ b/packages/core/src/shaderlib/extra/unlit.fs.glsl @@ -1,3 +1,4 @@ +#include #include #include @@ -8,14 +9,6 @@ uniform float u_alphaCutoff; uniform sampler2D u_baseTexture; #endif -vec4 gammaToLinear(vec4 srgbIn){ - return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); -} - -vec4 linearToGamma(vec4 linearIn){ - return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); -} - void main() { vec4 baseColor = u_baseColor; diff --git a/packages/core/src/shaderlib/pbr/pbr_helper.glsl b/packages/core/src/shaderlib/pbr/pbr_helper.glsl index 06ebe88a04..accb425dae 100644 --- a/packages/core/src/shaderlib/pbr/pbr_helper.glsl +++ b/packages/core/src/shaderlib/pbr/pbr_helper.glsl @@ -5,18 +5,6 @@ float pow2(float x ) { return x * x; } -vec4 RGBMToLinear(vec4 value, float maxRange ) { - return vec4( value.rgb * value.a * maxRange, 1.0 ); -} - -vec4 gammaToLinear(vec4 srgbIn){ - return vec4( pow(srgbIn.rgb, vec3(2.2)), srgbIn.a); -} - -vec4 linearToGamma(vec4 linearIn){ - return vec4( pow(linearIn.rgb, vec3(1.0 / 2.2)), linearIn.a); -} - vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor ) { return RECIPROCAL_PI * diffuseColor; }