From 39df8374458613e95c157c095e1ccea421636ebb Mon Sep 17 00:00:00 2001 From: Scott Nagy Date: Thu, 19 Oct 2017 09:57:26 -0700 Subject: [PATCH] Revert "Properly handle sRGB and gamma correct output" This reverts commit 80f3df922c61e1f0d5800d7aa7142ab135bc5d5c. --- main.js | 6 ++++-- mesh.js | 12 ++++-------- scene.js | 2 +- shaders/pbr-frag.glsl | 29 +++++++---------------------- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/main.js b/main.js index a0a7d9a5..1a4024ca 100644 --- a/main.js +++ b/main.js @@ -48,7 +48,7 @@ function loadCubeMap(gl, envMap, type, state) { gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); // todo: should this be srgb? or rgba? what's the HDR scale on this? - gl.texImage2D(face, j, state.hasSRGBExt ? state.hasSRGBExt.SRGB_EXT : gl.RGBA, state.hasSRGBExt ? state.hasSRGBExt.SRGB_EXT : gl.RGBA, gl.UNSIGNED_BYTE, image); + gl.texImage2D(face, j, state.sRGBifAvailable, state.sRGBifAvailable, gl.UNSIGNED_BYTE, image); }; } @@ -142,6 +142,8 @@ function init(vertSource, fragSource) { var ctx2d = canvas2d.getContext("2d"); + var hasSRGBExt = gl.getExtension('EXT_SRGB'); + glState = { uniforms: {}, attributes: {}, @@ -150,7 +152,7 @@ function init(vertSource, fragSource) { scene: null, hasLODExtension:gl.getExtension('EXT_shader_texture_lod'), hasDerivativesExtension:gl.getExtension('OES_standard_derivatives'), - hasSRGBExt:gl.getExtension('EXT_SRGB') + sRGBifAvailable: (hasSRGBExt ? hasSRGBExt.SRGB_EXT : gl.RGBA) }; var projectionMatrix = mat4.create(); diff --git a/mesh.js b/mesh.js index f4d1c622..40e4e66a 100644 --- a/mesh.js +++ b/mesh.js @@ -19,11 +19,7 @@ class Mesh { this.vertSource = globalState.vertSource; this.fragSource = globalState.fragSource; - this.hasSRGBExt = globalState.hasSRGBExt; - - if(!this.hasSRGBExt) { - this.defines.MANUAL_SRGB = 1; - } + this.sRGBifAvailable = globalState.sRGBifAvailable; ++scene.pendingBuffers; @@ -230,7 +226,7 @@ class Mesh { vals: baseColorFactor }; if (pbrMat && pbrMat.baseColorTexture && gltf.textures.length > pbrMat.baseColorTexture.index) { - imageInfos['baseColor'] = this.getImageInfo(gl, gltf, pbrMat.baseColorTexture.index, 'uniform1i', 'u_BaseColorSampler', this.hasSRGBExt ? this.hasSRGBExt.SRGB_EXT : gl.RGBA); + imageInfos['baseColor'] = this.getImageInfo(gl, gltf, pbrMat.baseColorTexture.index, 'uniform1i', 'u_BaseColorSampler', this.sRGBifAvailable); this.defines.HAS_BASECOLORMAP = 1; } else if (this.localState.uniforms['u_BaseColorSampler']) { @@ -266,12 +262,12 @@ class Mesh { // brdfLUT var brdfLUT = 'textures/brdfLUT.png'; samplerIndex = this.scene.getNextSamplerIndex(); - imageInfos['brdfLUT'] = { 'uri': brdfLUT, 'samplerIndex': samplerIndex, 'colorSpace': this.hasSRGBExt ? this.hasSRGBExt.SRGB_EXT : gl.RGBA, 'clamp': true }; + imageInfos['brdfLUT'] = { 'uri': brdfLUT, 'samplerIndex': samplerIndex, 'colorSpace': gl.RGBA, 'clamp': true }; this.localState.uniforms['u_brdfLUT'] = { 'funcName': 'uniform1i', 'vals': [samplerIndex] }; // Emissive if (this.material && this.material.emissiveTexture) { - imageInfos['emissive'] = this.getImageInfo(gl, gltf, this.material.emissiveTexture.index, 'uniform1i', 'u_EmissiveSampler', this.hasSRGBExt ? this.hasSRGBExt.SRGB_EXT : gl.RGBA); + imageInfos['emissive'] = this.getImageInfo(gl, gltf, this.material.emissiveTexture.index, 'uniform1i', 'u_EmissiveSampler', this.sRGBifAvailable); this.defines.HAS_EMISSIVEMAP = 1; var emissiveFactor = defined(this.material.emissiveFactor) ? this.material.emissiveFactor : [0.0, 0.0, 0.0]; this.localState.uniforms['u_EmissiveFactor'] = { diff --git a/scene.js b/scene.js index 4f8f5fce..6fcfba9a 100644 --- a/scene.js +++ b/scene.js @@ -96,7 +96,7 @@ class Scene { gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false); - gl.texImage2D(gl.TEXTURE_2D, 0, imageInfo.colorSpace, imageInfo.colorSpace, gl.UNSIGNED_BYTE, image); + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,/*imageInfo.colorSpace, imageInfo.colorSpace,*/ gl.UNSIGNED_BYTE, image); scene.pendingTextures--; scene.drawScene(gl); diff --git a/shaders/pbr-frag.glsl b/shaders/pbr-frag.glsl index 9c124e73..08e6ca7a 100644 --- a/shaders/pbr-frag.glsl +++ b/shaders/pbr-frag.glsl @@ -88,21 +88,6 @@ struct PBRInfo const float M_PI = 3.141592653589793; const float c_MinRoughness = 0.04; -vec4 SRGBtoLINEAR(vec4 srgbIn) -{ - #ifdef MANUAL_SRGB - #ifdef SRGB_FAST_APPROXIMATION - vec3 linOut = pow(srgbIn.xyz,vec3(2.2)); - #else //SRGB_FAST_APPROXIMATION - vec3 bLess = step(vec3(0.03928),srgbIn.xyz); - vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess ); - #endif //SRGB_FAST_APPROXIMATION - return vec4(linOut,srgbIn.w);; - #else //MANUAL_SRGB - return srgbIn; - #endif //MANUAL_SRGB -} - // Find the normal for this fragment, pulling either from a predefined normal map // or from the interpolated mesh normal and tangent attributes. vec3 getNormal() @@ -146,13 +131,13 @@ vec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection) float mipCount = 9.0; // resolution of 512x512 float lod = (pbrInputs.perceptualRoughness * mipCount); // retrieve a scale and bias to F0. See [1], Figure 3 - vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb; - vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb; + vec3 brdf = texture2D(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness)).rgb; + vec3 diffuseLight = textureCube(u_DiffuseEnvSampler, n).rgb; #ifdef USE_TEX_LOD - vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb; + vec3 specularLight = textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod).rgb; #else - vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb; + vec3 specularLight = textureCube(u_SpecularEnvSampler, reflection).rgb; #endif vec3 diffuse = diffuseLight * pbrInputs.diffuseColor; @@ -227,7 +212,7 @@ void main() // The albedo may be defined from a base texture or a flat color #ifdef HAS_BASECOLORMAP - vec4 baseColor = SRGBtoLINEAR(texture2D(u_BaseColorSampler, v_UV)) * u_BaseColorFactor; + vec4 baseColor = texture2D(u_BaseColorSampler, v_UV) * u_BaseColorFactor; #else vec4 baseColor = u_BaseColorFactor; #endif @@ -295,7 +280,7 @@ void main() #endif #ifdef HAS_EMISSIVEMAP - vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, v_UV)).rgb * u_EmissiveFactor; + vec3 emissive = texture2D(u_EmissiveSampler, v_UV).rgb * u_EmissiveFactor; color += emissive; #endif @@ -311,5 +296,5 @@ void main() color = mix(color, vec3(metallic), u_ScaleDiffBaseMR.z); color = mix(color, vec3(perceptualRoughness), u_ScaleDiffBaseMR.w); - gl_FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a); + gl_FragColor = vec4(color, baseColor.a); }