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 1 commit
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
10 changes: 10 additions & 0 deletions packages/core/src/shaderlib/extra/blinn-phong.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
#include <fog_share>
#include <normal_get>

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() {

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

}
19 changes: 18 additions & 1 deletion packages/core/src/shaderlib/extra/unlit.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,23 @@ uniform float u_alphaCutoff;
uniform sampler2D u_baseTexture;
#endif

vec4 gammaToLinear(vec4 srgbIn){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write a public Oasis.glsl, put the basic fun like gammaToLinear()linearToGamma() and 、decodeHDRTexture() in it.

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
Expand All @@ -21,6 +33,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;