Skip to content

Commit

Permalink
TSL: add addMethodsChaining (mrdoob#30201)
Browse files Browse the repository at this point in the history
* TSL: add `addMethodsChaining`

* usage ES6

* Revert "usage ES6"

This reverts commit a91ea44.

* cleanup

---------
  • Loading branch information
linbingquan authored Dec 26, 2024
1 parent 62b2a82 commit a7500fd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 107 deletions.
119 changes: 60 additions & 59 deletions src/nodes/math/MathNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TempNode from '../core/TempNode.js';
import { sub, mul, div } from './OperatorNode.js';
import { addMethodChaining, nodeObject, nodeProxy, float, vec2, vec3, vec4, Fn } from '../tsl/TSLCore.js';
import { addMethodsChaining, nodeObject, nodeProxy, float, vec2, vec3, vec4, Fn } from '../tsl/TSLCore.js';
import { WebGLCoordinateSystem, WebGPUCoordinateSystem } from '../../constants.js';

/** @module MathNode **/
Expand Down Expand Up @@ -950,61 +950,62 @@ export const inversesqrt = inverseSqrt;

// Method chaining

addMethodChaining( 'all', all );
addMethodChaining( 'any', any );
addMethodChaining( 'equals', equals );

addMethodChaining( 'radians', radians );
addMethodChaining( 'degrees', degrees );
addMethodChaining( 'exp', exp );
addMethodChaining( 'exp2', exp2 );
addMethodChaining( 'log', log );
addMethodChaining( 'log2', log2 );
addMethodChaining( 'sqrt', sqrt );
addMethodChaining( 'inverseSqrt', inverseSqrt );
addMethodChaining( 'floor', floor );
addMethodChaining( 'ceil', ceil );
addMethodChaining( 'normalize', normalize );
addMethodChaining( 'fract', fract );
addMethodChaining( 'sin', sin );
addMethodChaining( 'cos', cos );
addMethodChaining( 'tan', tan );
addMethodChaining( 'asin', asin );
addMethodChaining( 'acos', acos );
addMethodChaining( 'atan', atan );
addMethodChaining( 'abs', abs );
addMethodChaining( 'sign', sign );
addMethodChaining( 'length', length );
addMethodChaining( 'lengthSq', lengthSq );
addMethodChaining( 'negate', negate );
addMethodChaining( 'oneMinus', oneMinus );
addMethodChaining( 'dFdx', dFdx );
addMethodChaining( 'dFdy', dFdy );
addMethodChaining( 'round', round );
addMethodChaining( 'reciprocal', reciprocal );
addMethodChaining( 'trunc', trunc );
addMethodChaining( 'fwidth', fwidth );
addMethodChaining( 'atan2', atan2 );
addMethodChaining( 'min', min );
addMethodChaining( 'max', max );
addMethodChaining( 'mod', mod );
addMethodChaining( 'step', step );
addMethodChaining( 'reflect', reflect );
addMethodChaining( 'distance', distance );
addMethodChaining( 'dot', dot );
addMethodChaining( 'cross', cross );
addMethodChaining( 'pow', pow );
addMethodChaining( 'pow2', pow2 );
addMethodChaining( 'pow3', pow3 );
addMethodChaining( 'pow4', pow4 );
addMethodChaining( 'transformDirection', transformDirection );
addMethodChaining( 'mix', mixElement );
addMethodChaining( 'clamp', clamp );
addMethodChaining( 'refract', refract );
addMethodChaining( 'smoothstep', smoothstepElement );
addMethodChaining( 'faceForward', faceForward );
addMethodChaining( 'difference', difference );
addMethodChaining( 'saturate', saturate );
addMethodChaining( 'cbrt', cbrt );
addMethodChaining( 'transpose', transpose );
addMethodChaining( 'rand', rand );
addMethodsChaining( {
all: all,
any: any,
equals: equals,
radians: radians,
degrees: degrees,
exp: exp,
exp2: exp2,
log: log,
log2: log2,
sqrt: sqrt,
inverseSqrt: inverseSqrt,
floor: floor,
ceil: ceil,
normalize: normalize,
fract: fract,
sin: sin,
cos: cos,
tan: tan,
asin: asin,
acos: acos,
atan: atan,
abs: abs,
sign: sign,
length: length,
lengthSq: lengthSq,
negate: negate,
oneMinus: oneMinus,
dFdx: dFdx,
dFdy: dFdy,
round: round,
reciprocal: reciprocal,
trunc: trunc,
fwidth: fwidth,
atan2: atan2,
min: min,
max: max,
mod: mod,
step: step,
reflect: reflect,
distance: distance,
dot: dot,
cross: cross,
pow: pow,
pow2: pow2,
pow3: pow3,
pow4: pow4,
transformDirection: transformDirection,
mix: mixElement,
clamp: clamp,
refract: refract,
smoothstep: smoothstepElement,
faceForward: faceForward,
difference: difference,
saturate: saturate,
cbrt: cbrt,
transpose: transpose,
rand: rand
} );
50 changes: 25 additions & 25 deletions src/nodes/math/OperatorNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TempNode from '../core/TempNode.js';
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
import { addMethodsChaining, nodeProxy } from '../tsl/TSLCore.js';

/** @module OperatorNode **/

Expand Down Expand Up @@ -519,34 +519,34 @@ export const shiftLeft = /*@__PURE__*/ nodeProxy( OperatorNode, '<<' );
*/
export const shiftRight = /*@__PURE__*/ nodeProxy( OperatorNode, '>>' );

addMethodChaining( 'add', add );
addMethodChaining( 'sub', sub );
addMethodChaining( 'mul', mul );
addMethodChaining( 'div', div );
addMethodChaining( 'modInt', modInt );
addMethodChaining( 'equal', equal );
addMethodChaining( 'notEqual', notEqual );
addMethodChaining( 'lessThan', lessThan );
addMethodChaining( 'greaterThan', greaterThan );
addMethodChaining( 'lessThanEqual', lessThanEqual );
addMethodChaining( 'greaterThanEqual', greaterThanEqual );
addMethodChaining( 'and', and );
addMethodChaining( 'or', or );
addMethodChaining( 'not', not );
addMethodChaining( 'xor', xor );
addMethodChaining( 'bitAnd', bitAnd );
addMethodChaining( 'bitNot', bitNot );
addMethodChaining( 'bitOr', bitOr );
addMethodChaining( 'bitXor', bitXor );
addMethodChaining( 'shiftLeft', shiftLeft );
addMethodChaining( 'shiftRight', shiftRight );


export const remainder = ( ...params ) => { // @deprecated, r168

console.warn( 'TSL.OperatorNode: .remainder() has been renamed to .modInt().' );
return modInt( ...params );

};

addMethodChaining( 'remainder', remainder );
addMethodsChaining( {
add: add,
sub: sub,
mul: mul,
div: div,
modInt: modInt,
equal: equal,
notEqual: notEqual,
lessThan: lessThan,
greaterThan: greaterThan,
lessThanEqual: lessThanEqual,
greaterThanEqual: greaterThanEqual,
and: and,
or: or,
not: not,
xor: xor,
bitAnd: bitAnd,
bitNot: bitNot,
bitOr: bitOr,
bitXor: bitXor,
shiftLeft: shiftLeft,
shiftRight: shiftRight,
remainder: remainder
} );
57 changes: 34 additions & 23 deletions src/nodes/tsl/TSLCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export function addMethodChaining( name, nodeElement ) {

}

export function addMethodsChaining( object ) {

for ( const key in object ) {

addMethodChaining( key, object[ key ] );

}

}

const parseSwizzle = ( props ) => props.replace( /r|s/g, 'x' ).replace( /g|t/g, 'y' ).replace( /b|p/g, 'z' ).replace( /a|q/g, 'w' );
const parseSwizzleAndSort = ( props ) => parseSwizzle( props ).split( '' ).sort().join( '' );

Expand Down Expand Up @@ -629,32 +639,33 @@ export const mat4 = new ConvertType( 'mat4' );
export const string = ( value = '' ) => nodeObject( new ConstNode( value, 'string' ) );
export const arrayBuffer = ( value ) => nodeObject( new ConstNode( value, 'ArrayBuffer' ) );

addMethodChaining( 'toColor', color );
addMethodChaining( 'toFloat', float );
addMethodChaining( 'toInt', int );
addMethodChaining( 'toUint', uint );
addMethodChaining( 'toBool', bool );
addMethodChaining( 'toVec2', vec2 );
addMethodChaining( 'toIVec2', ivec2 );
addMethodChaining( 'toUVec2', uvec2 );
addMethodChaining( 'toBVec2', bvec2 );
addMethodChaining( 'toVec3', vec3 );
addMethodChaining( 'toIVec3', ivec3 );
addMethodChaining( 'toUVec3', uvec3 );
addMethodChaining( 'toBVec3', bvec3 );
addMethodChaining( 'toVec4', vec4 );
addMethodChaining( 'toIVec4', ivec4 );
addMethodChaining( 'toUVec4', uvec4 );
addMethodChaining( 'toBVec4', bvec4 );
addMethodChaining( 'toMat2', mat2 );
addMethodChaining( 'toMat3', mat3 );
addMethodChaining( 'toMat4', mat4 );

// basic nodes

export const element = /*@__PURE__*/ nodeProxy( ArrayElementNode );
export const convert = ( node, types ) => nodeObject( new ConvertNode( nodeObject( node ), types ) );
export const split = ( node, channels ) => nodeObject( new SplitNode( nodeObject( node ), channels ) );

addMethodChaining( 'element', element );
addMethodChaining( 'convert', convert );
addMethodsChaining( {
toColor: color,
toFloat: float,
toInt: int,
toUint: uint,
toBool: bool,
toVec2: vec2,
toIVec2: ivec2,
toUVec2: uvec2,
toBVec2: bvec2,
toVec3: vec3,
toIVec3: ivec3,
toUVec3: uvec3,
toBVec3: bvec3,
toVec4: vec4,
toIVec4: ivec4,
toUVec4: uvec4,
toBVec4: bvec4,
toMat2: mat2,
toMat3: mat3,
toMat4: mat4,
element: element,
convert: convert
} );

0 comments on commit a7500fd

Please sign in to comment.