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: if, elseif, else syntax #25653

Merged
merged 2 commits into from
Mar 12, 2023
Merged

TSL: if, elseif, else syntax #25653

merged 2 commits into from
Mar 12, 2023

Conversation

sunag
Copy link
Collaborator

@sunag sunag commented Mar 11, 2023

Description

This PR implements If/Else for TSL (Three.js Shading Language) and Nodes.

TSL: Simple

material.colorNode = new ShaderNode( ( stack ) => {

    const n = temp( 1 );

    stack.if( n.greaterThan( .5 ), ( stack ) => {

        stack.assign( n, .5 );

    } ).else( ( stack ) => {
        
        stack.assign( n, 0 );
        
    } );
    
    return n;

} );

TSL: Complex

material.colorNode = new ShaderNode( ( stack ) => {

    const n = temp( 1 );

    stack.if( n.greaterThan( .5 ), ( stack ) => {

        stack.assign( n, .5 );
        
        stack.if( n.greaterThan( .1 ), ( stack ) => {

            stack.assign( n, .1 );

        } );

    } ).elseif( n.greaterThan( .3 ), ( stack ) => {
        
        stack.assign( n, .3 );
        
    } ).else( ( stack ) => {
        
        stack.assign( n, 0 );
        
    } );
    
    return n;

} );

WGSL: Output example

nodeVar1 = 1.0;

if ( ( nodeVar1 > 0.5 ) ) {

    nodeVar1 = 0.5;

    if ( ( nodeVar1 > 0.1 ) ) {

        nodeVar1 = 0.1;
        

    }

    

} else {


    if ( ( nodeVar1 > 0.3 ) ) {

        nodeVar1 = 0.3;
        

    } else {

        nodeVar1 = 0.0;
        

    }

    

}

@sunag sunag added Nodes TSL Three.js Shading Language labels Mar 11, 2023
@sunag sunag added this to the r151 milestone Mar 11, 2023
@@ -311,6 +313,8 @@ export const nodeArray = ( val ) => new ShaderNodeArray( val );
export const nodeProxy = ( ...val ) => new ShaderNodeProxy( ...val );
export const nodeImmutable = ( ...val ) => new ShaderNodeImmutable( ...val );

export const shader = ( ...val ) => new ShaderNode( ...val );
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we then change usages of new ShaderNode (for example, in Skinning) to shader?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shouldn't we then change usages of new ShaderNode (for example, in Skinning) to shader?

I think so.

@LeviPesin
Copy link
Contributor

So nice!!!

Now the only major syntax thing remaining to be required in TSL is loops 😃

@sunag sunag merged commit fb2db55 into mrdoob:dev Mar 12, 2023
@sunag sunag deleted the dev-ifelse branch March 12, 2023 05:09
@Methuselah96 Methuselah96 mentioned this pull request Jan 18, 2024
45 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Nodes TSL Three.js Shading Language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants