Skip to content
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
2 changes: 1 addition & 1 deletion examples/example.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ a {
pointer-events: none;
}

#info > a {
#info a {
pointer-events: auto;
}

Expand Down
3 changes: 2 additions & 1 deletion examples/jsm/transpiler/AST.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,12 +612,13 @@ export class For extends ASTNode {

export class While extends ASTNode {

constructor( condition, body = [] ) {
constructor( condition, body = [], doWhile = false ) {

super();

this.condition = condition;
this.body = body;
this.doWhile = doWhile;

this.isWhile = true;

Expand Down
43 changes: 43 additions & 0 deletions examples/jsm/transpiler/GLSLDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,45 @@ class GLSLDecoder {

}

parseDoWhile() {

this.readToken(); // skip 'do'

let body;

if ( this.getToken().str === '{' ) {

body = this.parseBlock();

} else {

body = [ this.parseExpression() ];

}

if ( this.getToken() && this.getToken().str === 'while' ) {

this.readToken(); // skip 'while'

} else {

throw new Error( 'THREE.GLSLDecoder: Expected \'while\' after \'do\' block' + this.getTokenPosition( this.getToken() ) );

}

const condTokens = this.readTokensUntil( ')' );
const condition = this.parseExpressionFromTokens( condTokens.slice( 1, - 1 ) );

if ( this.getToken() && this.getToken().str === ';' ) {

this.readToken(); // skip trailing ';'

}

return new While( condition, body, true );

}

parseFor() {

this.readToken(); // skip 'for'
Expand Down Expand Up @@ -1219,6 +1258,10 @@ class GLSLDecoder {

statement = this.parseWhile();

} else if ( token.str === 'do' ) {

statement = this.parseDoWhile();

} else if ( token.str === 'switch' ) {

statement = this.parseSwitch();
Expand Down
72 changes: 71 additions & 1 deletion examples/jsm/transpiler/TSLEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,38 @@ ${ this.tab }} )`;

}

isVariableUsed( node, varName ) {

if ( ! node ) return false;

if ( node.isAccessor && node.property === varName ) return true;

for ( const key in node ) {

if ( key === 'parent' ) continue;

const child = node[ key ];

if ( Array.isArray( child ) ) {

for ( const item of child ) {

if ( this.isVariableUsed( item, varName ) ) return true;

}

} else if ( child && child.isASTNode ) {

if ( this.isVariableUsed( child, varName ) ) return true;

}

}

return false;

}

emitLoop( node ) {

const start = this.emitExpression( node.initialization.value );
Expand Down Expand Up @@ -518,7 +550,22 @@ ${ this.tab }} )`;

}

let loopStr = `Loop( { start: ${ start }, end: ${ end + nameParam + typeParam + conditionParam + updateParam } }, ( { ${ name } } ) => {\n\n`;
let loopParams;

if ( start === '0' && nameParam === '' && typeParam === '' && conditionParam === '' && updateParam === '' ) {

loopParams = end;

} else {

loopParams = `{ start: ${ start }, end: ${ end + nameParam + typeParam + conditionParam + updateParam } }`;

}

const isUsed = this.isVariableUsed( node.body, name );
const callbackParams = isUsed ? `( { ${ name } } ) =>` : '() =>';

let loopStr = `Loop( ${ loopParams }, ${ callbackParams } {\n\n`;

loopStr += this.emitBody( node.body ) + '\n\n';

Expand Down Expand Up @@ -634,8 +681,31 @@ ${ this.tab }} )`;

}

emitDoWhile( node ) {

const condition = this.emitExpression( node.condition );
const body = this.emitBody( node.body );

let loopStr = `Loop( () => {\n\n${ body }\n\n${ this.tab }\tIf( ${ condition }.not(), () => {\n\n${ this.tab }\t\tBreak();\n\n${ this.tab }\t} );\n\n`;

loopStr += this.tab + '} )';

this.imports.add( 'Loop' );
this.imports.add( 'If' );
this.imports.add( 'Break' );

return loopStr;

}

emitWhile( node ) {

if ( node.doWhile ) {

return this.emitDoWhile( node );

}

const condition = this.emitExpression( node.condition );

let whileStr = `Loop( ${ condition }, () => {\n\n`;
Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/transpiler/WGSLEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ class WGSLEncoder {
const cond = this.emitExpression( node.condition );
const body = this.emitBody( node.body );

if ( node.doWhile ) {

return `loop {\n\n${ body }\n\n${ this.tab }\tcontinuing {\n${ this.tab }\t\tbreak if ( ! ( ${ cond } ) );\n${ this.tab }\t}\n${ this.tab }}`;

}

return `while ( ${ cond } ) {\n\n${ body }\n\n${ this.tab }}`;

}
Expand Down
2 changes: 1 addition & 1 deletion examples/webgpu_gaussian_splatting.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Gaussian Splatting</span>
</div>

<small id="status">WebGPU TSL Gaussian splat renderer by <a href="https://ben3d.ca">Ben Houston</a>.</small>
<small id="status">WebGPU TSL Gaussian splat renderer by <a href="https://ben3d.ca" target="_blank" rel="noopener">Ben Houston</a>.</small>
<small id="license"></small>
</div>

Expand Down
31 changes: 23 additions & 8 deletions src/nodes/utils/LoopNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Node from '../core/Node.js';
import { expression } from '../code/ExpressionNode.js';
import { nodeArray, Fn } from '../tsl/TSLBase.js';
import { nodeArray, Fn, bool } from '../tsl/TSLBase.js';
import { error } from '../../utils.js';

/**
Expand Down Expand Up @@ -88,10 +88,11 @@ class LoopNode extends Node {
//

const inputs = {};
const params = this._getInternalParams();

for ( let i = 0, l = this.params.length - 1; i < l; i ++ ) {
for ( let i = 0, l = params.length - 1; i < l; i ++ ) {

const param = this.params[ i ];
const param = params[ i ];

const name = ( param.isNode !== true && param.name ) || this.getVarName( i );
const type = ( param.isNode !== true && param.type ) || 'int';
Expand All @@ -102,16 +103,16 @@ class LoopNode extends Node {

const stack = builder.addStack();

const fnCall = this.params[ this.params.length - 1 ]( inputs );
const fnCall = params[ params.length - 1 ]( inputs );

properties.returnsNode = fnCall.context( { nodeLoop: fnCall } );
properties.stackNode = stack;

const baseParam = this.params[ 0 ];
const baseParam = params[ 0 ];

if ( baseParam.isNode !== true && typeof baseParam.update === 'function' ) {

const fnUpdateCall = Fn( this.params[ 0 ].update )( inputs );
const fnUpdateCall = Fn( baseParam.update )( inputs );

properties.updateNode = fnUpdateCall.context( { nodeLoop: fnUpdateCall } );

Expand All @@ -123,6 +124,20 @@ class LoopNode extends Node {

}

_getInternalParams() {

const params = this.params;

if ( typeof params[ 0 ] === 'function' ) {

return [ bool( true ), params[ 0 ] ];

}

return params;

}

setup( builder ) {

// setup properties
Expand All @@ -142,7 +157,7 @@ class LoopNode extends Node {

const properties = this.getProperties( builder );

const params = this.params;
const params = this._getInternalParams();
const stackNode = properties.stackNode;

for ( let i = 0, l = params.length - 1; i < l; i ++ ) {
Expand Down Expand Up @@ -306,7 +321,7 @@ class LoopNode extends Node {

builder.removeFlowTab().addFlowCode( '\n' + builder.tab + stackSnippet );

for ( let i = 0, l = this.params.length - 1; i < l; i ++ ) {
for ( let i = 0, l = params.length - 1; i < l; i ++ ) {

builder.addFlowCode( ( i === 0 ? '' : builder.tab ) + '}\n\n' ).removeFlowTab();

Expand Down