Skip to content

Commit 3b07268

Browse files
committed
add expression support for loop() update
1 parent ec16a14 commit 3b07268

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

src/nodes/utils/LoopNode.js

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Node from '../core/Node.js';
22
import { expression } from '../code/ExpressionNode.js';
3-
import { nodeObject, nodeArray } from '../tsl/TSLBase.js';
3+
import { nodeObject, nodeArray, Fn } from '../tsl/TSLBase.js';
44

55
/**
66
* This module offers a variety of ways to implement loops in TSL. In it's basic form it's:
@@ -101,9 +101,15 @@ class LoopNode extends Node {
101101

102102
const stack = builder.addStack(); // TODO: cache() it
103103

104-
properties.returnsNode = this.params[ this.params.length - 1 ]( inputs, stack, builder );
104+
properties.returnsNode = this.params[ this.params.length - 1 ]( inputs, builder );
105105
properties.stackNode = stack;
106106

107+
if ( typeof this.params[ 0 ].update === 'function' ) {
108+
109+
properties.updateNode = Fn( this.params[ 0 ].update )( inputs );
110+
111+
}
112+
107113
builder.removeStack();
108114

109115
return properties;
@@ -222,30 +228,67 @@ class LoopNode extends Node {
222228
const startSnippet = internalParam.start;
223229
const endSnippet = internalParam.end;
224230

225-
let declarationSnippet = '';
226-
let conditionalSnippet = '';
227-
let updateSnippet = '';
231+
let updateSnippet;
228232

229-
if ( ! update ) {
233+
const deltaOperator = () => condition.includes( '<' ) ? '+=' : '-=';
230234

231-
if ( type === 'int' || type === 'uint' ) {
235+
switch ( typeof update ) {
232236

233-
if ( condition.includes( '<' ) ) update = '++';
234-
else update = '--';
237+
case 'undefined':
235238

236-
} else {
239+
if ( type === 'int' || type === 'uint' ) {
237240

238-
if ( condition.includes( '<' ) ) update = '+= 1.';
239-
else update = '-= 1.';
241+
update = condition.includes( '<' ) ? '++' : '--';
240242

241-
}
243+
} else {
242244

243-
}
245+
update = deltaOperator() + ' 1.';
246+
247+
}
248+
249+
updateSnippet = name + ' ' + update;
250+
251+
break;
252+
253+
case 'function':
254+
255+
const flow = builder.flowStagesNode( properties.updateNode, 'void' );
256+
const snippet = flow.code.replace( /\t|;/g, '' );
257+
258+
updateSnippet = snippet;
259+
260+
break;
244261

245-
declarationSnippet += builder.getVar( type, name ) + ' = ' + startSnippet;
262+
case 'number':
263+
264+
updateSnippet = name + ' ' + deltaOperator() + ' ' + builder.generateConst( type, update );
265+
266+
break;
267+
268+
case 'string':
269+
270+
updateSnippet = name + ' ' + update;
271+
272+
break;
273+
274+
default:
275+
276+
if ( update && update.isNode ) {
277+
278+
updateSnippet = name + ' ' + deltaOperator() + ' ' + update.build( builder );
279+
280+
} else {
281+
282+
console.error( 'THREE.TSL: \'Loop( { update: ... } )\' is not a function, string or number.' );
283+
284+
updateSnippet = 'break /* invalid update */';
285+
286+
}
287+
288+
}
246289

247-
conditionalSnippet += name + ' ' + condition + ' ' + endSnippet;
248-
updateSnippet += name + ' ' + update;
290+
const declarationSnippet = builder.getVar( type, name ) + ' = ' + startSnippet;
291+
const conditionalSnippet = name + ' ' + condition + ' ' + endSnippet;
249292

250293
loopSnippet = `for ( ${ declarationSnippet }; ${ conditionalSnippet }; ${ updateSnippet } )`;
251294

0 commit comments

Comments
 (0)