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

Nodes: Fix VideoTexture in WebGPUBackend & ColorSpaceNode revision #26261

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/files.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@
"webgpu_skinning",
"webgpu_skinning_instancing",
"webgpu_skinning_points",
"webgpu_sprites"
"webgpu_sprites",
"webgpu_video_panorama"
],
"webaudio": [
"webaudio_orientation",
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export { default as UserDataNode, userData } from './accessors/UserDataNode.js';
// display
export { default as BlendModeNode, burn, dodge, overlay, screen } from './display/BlendModeNode.js';
export { default as ColorAdjustmentNode, saturation, vibrance, hue, lumaCoeffs, luminance } from './display/ColorAdjustmentNode.js';
export { default as ColorSpaceNode, colorSpace } from './display/ColorSpaceNode.js';
export { default as ColorSpaceNode, linearToColorSpace, colorSpaceToLinear as sRGBToColorSpace, linearTosRGB, sRGBToLinear } from './display/ColorSpaceNode.js';
export { default as FrontFacingNode, frontFacing, faceDirection } from './display/FrontFacingNode.js';
export { default as NormalMapNode, normalMap, TBNViewMatrix } from './display/NormalMapNode.js';
export { default as PosterizeNode, posterize } from './display/PosterizeNode.js';
Expand Down
12 changes: 11 additions & 1 deletion examples/jsm/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import UniformNode from '../core/UniformNode.js';
import { uv } from './UVNode.js';
import { textureSize } from './TextureSizeNode.js';
import { colorSpaceToLinear } from '../display/ColorSpaceNode.js';
import { context } from '../core/ContextNode.js';
import { expression } from '../code/ExpressionNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, nodeProxy } from '../shadernode/ShaderNode.js';

Expand Down Expand Up @@ -136,7 +138,15 @@ class TextureNode extends UniformNode {

}

return builder.format( propertyName, nodeType, output );
let snippet = propertyName;

if ( builder.needsColorSpace( this.value ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should name this method something like needsForceLinear or needsConversionToLinear?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm.. Maybe needsColorConversionToLinear


snippet = colorSpaceToLinear( expression( snippet, nodeType ), this.value.colorSpace ).construct( builder ).build( builder, nodeType );

}

return builder.format( snippet, nodeType, output );

}

Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,12 @@ class NodeBuilder {

}

needsColorSpace( /*texture*/ ) {

return false;

}

/** @deprecated, r152 */
getTextureEncodingFromMap( map ) {

Expand Down
98 changes: 50 additions & 48 deletions examples/jsm/nodes/display/ColorSpaceNode.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import TempNode from '../core/TempNode.js';
import { mix } from '../math/MathNode.js';
import { addNodeClass } from '../core/Node.js';
import { addNodeElement, ShaderNode, nodeObject, vec4 } from '../shadernode/ShaderNode.js';
import { addNodeElement, ShaderNode, nodeObject, nodeProxy, vec4 } from '../shadernode/ShaderNode.js';

import { LinearEncoding, LinearSRGBColorSpace, sRGBEncoding, SRGBColorSpace } from 'three';
import { LinearSRGBColorSpace, SRGBColorSpace } from 'three';

export const LinearToLinear = new ShaderNode( ( inputs ) => {
const sRGBToLinearShader = new ShaderNode( ( inputs ) => {

return inputs.value;
const { value } = inputs;
const { rgb } = value;

const a = rgb.mul( 0.9478672986 ).add( 0.0521327014 ).pow( 2.4 );
const b = rgb.mul( 0.0773993808 );
const factor = rgb.lessThanEqual( 0.04045 );

const rgbResult = mix( a, b, factor );

return vec4( rgbResult, value.a );

} );

export const LinearTosRGB = new ShaderNode( ( inputs ) => {
const LinearTosRGBShader = new ShaderNode( ( inputs ) => {

const { value } = inputs;
const { rgb } = value;
Expand All @@ -26,82 +35,75 @@ export const LinearTosRGB = new ShaderNode( ( inputs ) => {

} );

const EncodingLib = {
LinearToLinear,
LinearTosRGB
};
const getColorSpaceMethod = ( colorSpace ) => {

class ColorSpaceNode extends TempNode {
let method = null;

constructor( method, node ) {
if ( colorSpace === LinearSRGBColorSpace ) {

super( 'vec4' );
method = 'Linear';

this.method = method;
} else if ( colorSpace === SRGBColorSpace ) {

this.node = node;
method = 'sRGB';

}

fromColorSpace( colorSpace ) {

let method = null;

if ( colorSpace === LinearSRGBColorSpace ) {

method = 'Linear';

} else if ( colorSpace === SRGBColorSpace ) {

method = 'sRGB';
return method;

}

this.method = 'LinearTo' + method;

return this;

}

fromEncoding( encoding ) { // @deprecated, r152

console.warn( 'THREE.ColorSpaceNode: Method .fromEncoding renamed to .fromColorSpace.' );
};

let method = null;
const getMethod = ( source, target ) => {

if ( encoding === LinearEncoding ) {
return getColorSpaceMethod( source ) + 'To' + getColorSpaceMethod( target );

method = 'Linear';
};

} else if ( encoding === sRGBEncoding ) {
class ColorSpaceNode extends TempNode {

method = 'sRGB';
constructor( method, node ) {

}
super( 'vec4' );

this.method = 'LinearTo' + method;
this.method = method;

return this;
this.node = node;

}

construct() {

const { method, node } = this;

return EncodingLib[ method ].call( { value: node } );
if ( method === ColorSpaceNode.LINEAR_TO_LINEAR )
return node;

return Methods[ method ].call( { value: node } );

}

}

ColorSpaceNode.LINEAR_TO_LINEAR = 'LinearToLinear';
ColorSpaceNode.LINEAR_TO_SRGB = 'LinearTosRGB';
ColorSpaceNode.LINEAR_TO_sRGB = 'LinearTosRGB';
ColorSpaceNode.sRGB_TO_LINEAR = 'sRGBToLinear';

const Methods = {
[ ColorSpaceNode.LINEAR_TO_sRGB ]: LinearTosRGBShader,
[ ColorSpaceNode.sRGB_TO_LINEAR ]: sRGBToLinearShader
};

export default ColorSpaceNode;

export const colorSpace = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( null, nodeObject( node ) ).fromColorSpace( colorSpace ) );
export const linearToColorSpace = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( getMethod( LinearSRGBColorSpace, colorSpace ), nodeObject( node ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just toColorSpace and fromColorSpace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's is not possible currently. We need to know what the source is to made the conversion properly.

export const colorSpaceToLinear = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( getMethod( colorSpace, LinearSRGBColorSpace ), nodeObject( node ) ) );

export const linearTosRGB = nodeProxy( ColorSpaceNode, ColorSpaceNode.LINEAR_TO_sRGB );
export const sRGBToLinear = nodeProxy( ColorSpaceNode, ColorSpaceNode.sRGB_TO_LINEAR );

addNodeElement( 'colorSpace', colorSpace );
addNodeElement( 'linearTosRGB', linearTosRGB );
addNodeElement( 'sRGBToLinear', sRGBToLinear );
addNodeElement( 'linearToColorSpace', linearToColorSpace );
addNodeElement( 'colorSpaceToLinear', colorSpaceToLinear );

addNodeClass( ColorSpaceNode );
2 changes: 1 addition & 1 deletion examples/jsm/nodes/materials/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class NodeMaterial extends ShaderMaterial {

}

if ( outputColorSpace !== NoColorSpace ) outputNode = outputNode.colorSpace( outputColorSpace );
if ( outputColorSpace !== NoColorSpace ) outputNode = outputNode.linearToColorSpace( outputColorSpace );

// FOG

Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ class WebGPUNodeBuilder extends NodeBuilder {

}

needsColorSpace( texture ) {

return texture.isVideoTexture === true;

}

getSampler( textureProperty, uvSnippet, shaderStage = this.shaderStage ) {

if ( shaderStage === 'fragment' ) {
Expand Down
Binary file added examples/screenshots/webgpu_video_panorama.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.