Skip to content

Commit

Permalink
VideoTexture: Restore inline sRGB decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Aug 1, 2023
1 parent 5c619ba commit 808e38b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/renderers/shaders/ShaderChunk/map_fragment.glsl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
export default /* glsl */`
#ifdef USE_MAP
diffuseColor *= texture2D( map, vMapUv );
vec4 sampledDiffuseColor = texture2D( map, vMapUv );
#ifdef DECODE_VIDEO_TEXTURE
// use inline sRGB decode until browsers properly support SRGB8_APLHA8 with video textures
sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w );
#endif
diffuseColor *= sampledDiffuseColor;
#endif
`;
8 changes: 8 additions & 0 deletions src/renderers/shaders/ShaderLib/background.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ void main() {
vec4 texColor = texture2D( t2D, vUv );
#ifdef DECODE_VIDEO_TEXTURE
// use inline sRGB decode until browsers properly support SRGB8_APLHA8 with video textures
texColor = vec4( mix( pow( texColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), texColor.rgb * 0.0773993808, vec3( lessThanEqual( texColor.rgb, vec3( 0.04045 ) ) ) ), texColor.w );
#endif
texColor.rgb *= backgroundIntensity;
gl_FragColor = texColor;
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,8 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) {

parameters.useLegacyLights ? '#define LEGACY_LIGHTS' : '',

parameters.decodeVideoTexture ? '#define DECODE_VIDEO_TEXTURE' : '',

parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '',
( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '',

Expand Down
6 changes: 5 additions & 1 deletion src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BackSide, DoubleSide, CubeUVReflectionMapping, ObjectSpaceNormalMap, TangentSpaceNormalMap, NoToneMapping, NormalBlending, LinearSRGBColorSpace } from '../../constants.js';
import { BackSide, DoubleSide, CubeUVReflectionMapping, ObjectSpaceNormalMap, TangentSpaceNormalMap, NoToneMapping, NormalBlending, LinearSRGBColorSpace, SRGBColorSpace } from '../../constants.js';
import { Layers } from '../../core/Layers.js';
import { WebGLProgram } from './WebGLProgram.js';
import { WebGLShaderCache } from './WebGLShaderCache.js';
Expand Down Expand Up @@ -335,6 +335,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
toneMapping: toneMapping,
useLegacyLights: renderer._useLegacyLights,

decodeVideoTexture: HAS_MAP && ( material.map.isVideoTexture === true ) && ( material.map.colorSpace === SRGBColorSpace ),

premultipliedAlpha: material.premultipliedAlpha,

doubleSided: material.side === DoubleSide,
Expand Down Expand Up @@ -536,6 +538,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
_programLayers.enable( 17 );
if ( parameters.pointsUvs )
_programLayers.enable( 18 );
if ( parameters.decodeVideoTexture )
_programLayers.enable( 19 );

array.push( _programLayers.mask );

Expand Down
4 changes: 2 additions & 2 deletions src/renderers/webgl/WebGLTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
glFormat = utils.convert( texture.format, texture.colorSpace );

let glType = utils.convert( texture.type ),
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace );
glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );

setTextureParameters( textureType, texture, supportsMips );

Expand Down Expand Up @@ -2027,7 +2027,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
const format = texture.format;
const type = texture.type;

if ( texture.isCompressedTexture === true || texture.format === _SRGBAFormat ) return image;
if ( texture.isCompressedTexture === true || texture.isVideoTexture === true || texture.format === _SRGBAFormat ) return image;

if ( colorSpace !== LinearSRGBColorSpace && colorSpace !== NoColorSpace ) {

Expand Down

0 comments on commit 808e38b

Please sign in to comment.