Skip to content

Commit

Permalink
add WGSLNodeFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Oct 20, 2021
1 parent 606a7dd commit a7a7351
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
89 changes: 89 additions & 0 deletions examples/jsm/renderers/nodes/parsers/WGSLNodeFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import NodeFunction from '../core/NodeFunction.js';
import NodeFunctionInput from '../core/NodeFunctionInput.js';

const declarationRegexp = /^fn\s*([a-z_0-9]+)?\s*\(([\s\S]*?)\)\s*\-\>\s*([a-z_0-9]+)?/i;
const propertiesRegexp = /[a-z_0-9]+/ig;

const parse = ( source ) => {

source = source.trim();

const declaration = source.match( declarationRegexp );

if ( declaration !== null && declaration.length === 4 ) {

// tokenizer

const inputsCode = declaration[ 2 ];
const propsMatches = [];

let nameMatch = null;

while ( ( nameMatch = propertiesRegexp.exec( inputsCode ) ) !== null ) {

propsMatches.push( nameMatch );

}

// parser

const inputs = [];

let i = 0;

while ( i < propsMatches.length ) {

const name = propsMatches[ i ++ ][ 0 ];
const type = propsMatches[ i ++ ][ 0 ];

propsMatches[ i ++ ][ 0 ]; // precision

inputs.push( new NodeFunctionInput( type, name ) );

}

//

const blockCode = source.substring( declaration[ 0 ].length );

const name = declaration[ 1 ] !== undefined ? declaration[ 1 ] : '';
const type = declaration[ 3 ];

return {
type,
inputs,
name,
inputsCode,
blockCode
};

} else {

throw new Error( 'FunctionNode: Function is not a WGSL code.' );

}

};

class WGSLNodeFunction extends NodeFunction {

constructor( source ) {

const { type, inputs, name, inputsCode, blockCode } = parse( source );

super( type, inputs, name );

this.inputsCode = inputsCode;
this.blockCode = blockCode;

}

getCode( name = this.name ) {

return `fn ${ name } ( ${ this.inputsCode.trim() } ) -> ${ this.type }` + this.blockCode;

}

}

export default WGSLNodeFunction;
14 changes: 14 additions & 0 deletions examples/jsm/renderers/nodes/parsers/WGSLNodeParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import NodeParser from '../core/NodeParser.js';
import WGSLNodeFunction from './WGSLNodeFunction.js';

class WGSLNodeParser extends NodeParser {

parseFunction( source ) {

return new WGSLNodeFunction( source );

}

}

export default WGSLNodeParser;
3 changes: 2 additions & 1 deletion examples/jsm/renderers/webgpu/nodes/WebGPUNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ModelViewProjectionNode from '../../nodes/accessors/ModelViewProjectionNo
import SkinningNode from '../../nodes/accessors/SkinningNode.js';
import LightContextNode from '../../nodes/lights/LightContextNode.js';
import OperatorNode from '../../nodes/math/OperatorNode.js';
import WGSLNodeParser from '../../nodes/parsers/WGSLNodeParser.js';

const wgslTypeLib = {
float: 'f32',
Expand Down Expand Up @@ -68,7 +69,7 @@ class WebGPUNodeBuilder extends NodeBuilder {

constructor( object, renderer, lightNode = null ) {

super( object, renderer );
super( object, renderer, new WGSLNodeParser() );

this.lightNode = lightNode;

Expand Down

0 comments on commit a7a7351

Please sign in to comment.