Skip to content

Commit

Permalink
fix: color space correction (galacean#566)
Browse files Browse the repository at this point in the history
* fix: color space correction
  • Loading branch information
zhuxudong authored Nov 8, 2021
1 parent 8307168 commit 56f6de6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
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
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;

0 comments on commit 56f6de6

Please sign in to comment.