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: Fix equal() inconsistent #30777

Merged
merged 2 commits into from
Mar 22, 2025
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
8 changes: 0 additions & 8 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,6 @@ class NodeBuilder {
*/
this.buildStage = null;

/**
* Whether comparison in shader code are generated with methods or not.
*
* @type {boolean}
* @default false
*/
this.useComparisonMethod = false;

}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/nodes/math/MathNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TempNode from '../core/TempNode.js';
import { sub, mul, div } from './OperatorNode.js';
import { sub, mul, div, equal } from './OperatorNode.js';
import { addMethodChaining, nodeObject, nodeProxy, float, vec2, vec3, vec4, Fn } from '../tsl/TSLCore.js';
import { WebGLCoordinateSystem, WebGPUCoordinateSystem } from '../../constants.js';

Expand Down Expand Up @@ -126,7 +126,7 @@ class MathNode extends TempNode {

return 'vec3';

} else if ( method === MathNode.ALL ) {
} else if ( method === MathNode.ALL || method === MathNode.ANY ) {

return 'bool';

Expand Down Expand Up @@ -714,9 +714,15 @@ export const bitcast = /*@__PURE__*/ nodeProxy( MathNode, MathNode.BITCAST );
* @function
* @param {Node | number} x - The first parameter.
* @param {Node | number} y - The second parameter.
* @deprecated since r175. Use {@link equal} instead.
* @returns {Node<bool>}
*/
export const equals = /*@__PURE__*/ nodeProxy( MathNode, MathNode.EQUALS );
export const equals = ( x, y ) => { // @deprecated, r172

console.warn( 'THREE.TSL: "equals" is deprecated. Use "equal" inside a vector instead, like: "bvec*( equal( ... ) )"' );
return equal( x, y );

};

/**
* Returns the lesser of two values.
Expand Down
59 changes: 54 additions & 5 deletions src/nodes/math/OperatorNode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WebGLCoordinateSystem } from '../../constants.js';
import TempNode from '../core/TempNode.js';
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';

Expand Down Expand Up @@ -117,6 +118,7 @@ class OperatorNode extends TempNode {
} else {

// Handle matrix operations

if ( builder.isMatrix( typeA ) ) {

if ( typeB === 'float' ) {
Expand Down Expand Up @@ -148,6 +150,7 @@ class OperatorNode extends TempNode {
}

// Handle non-matrix cases

if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {

// anytype x anytype: use the greater length vector
Expand Down Expand Up @@ -201,6 +204,7 @@ class OperatorNode extends TempNode {
if ( typeB === 'float' ) {

// Keep matrix type for typeA, but ensure typeB stays float

typeB = 'float';

} else if ( builder.isVector( typeB ) ) {
Expand All @@ -209,7 +213,9 @@ class OperatorNode extends TempNode {
typeB = builder.getVectorFromMatrix( typeA );

} else if ( builder.isMatrix( typeB ) ) {

// matrix x matrix - keep both types

} else {

typeA = typeB = type;
Expand All @@ -221,11 +227,13 @@ class OperatorNode extends TempNode {
if ( typeA === 'float' ) {

// Keep matrix type for typeB, but ensure typeA stays float

typeA = 'float';

} else if ( builder.isVector( typeA ) ) {

// vector x matrix

typeA = builder.getVectorFromMatrix( typeB );

} else {
Expand Down Expand Up @@ -256,50 +264,90 @@ class OperatorNode extends TempNode {

if ( output !== 'void' ) {

if ( op === '<' && outputLength > 1 ) {
const isGLSL = builder.renderer.coordinateSystem === WebGLCoordinateSystem;

if ( op === '==' ) {

if ( isGLSL ) {

if ( outputLength > 1 ) {

return builder.format( `${ builder.getMethod( 'equal', output ) }( ${ a }, ${ b } )`, type, output );

} else {

return builder.format( `( ${ a } ${ op } ${ b } )`, type, output );

}

} else {

// WGSL

if ( outputLength > 1 || ! builder.isVector( typeA ) ) {

return builder.format( `( ${ a } == ${ b } )`, type, output );

if ( builder.useComparisonMethod ) {
} else {

return builder.format( `all( ${ a } == ${ b } )`, type, output );

}

}

} else if ( op === '<' && outputLength > 1 ) {

if ( isGLSL ) {

return builder.format( `${ builder.getMethod( 'lessThan', output ) }( ${ a }, ${ b } )`, type, output );

} else {

// WGSL

return builder.format( `( ${ a } < ${ b } )`, type, output );

}

} else if ( op === '<=' && outputLength > 1 ) {

if ( builder.useComparisonMethod ) {
if ( isGLSL ) {

return builder.format( `${ builder.getMethod( 'lessThanEqual', output ) }( ${ a }, ${ b } )`, type, output );

} else {

// WGSL

return builder.format( `( ${ a } <= ${ b } )`, type, output );

}

} else if ( op === '>' && outputLength > 1 ) {

if ( builder.useComparisonMethod ) {
if ( isGLSL ) {

return builder.format( `${ builder.getMethod( 'greaterThan', output ) }( ${ a }, ${ b } )`, type, output );

} else {

// WGSL

return builder.format( `( ${ a } > ${ b } )`, type, output );

}

} else if ( op === '>=' && outputLength > 1 ) {

if ( builder.useComparisonMethod ) {
if ( isGLSL ) {

return builder.format( `${ builder.getMethod( 'greaterThanEqual', output ) }( ${ a }, ${ b } )`, type, output );

} else {

// WGSL

return builder.format( `( ${ a } >= ${ b } )`, type, output );

}
Expand All @@ -315,6 +363,7 @@ class OperatorNode extends TempNode {
} else {

// Handle matrix operations

if ( builder.isMatrix( typeA ) && typeB === 'float' ) {

return builder.format( `( ${ b } ${ op } ${ a } )`, type, output );
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/pmrem/PMREMUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fn, int, float, vec2, vec3, vec4, If } from '../tsl/TSLBase.js';
import { cos, sin, abs, max, exp2, log2, clamp, fract, mix, floor, normalize, cross, all } from '../math/MathNode.js';
import { cos, sin, abs, max, exp2, log2, clamp, fract, mix, floor, normalize, cross } from '../math/MathNode.js';
import { mul } from '../math/OperatorNode.js';
import { select } from '../math/ConditionalNode.js';
import { Loop, Break } from '../utils/LoopNode.js';
Expand Down Expand Up @@ -258,7 +258,7 @@ export const blur = /*@__PURE__*/ Fn( ( { n, latitudinal, poleAxis, outputDirect

const axis = vec3( select( latitudinal, poleAxis, cross( poleAxis, outputDirection ) ) ).toVar();

If( all( axis.equals( vec3( 0.0 ) ) ), () => {
If( axis.equal( vec3( 0.0 ) ), () => {

axis.assign( vec3( outputDirection.z, 0.0, outputDirection.x.negate() ) );

Expand Down
8 changes: 0 additions & 8 deletions src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ class GLSLNodeBuilder extends NodeBuilder {
*/
this.builtins = { vertex: [], fragment: [], compute: [] };

/**
* Whether comparison in shader code are generated with methods or not.
*
* @type {boolean}
* @default true
*/
this.useComparisonMethod = true;

}

/**
Expand Down
Loading