77 NoToneMapping ,
88 NoBlending ,
99 RGBAFormat ,
10+ UnsignedByteType ,
11+ sRGBEncoding ,
1012 HalfFloatType
1113} from '../constants.js' ;
1214
@@ -40,6 +42,11 @@ const TOTAL_LODS = LOD_MAX - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length;
4042// samples and exit early, but not recompile the shader.
4143const MAX_SAMPLES = 20 ;
4244
45+ const ENCODINGS = {
46+ [ LinearEncoding ] : 0 ,
47+ [ sRGBEncoding ] : 1
48+ } ;
49+
4350const _flatCamera = /*@__PURE__ */ new OrthographicCamera ( ) ;
4451const { _lodPlanes, _sizeLods, _sigmas } = /*@__PURE__ */ _createPlanes ( ) ;
4552const _clearColor = /*@__PURE__ */ new Color ( ) ;
@@ -335,6 +342,20 @@ class PMREMGenerator {
335342
336343 }
337344
345+ _setEncoding ( uniform , texture ) {
346+
347+ if ( this . _renderer . capabilities . isWebGL2 === true && texture . format === RGBAFormat && texture . type === UnsignedByteType && texture . encoding === sRGBEncoding ) {
348+
349+ uniform . value = ENCODINGS [ LinearEncoding ] ;
350+
351+ } else {
352+
353+ uniform . value = ENCODINGS [ texture . encoding ] ;
354+
355+ }
356+
357+ }
358+
338359 _textureToCubeUV ( texture , cubeUVRenderTarget ) {
339360
340361 const renderer = this . _renderer ;
@@ -374,6 +395,8 @@ class PMREMGenerator {
374395
375396 }
376397
398+ this . _setEncoding ( uniforms [ 'inputEncoding' ] , texture ) ;
399+
377400 _setViewport ( cubeUVRenderTarget , 0 , 0 , 3 * SIZE_MAX , 2 * SIZE_MAX ) ;
378401
379402 renderer . setRenderTarget ( cubeUVRenderTarget ) ;
@@ -648,6 +671,8 @@ function _getBlurShader( maxSamples ) {
648671 uniform float mipInt;
649672 uniform vec3 poleAxis;
650673
674+ ${ _getEncodings ( ) }
675+
651676 #define ENVMAP_TYPE_CUBE_UV
652677 #include <cube_uv_reflection_fragment>
653678
@@ -714,7 +739,8 @@ function _getEquirectShader() {
714739
715740 uniforms : {
716741 'envMap' : { value : null } ,
717- 'texelSize' : { value : texelSize }
742+ 'texelSize' : { value : texelSize } ,
743+ 'inputEncoding' : { value : ENCODINGS [ LinearEncoding ] }
718744 } ,
719745
720746 vertexShader : _getCommonVertexShader ( ) ,
@@ -729,6 +755,8 @@ function _getEquirectShader() {
729755 uniform sampler2D envMap;
730756 uniform vec2 texelSize;
731757
758+ ${ _getEncodings ( ) }
759+
732760 #include <common>
733761
734762 void main() {
@@ -740,13 +768,13 @@ function _getEquirectShader() {
740768
741769 vec2 f = fract( uv / texelSize - 0.5 );
742770 uv -= f * texelSize;
743- vec3 tl = texture2D ( envMap, uv ).rgb;
771+ vec3 tl = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb;
744772 uv.x += texelSize.x;
745- vec3 tr = texture2D ( envMap, uv ).rgb;
773+ vec3 tr = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb;
746774 uv.y += texelSize.y;
747- vec3 br = texture2D ( envMap, uv ).rgb;
775+ vec3 br = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb;
748776 uv.x -= texelSize.x;
749- vec3 bl = texture2D ( envMap, uv ).rgb;
777+ vec3 bl = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb;
750778
751779 vec3 tm = mix( tl, tr, f.x );
752780 vec3 bm = mix( bl, br, f.x );
@@ -773,7 +801,8 @@ function _getCubemapShader() {
773801
774802 uniforms : {
775803 'envMap' : { value : null } ,
776- 'flipEnvMap' : { value : - 1 }
804+ 'flipEnvMap' : { value : - 1 } ,
805+ 'inputEncoding' : { value : ENCODINGS [ LinearEncoding ] }
777806 } ,
778807
779808 vertexShader : _getCommonVertexShader ( ) ,
@@ -789,9 +818,11 @@ function _getCubemapShader() {
789818
790819 uniform samplerCube envMap;
791820
821+ ${ _getEncodings ( ) }
822+
792823 void main() {
793824
794- gl_FragColor = textureCube( envMap, vec3( flipEnvMap * vOutputDirection.x, vOutputDirection.yz ) );
825+ gl_FragColor = envMapTexelToLinear( textureCube( envMap, vec3( flipEnvMap * vOutputDirection.x, vOutputDirection.yz ) ) );
795826
796827 }
797828 ` ,
@@ -869,4 +900,35 @@ function _getCommonVertexShader() {
869900
870901}
871902
903+ function _getEncodings ( ) {
904+
905+ return /* glsl */ `
906+
907+ uniform int inputEncoding;
908+
909+ #include <encodings_pars_fragment>
910+
911+ vec4 inputTexelToLinear( vec4 value ) {
912+
913+ if ( inputEncoding == 0 ) {
914+
915+ return value;
916+
917+ } else {
918+
919+ return sRGBToLinear( value );
920+
921+ }
922+
923+ }
924+
925+ vec4 envMapTexelToLinear( vec4 color ) {
926+
927+ return inputTexelToLinear( color );
928+
929+ }
930+ ` ;
931+
932+ }
933+
872934export { PMREMGenerator } ;
0 commit comments