Skip to content

Commit 51856a6

Browse files
authored
TSL: Fix equal() inconsistent (#30777)
* fix `equal()` inconsistent * cleanup
1 parent 4f03f16 commit 51856a6

File tree

5 files changed

+65
-26
lines changed

5 files changed

+65
-26
lines changed

src/nodes/core/NodeBuilder.js

-8
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,6 @@ class NodeBuilder {
412412
*/
413413
this.buildStage = null;
414414

415-
/**
416-
* Whether comparison in shader code are generated with methods or not.
417-
*
418-
* @type {boolean}
419-
* @default false
420-
*/
421-
this.useComparisonMethod = false;
422-
423415
}
424416

425417
/**

src/nodes/math/MathNode.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import TempNode from '../core/TempNode.js';
2-
import { sub, mul, div } from './OperatorNode.js';
2+
import { sub, mul, div, equal } from './OperatorNode.js';
33
import { addMethodChaining, nodeObject, nodeProxy, float, vec2, vec3, vec4, Fn } from '../tsl/TSLCore.js';
44
import { WebGLCoordinateSystem, WebGPUCoordinateSystem } from '../../constants.js';
55

@@ -126,7 +126,7 @@ class MathNode extends TempNode {
126126

127127
return 'vec3';
128128

129-
} else if ( method === MathNode.ALL ) {
129+
} else if ( method === MathNode.ALL || method === MathNode.ANY ) {
130130

131131
return 'bool';
132132

@@ -714,9 +714,15 @@ export const bitcast = /*@__PURE__*/ nodeProxy( MathNode, MathNode.BITCAST );
714714
* @function
715715
* @param {Node | number} x - The first parameter.
716716
* @param {Node | number} y - The second parameter.
717+
* @deprecated since r175. Use {@link equal} instead.
717718
* @returns {Node<bool>}
718719
*/
719-
export const equals = /*@__PURE__*/ nodeProxy( MathNode, MathNode.EQUALS );
720+
export const equals = ( x, y ) => { // @deprecated, r172
721+
722+
console.warn( 'THREE.TSL: "equals" is deprecated. Use "equal" inside a vector instead, like: "bvec*( equal( ... ) )"' );
723+
return equal( x, y );
724+
725+
};
720726

721727
/**
722728
* Returns the lesser of two values.

src/nodes/math/OperatorNode.js

+54-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WebGLCoordinateSystem } from '../../constants.js';
12
import TempNode from '../core/TempNode.js';
23
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
34

@@ -117,6 +118,7 @@ class OperatorNode extends TempNode {
117118
} else {
118119

119120
// Handle matrix operations
121+
120122
if ( builder.isMatrix( typeA ) ) {
121123

122124
if ( typeB === 'float' ) {
@@ -148,6 +150,7 @@ class OperatorNode extends TempNode {
148150
}
149151

150152
// Handle non-matrix cases
153+
151154
if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
152155

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

203206
// Keep matrix type for typeA, but ensure typeB stays float
207+
204208
typeB = 'float';
205209

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

211215
} else if ( builder.isMatrix( typeB ) ) {
216+
212217
// matrix x matrix - keep both types
218+
213219
} else {
214220

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

223229
// Keep matrix type for typeB, but ensure typeA stays float
230+
224231
typeA = 'float';
225232

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

228235
// vector x matrix
236+
229237
typeA = builder.getVectorFromMatrix( typeB );
230238

231239
} else {
@@ -256,50 +264,90 @@ class OperatorNode extends TempNode {
256264

257265
if ( output !== 'void' ) {
258266

259-
if ( op === '<' && outputLength > 1 ) {
267+
const isGLSL = builder.renderer.coordinateSystem === WebGLCoordinateSystem;
268+
269+
if ( op === '==' ) {
270+
271+
if ( isGLSL ) {
272+
273+
if ( outputLength > 1 ) {
274+
275+
return builder.format( `${ builder.getMethod( 'equal', output ) }( ${ a }, ${ b } )`, type, output );
276+
277+
} else {
278+
279+
return builder.format( `( ${ a } ${ op } ${ b } )`, type, output );
280+
281+
}
282+
283+
} else {
284+
285+
// WGSL
286+
287+
if ( outputLength > 1 || ! builder.isVector( typeA ) ) {
288+
289+
return builder.format( `( ${ a } == ${ b } )`, type, output );
260290

261-
if ( builder.useComparisonMethod ) {
291+
} else {
292+
293+
return builder.format( `all( ${ a } == ${ b } )`, type, output );
294+
295+
}
296+
297+
}
298+
299+
} else if ( op === '<' && outputLength > 1 ) {
300+
301+
if ( isGLSL ) {
262302

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

265305
} else {
266306

307+
// WGSL
308+
267309
return builder.format( `( ${ a } < ${ b } )`, type, output );
268310

269311
}
270312

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

273-
if ( builder.useComparisonMethod ) {
315+
if ( isGLSL ) {
274316

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

277319
} else {
278320

321+
// WGSL
322+
279323
return builder.format( `( ${ a } <= ${ b } )`, type, output );
280324

281325
}
282326

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

285-
if ( builder.useComparisonMethod ) {
329+
if ( isGLSL ) {
286330

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

289333
} else {
290334

335+
// WGSL
336+
291337
return builder.format( `( ${ a } > ${ b } )`, type, output );
292338

293339
}
294340

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

297-
if ( builder.useComparisonMethod ) {
343+
if ( isGLSL ) {
298344

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

301347
} else {
302348

349+
// WGSL
350+
303351
return builder.format( `( ${ a } >= ${ b } )`, type, output );
304352

305353
}
@@ -315,6 +363,7 @@ class OperatorNode extends TempNode {
315363
} else {
316364

317365
// Handle matrix operations
366+
318367
if ( builder.isMatrix( typeA ) && typeB === 'float' ) {
319368

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

src/nodes/pmrem/PMREMUtils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Fn, int, float, vec2, vec3, vec4, If } from '../tsl/TSLBase.js';
2-
import { cos, sin, abs, max, exp2, log2, clamp, fract, mix, floor, normalize, cross, all } from '../math/MathNode.js';
2+
import { cos, sin, abs, max, exp2, log2, clamp, fract, mix, floor, normalize, cross } from '../math/MathNode.js';
33
import { mul } from '../math/OperatorNode.js';
44
import { select } from '../math/ConditionalNode.js';
55
import { Loop, Break } from '../utils/LoopNode.js';
@@ -258,7 +258,7 @@ export const blur = /*@__PURE__*/ Fn( ( { n, latitudinal, poleAxis, outputDirect
258258

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

261-
If( all( axis.equals( vec3( 0.0 ) ) ), () => {
261+
If( axis.equal( vec3( 0.0 ) ), () => {
262262

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

src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js

-8
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ class GLSLNodeBuilder extends NodeBuilder {
9797
*/
9898
this.builtins = { vertex: [], fragment: [], compute: [] };
9999

100-
/**
101-
* Whether comparison in shader code are generated with methods or not.
102-
*
103-
* @type {boolean}
104-
* @default true
105-
*/
106-
this.useComparisonMethod = true;
107-
108100
}
109101

110102
/**

0 commit comments

Comments
 (0)