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

TSL: Add workingToColorSpace, colorSpaceToWorking #29378

Merged
merged 6 commits into from
Sep 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UniformNode, { uniform } from '../core/UniformNode.js';
import { uv } from './UV.js';
import { textureSize } from './TextureSizeNode.js';
import { toWorkingColorSpace } from '../display/ColorSpaceNode.js';
import { colorSpaceToWorking } from '../display/ColorSpaceNode.js';
import { expression } from '../code/ExpressionNode.js';
import { maxMipLevel } from '../utils/MaxMipLevelNode.js';
import { nodeProxy, vec3, nodeObject } from '../tsl/TSLBase.js';
Expand Down Expand Up @@ -281,7 +281,7 @@ class TextureNode extends UniformNode {

if ( builder.needsToWorkingColorSpace( texture ) ) {

snippet = toWorkingColorSpace( expression( snippet, nodeType ), texture.colorSpace ).setup( builder ).build( builder, nodeType );
snippet = colorSpaceToWorking( expression( snippet, nodeType ), texture.colorSpace ).setup( builder ).build( builder, nodeType );

}

Expand Down
50 changes: 38 additions & 12 deletions src/nodes/display/ColorSpaceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import TempNode from '../core/TempNode.js';
import { addMethodChaining, nodeObject, vec4 } from '../tsl/TSLCore.js';

import { LinearSRGBColorSpace, SRGBColorSpace } from '../../constants.js';
import { ColorManagement } from '../../math/ColorManagement.js';

const getColorSpaceName = ( colorSpace ) => {
const WORKING_COLOR_SPACE = 'WorkingColorSpace';
const OUTPUT_COLOR_SPACE = 'OutputColorSpace';

function getColorSpaceName( colorSpace ) {

let method = null;

Expand All @@ -19,13 +23,13 @@ const getColorSpaceName = ( colorSpace ) => {

return method;

};
}

export const getColorSpaceMethod = ( source, target ) => {
export function getColorSpaceMethod( source, target ) {

return getColorSpaceName( source ) + 'To' + getColorSpaceName( target );

};
}

class ColorSpaceNode extends TempNode {

Expand All @@ -35,23 +39,39 @@ class ColorSpaceNode extends TempNode {

}

constructor( colorNode, target = null, source = null ) {
constructor( colorNode, source, target ) {

super( 'vec4' );

this.colorNode = colorNode;
this.target = target;
this.source = source;
this.target = target;

}

getColorSpace( builder, colorSpace ) {

if ( colorSpace === WORKING_COLOR_SPACE ) {

return ColorManagement.workingColorSpace;

} else if ( colorSpace === OUTPUT_COLOR_SPACE ) {

return builder.context.outputColorSpace || builder.renderer.outputColorSpace;

}

return colorSpace;

}

setup( builder ) {

const { renderer, context } = builder;
const { renderer } = builder;
const { colorNode } = this;

const source = this.source || context.outputColorSpace || renderer.outputColorSpace;
const target = this.target || context.outputColorSpace || renderer.outputColorSpace;
const colorNode = this.colorNode;
const source = this.getColorSpace( builder, this.source );
const target = this.getColorSpace( builder, this.target );

if ( source === target ) return colorNode;

Expand Down Expand Up @@ -81,8 +101,14 @@ class ColorSpaceNode extends TempNode {

export default ColorSpaceNode;

export const toOutputColorSpace = ( node, colorSpace = null ) => nodeObject( new ColorSpaceNode( nodeObject( node ), colorSpace, LinearSRGBColorSpace ) );
export const toWorkingColorSpace = ( node, colorSpace = null ) => nodeObject( new ColorSpaceNode( nodeObject( node ), LinearSRGBColorSpace, colorSpace ) );
export const toOutputColorSpace = ( node ) => nodeObject( new ColorSpaceNode( nodeObject( node ), WORKING_COLOR_SPACE, OUTPUT_COLOR_SPACE ) );
export const toWorkingColorSpace = ( node ) => nodeObject( new ColorSpaceNode( nodeObject( node ), OUTPUT_COLOR_SPACE, WORKING_COLOR_SPACE ) );

export const workingToColorSpace = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( nodeObject( node ), WORKING_COLOR_SPACE, colorSpace ) );
export const colorSpaceToWorking = ( node, colorSpace ) => nodeObject( new ColorSpaceNode( nodeObject( node ), colorSpace, WORKING_COLOR_SPACE ) );

addMethodChaining( 'toOutputColorSpace', toOutputColorSpace );
addMethodChaining( 'toWorkingColorSpace', toWorkingColorSpace );

addMethodChaining( 'workingToColorSpace', workingToColorSpace );
addMethodChaining( 'colorSpaceToWorking', colorSpaceToWorking );
11 changes: 6 additions & 5 deletions src/nodes/display/RenderOutputNode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import TempNode from '../core/TempNode.js';
import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js';

import { LinearSRGBColorSpace, SRGBColorSpace, NoToneMapping } from '../../constants.js';
import { NoColorSpace, NoToneMapping } from '../../constants.js';
import { ColorManagement } from '../../math/ColorManagement.js';

class RenderOutputNode extends TempNode {

Expand Down Expand Up @@ -30,19 +31,19 @@ class RenderOutputNode extends TempNode {
// tone mapping

const toneMapping = ( this.toneMapping !== null ? this.toneMapping : context.toneMapping ) || NoToneMapping;
const outputColorSpace = ( this.outputColorSpace !== null ? this.outputColorSpace : context.outputColorSpace ) || LinearSRGBColorSpace;
const outputColorSpace = ( this.outputColorSpace !== null ? this.outputColorSpace : context.outputColorSpace ) || NoColorSpace;

if ( toneMapping !== NoToneMapping ) {

outputNode = outputNode.toneMapping( toneMapping );

}

// output color space
// working to output color space

if ( outputColorSpace === SRGBColorSpace ) {
if ( outputColorSpace !== NoColorSpace && outputColorSpace !== ColorManagement.workingColorSpace ) {

outputNode = outputNode.toOutputColorSpace( outputColorSpace );
outputNode = outputNode.workingToColorSpace( outputColorSpace );

}

Expand Down