123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- import Node, { addNodeClass } from '../core/Node.js';
- import ArrayElementNode from '../utils/ArrayElementNode.js';
- import ConvertNode from '../utils/ConvertNode.js';
- import JoinNode from '../utils/JoinNode.js';
- import SplitNode from '../utils/SplitNode.js';
- import SetNode from '../utils/SetNode.js';
- import ConstNode from '../core/ConstNode.js';
- import { getValueFromType, getValueType } from '../core/NodeUtils.js';
- //
- let currentStack = null;
- const NodeElements = new Map(); // @TODO: Currently only a few nodes are added, probably also add others
- export function addNodeElement( name, nodeElement ) {
- if ( NodeElements.has( name ) ) {
- console.warn( `Redefinition of node element ${ name }` );
- return;
- }
- if ( typeof nodeElement !== 'function' ) throw new Error( `Node element ${ name } is not a function` );
- NodeElements.set( name, nodeElement );
- }
- 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 shaderNodeHandler = {
- setup( NodeClosure, params ) {
- const inputs = params.shift();
- return NodeClosure( nodeObjects( inputs ), ...params );
- },
- get( node, prop, nodeObj ) {
- if ( typeof prop === 'string' && node[ prop ] === undefined ) {
- if ( node.isStackNode !== true && prop === 'assign' ) {
- return ( ...params ) => {
- currentStack.assign( nodeObj, ...params );
- return nodeObj;
- };
- } else if ( NodeElements.has( prop ) ) {
- const nodeElement = NodeElements.get( prop );
- return node.isStackNode ? ( ...params ) => nodeObj.add( nodeElement( ...params ) ) : ( ...params ) => nodeElement( nodeObj, ...params );
- } else if ( prop === 'self' ) {
- return node;
- } else if ( prop.endsWith( 'Assign' ) && NodeElements.has( prop.slice( 0, prop.length - 'Assign'.length ) ) ) {
- const nodeElement = NodeElements.get( prop.slice( 0, prop.length - 'Assign'.length ) );
- return node.isStackNode ? ( ...params ) => nodeObj.assign( params[ 0 ], nodeElement( ...params ) ) : ( ...params ) => nodeObj.assign( nodeElement( nodeObj, ...params ) );
- } else if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true ) {
- // accessing properties ( swizzle )
- prop = parseSwizzle( prop );
- return nodeObject( new SplitNode( nodeObj, prop ) );
- } else if ( /^set[XYZWRGBASTPQ]{1,4}$/.test( prop ) === true ) {
- // set properties ( swizzle )
- prop = parseSwizzle( prop.slice( 3 ).toLowerCase() );
- // sort to xyzw sequence
- prop = prop.split( '' ).sort().join( '' );
- return ( value ) => nodeObject( new SetNode( node, prop, value ) );
- } else if ( prop === 'width' || prop === 'height' || prop === 'depth' ) {
- // accessing property
- if ( prop === 'width' ) prop = 'x';
- else if ( prop === 'height' ) prop = 'y';
- else if ( prop === 'depth' ) prop = 'z';
- return nodeObject( new SplitNode( node, prop ) );
- } else if ( /^\d+$/.test( prop ) === true ) {
- // accessing array
- return nodeObject( new ArrayElementNode( nodeObj, new ConstNode( Number( prop ), 'uint' ) ) );
- }
- }
- return Reflect.get( node, prop, nodeObj );
- },
- set( node, prop, value, nodeObj ) {
- if ( typeof prop === 'string' && node[ prop ] === undefined ) {
- // setting properties
- if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true || prop === 'width' || prop === 'height' || prop === 'depth' || /^\d+$/.test( prop ) === true ) {
- nodeObj[ prop ].assign( value );
- return true;
- }
- }
- return Reflect.set( node, prop, value, nodeObj );
- }
- };
- const nodeObjectsCacheMap = new WeakMap();
- const nodeBuilderFunctionsCacheMap = new WeakMap();
- const ShaderNodeObject = function ( obj, altType = null ) {
- const type = getValueType( obj );
- if ( type === 'node' ) {
- let nodeObject = nodeObjectsCacheMap.get( obj );
- if ( nodeObject === undefined ) {
- nodeObject = new Proxy( obj, shaderNodeHandler );
- nodeObjectsCacheMap.set( obj, nodeObject );
- nodeObjectsCacheMap.set( nodeObject, nodeObject );
- }
- return nodeObject;
- } else if ( ( altType === null && ( type === 'float' || type === 'boolean' ) ) || ( type && type !== 'shader' && type !== 'string' ) ) {
- return nodeObject( getConstNode( obj, altType ) );
- } else if ( type === 'shader' ) {
- return tslFn( obj );
- }
- return obj;
- };
- const ShaderNodeObjects = function ( objects, altType = null ) {
- for ( const name in objects ) {
- objects[ name ] = nodeObject( objects[ name ], altType );
- }
- return objects;
- };
- const ShaderNodeArray = function ( array, altType = null ) {
- const len = array.length;
- for ( let i = 0; i < len; i ++ ) {
- array[ i ] = nodeObject( array[ i ], altType );
- }
- return array;
- };
- const ShaderNodeProxy = function ( NodeClass, scope = null, factor = null, settings = null ) {
- const assignNode = ( node ) => nodeObject( settings !== null ? Object.assign( node, settings ) : node );
- if ( scope === null ) {
- return ( ...params ) => {
- return assignNode( new NodeClass( ...nodeArray( params ) ) );
- };
- } else if ( factor !== null ) {
- factor = nodeObject( factor );
- return ( ...params ) => {
- return assignNode( new NodeClass( scope, ...nodeArray( params ), factor ) );
- };
- } else {
- return ( ...params ) => {
- return assignNode( new NodeClass( scope, ...nodeArray( params ) ) );
- };
- }
- };
- const ShaderNodeImmutable = function ( NodeClass, ...params ) {
- return nodeObject( new NodeClass( ...nodeArray( params ) ) );
- };
- class ShaderCallNodeInternal extends Node {
- constructor( shaderNode, inputNodes ) {
- super();
- this.shaderNode = shaderNode;
- this.inputNodes = inputNodes;
- }
- getNodeType( builder ) {
- const properties = builder.getNodeProperties( this );
- if ( properties.outputNode === null ) {
- properties.outputNode = this.setupOutput( builder );
- }
- return properties.outputNode.getNodeType( builder );
- }
- call( builder ) {
- const { shaderNode, inputNodes } = this;
- if ( shaderNode.layout ) {
- let functionNodesCacheMap = nodeBuilderFunctionsCacheMap.get( builder.constructor );
- if ( functionNodesCacheMap === undefined ) {
- functionNodesCacheMap = new WeakMap();
- nodeBuilderFunctionsCacheMap.set( builder.constructor, functionNodesCacheMap );
- }
- let functionNode = functionNodesCacheMap.get( shaderNode );
- if ( functionNode === undefined ) {
- functionNode = nodeObject( builder.buildFunctionNode( shaderNode ) );
- functionNodesCacheMap.set( shaderNode, functionNode );
- }
- if ( builder.currentFunctionNode !== null ) {
- builder.currentFunctionNode.includes.push( functionNode );
- }
- return nodeObject( functionNode.call( inputNodes ) );
- }
- const jsFunc = shaderNode.jsFunc;
- const outputNode = inputNodes !== null ? jsFunc( inputNodes, builder.stack, builder ) : jsFunc( builder.stack, builder );
- return nodeObject( outputNode );
- }
- setup( builder ) {
- const { outputNode } = builder.getNodeProperties( this );
- return outputNode || this.setupOutput( builder );
- }
- setupOutput( builder ) {
- builder.addStack();
- builder.stack.outputNode = this.call( builder );
- return builder.removeStack();
- }
- generate( builder, output ) {
- const { outputNode } = builder.getNodeProperties( this );
- if ( outputNode === null ) {
- // TSL: It's recommended to use `tslFn` in setup() pass.
- return this.call( builder ).build( builder, output );
- }
- return super.generate( builder, output );
- }
- }
- class ShaderNodeInternal extends Node {
- constructor( jsFunc ) {
- super();
- this.jsFunc = jsFunc;
- this.layout = null;
- this.global = true;
- }
- get isArrayInput() {
- return /^\((\s+)?\[/.test( this.jsFunc.toString() );
- }
- setLayout( layout ) {
- this.layout = layout;
- return this;
- }
- call( inputs = null ) {
- nodeObjects( inputs );
- return nodeObject( new ShaderCallNodeInternal( this, inputs ) );
- }
- setup() {
- return this.call();
- }
- }
- const bools = [ false, true ];
- const uints = [ 0, 1, 2, 3 ];
- const ints = [ - 1, - 2 ];
- const floats = [ 0.5, 1.5, 1 / 3, 1e-6, 1e6, Math.PI, Math.PI * 2, 1 / Math.PI, 2 / Math.PI, 1 / ( Math.PI * 2 ), Math.PI / 2 ];
- const boolsCacheMap = new Map();
- for ( const bool of bools ) boolsCacheMap.set( bool, new ConstNode( bool ) );
- const uintsCacheMap = new Map();
- for ( const uint of uints ) uintsCacheMap.set( uint, new ConstNode( uint, 'uint' ) );
- const intsCacheMap = new Map( [ ...uintsCacheMap ].map( el => new ConstNode( el.value, 'int' ) ) );
- for ( const int of ints ) intsCacheMap.set( int, new ConstNode( int, 'int' ) );
- const floatsCacheMap = new Map( [ ...intsCacheMap ].map( el => new ConstNode( el.value ) ) );
- for ( const float of floats ) floatsCacheMap.set( float, new ConstNode( float ) );
- for ( const float of floats ) floatsCacheMap.set( - float, new ConstNode( - float ) );
- const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap };
- const constNodesCacheMap = new Map( [ ...boolsCacheMap, ...floatsCacheMap ] );
- const getConstNode = ( value, type ) => {
- if ( constNodesCacheMap.has( value ) ) {
- return constNodesCacheMap.get( value );
- } else if ( value.isNode === true ) {
- return value;
- } else {
- return new ConstNode( value, type );
- }
- };
- const safeGetNodeType = ( node ) => {
- try {
- return node.getNodeType();
- } catch ( _ ) {
- return undefined;
- }
- };
- const ConvertType = function ( type, cacheMap = null ) {
- return ( ...params ) => {
- if ( params.length === 0 || ( ! [ 'bool', 'float', 'int', 'uint' ].includes( type ) && params.every( param => typeof param !== 'object' ) ) ) {
- params = [ getValueFromType( type, ...params ) ];
- }
- if ( params.length === 1 && cacheMap !== null && cacheMap.has( params[ 0 ] ) ) {
- return nodeObject( cacheMap.get( params[ 0 ] ) );
- }
- if ( params.length === 1 ) {
- const node = getConstNode( params[ 0 ], type );
- if ( safeGetNodeType( node ) === type ) return nodeObject( node );
- return nodeObject( new ConvertNode( node, type ) );
- }
- const nodes = params.map( param => getConstNode( param ) );
- return nodeObject( new JoinNode( nodes, type ) );
- };
- };
- // exports
- export const defined = ( value ) => value && value.value;
- // utils
- export const getConstNodeType = ( value ) => ( value !== undefined && value !== null ) ? ( value.nodeType || value.convertTo || ( typeof value === 'string' ? value : null ) ) : null;
- // shader node base
- export function ShaderNode( jsFunc ) {
- return new Proxy( new ShaderNodeInternal( jsFunc ), shaderNodeHandler );
- }
- export const nodeObject = ( val, altType = null ) => /* new */ ShaderNodeObject( val, altType );
- export const nodeObjects = ( val, altType = null ) => new ShaderNodeObjects( val, altType );
- export const nodeArray = ( val, altType = null ) => new ShaderNodeArray( val, altType );
- export const nodeProxy = ( ...params ) => new ShaderNodeProxy( ...params );
- export const nodeImmutable = ( ...params ) => new ShaderNodeImmutable( ...params );
- export const tslFn = ( jsFunc ) => {
- const shaderNode = new ShaderNode( jsFunc );
- const fn = ( ...params ) => {
- let inputs;
- nodeObjects( params );
- if ( params[ 0 ] && params[ 0 ].isNode ) {
- inputs = [ ...params ];
- } else {
- inputs = params[ 0 ];
- }
- return shaderNode.call( inputs );
- };
- fn.shaderNode = shaderNode;
- fn.setLayout = ( layout ) => {
- shaderNode.setLayout( layout );
- return fn;
- };
- return fn;
- };
- addNodeClass( 'ShaderNode', ShaderNode );
- //
- addNodeElement( 'toGlobal', ( node ) => {
- node.global = true;
- return node;
- } );
- //
- export const setCurrentStack = ( stack ) => {
- if ( currentStack === stack ) {
- //throw new Error( 'Stack already defined.' );
- }
- currentStack = stack;
- };
- export const getCurrentStack = () => currentStack;
- export const If = ( ...params ) => currentStack.if( ...params );
- export function append( node ) {
- if ( currentStack ) currentStack.add( node );
- return node;
- }
- addNodeElement( 'append', append );
- // types
- // @TODO: Maybe export from ConstNode.js?
- export const color = new ConvertType( 'color' );
- export const float = new ConvertType( 'float', cacheMaps.float );
- export const int = new ConvertType( 'int', cacheMaps.ints );
- export const uint = new ConvertType( 'uint', cacheMaps.uint );
- export const bool = new ConvertType( 'bool', cacheMaps.bool );
- export const vec2 = new ConvertType( 'vec2' );
- export const ivec2 = new ConvertType( 'ivec2' );
- export const uvec2 = new ConvertType( 'uvec2' );
- export const bvec2 = new ConvertType( 'bvec2' );
- export const vec3 = new ConvertType( 'vec3' );
- export const ivec3 = new ConvertType( 'ivec3' );
- export const uvec3 = new ConvertType( 'uvec3' );
- export const bvec3 = new ConvertType( 'bvec3' );
- export const vec4 = new ConvertType( 'vec4' );
- export const ivec4 = new ConvertType( 'ivec4' );
- export const uvec4 = new ConvertType( 'uvec4' );
- export const bvec4 = new ConvertType( 'bvec4' );
- export const mat2 = new ConvertType( 'mat2' );
- export const imat2 = new ConvertType( 'imat2' );
- export const umat2 = new ConvertType( 'umat2' );
- export const bmat2 = new ConvertType( 'bmat2' );
- export const mat3 = new ConvertType( 'mat3' );
- export const imat3 = new ConvertType( 'imat3' );
- export const umat3 = new ConvertType( 'umat3' );
- export const bmat3 = new ConvertType( 'bmat3' );
- export const mat4 = new ConvertType( 'mat4' );
- export const imat4 = new ConvertType( 'imat4' );
- export const umat4 = new ConvertType( 'umat4' );
- export const bmat4 = new ConvertType( 'bmat4' );
- export const string = ( value = '' ) => nodeObject( new ConstNode( value, 'string' ) );
- export const arrayBuffer = ( value ) => nodeObject( new ConstNode( value, 'ArrayBuffer' ) );
- addNodeElement( 'toColor', color );
- addNodeElement( 'toFloat', float );
- addNodeElement( 'toInt', int );
- addNodeElement( 'toUint', uint );
- addNodeElement( 'toBool', bool );
- addNodeElement( 'toVec2', vec2 );
- addNodeElement( 'toIvec2', ivec2 );
- addNodeElement( 'toUvec2', uvec2 );
- addNodeElement( 'toBvec2', bvec2 );
- addNodeElement( 'toVec3', vec3 );
- addNodeElement( 'toIvec3', ivec3 );
- addNodeElement( 'toUvec3', uvec3 );
- addNodeElement( 'toBvec3', bvec3 );
- addNodeElement( 'toVec4', vec4 );
- addNodeElement( 'toIvec4', ivec4 );
- addNodeElement( 'toUvec4', uvec4 );
- addNodeElement( 'toBvec4', bvec4 );
- addNodeElement( 'toMat2', mat2 );
- addNodeElement( 'toImat2', imat2 );
- addNodeElement( 'toUmat2', umat2 );
- addNodeElement( 'toBmat2', bmat2 );
- addNodeElement( 'toMat3', mat3 );
- addNodeElement( 'toImat3', imat3 );
- addNodeElement( 'toUmat3', umat3 );
- addNodeElement( 'toBmat3', bmat3 );
- addNodeElement( 'toMat4', mat4 );
- addNodeElement( 'toImat4', imat4 );
- addNodeElement( 'toUmat4', umat4 );
- addNodeElement( 'toBmat4', bmat4 );
- // basic nodes
- // HACK - we cannot export them from the corresponding files because of the cyclic dependency
- export const element = nodeProxy( ArrayElementNode );
- export const convert = ( node, types ) => nodeObject( new ConvertNode( nodeObject( node ), types ) );
- export const split = ( node, channels ) => nodeObject( new SplitNode( nodeObject( node ), channels ) );
- addNodeElement( 'element', element );
- addNodeElement( 'convert', convert );
|