Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: color space correction #566

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/shader/ShaderUniform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class ShaderUniform {
Color.gammaToLinearSpace((<Color>value).r),
Color.gammaToLinearSpace((<Color>value).g),
Color.gammaToLinearSpace((<Color>value).b),
Color.gammaToLinearSpace((<Color>value).a)
(<Color>value).a
);
} else {
this._gl.uniform4f(
Expand Down
21 changes: 15 additions & 6 deletions packages/core/src/shaderlib/begin_mobile_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/shaderlib/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@
#define LOG2 1.442695

#define saturate( a ) clamp( a, 0.0, 1.0 )
#define whiteCompliment(a) ( 1.0 - saturate( a ) )
#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);
}
4 changes: 3 additions & 1 deletion packages/core/src/shaderlib/extra/blinn-phong.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <fog_share>
#include <normal_get>


void main() {

#include <begin_mobile_frag>
Expand All @@ -22,6 +21,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 <fog_frag>

}
12 changes: 11 additions & 1 deletion packages/core/src/shaderlib/extra/unlit.fs.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <common>
#include <uv_share>
#include <fog_share>

Expand All @@ -12,7 +13,11 @@ 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
Expand All @@ -21,6 +26,11 @@ void main() {
}
#endif


#ifndef OASIS_COLORSPACE_GAMMA
baseColor = linearToGamma(baseColor);
#endif

gl_FragColor = baseColor;

#include <fog_frag>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shaderlib/pbr/ibl_frag_define.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/shaderlib/pbr/pbr_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
12 changes: 0 additions & 12 deletions packages/core/src/shaderlib/pbr/pbr_helper.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down