123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251 |
- import NodeUniform from './NodeUniform.js';
- import NodeAttribute from './NodeAttribute.js';
- import NodeVarying from './NodeVarying.js';
- import NodeVar from './NodeVar.js';
- import NodeCode from './NodeCode.js';
- import NodeKeywords from './NodeKeywords.js';
- import NodeCache from './NodeCache.js';
- import ParameterNode from './ParameterNode.js';
- import FunctionNode from '../code/FunctionNode.js';
- import { createNodeMaterialFromType, default as NodeMaterial } from '../materials/NodeMaterial.js';
- import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js';
- import {
- FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
- ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
- } from '../../renderers/common/nodes/NodeUniform.js';
- import { REVISION, RenderTarget, Color, Vector2, Vector3, Vector4, IntType, UnsignedIntType, Float16BufferAttribute } from 'three';
- import { stack } from './StackNode.js';
- import { getCurrentStack, setCurrentStack } from '../shadernode/ShaderNode.js';
- import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js';
- import ChainMap from '../../renderers/common/ChainMap.js';
- import PMREMGenerator from '../../renderers/common/extras/PMREMGenerator.js';
- const uniformsGroupCache = new ChainMap();
- const typeFromLength = new Map( [
- [ 2, 'vec2' ],
- [ 3, 'vec3' ],
- [ 4, 'vec4' ],
- [ 9, 'mat3' ],
- [ 16, 'mat4' ]
- ] );
- const typeFromArray = new Map( [
- [ Int8Array, 'int' ],
- [ Int16Array, 'int' ],
- [ Int32Array, 'int' ],
- [ Uint8Array, 'uint' ],
- [ Uint16Array, 'uint' ],
- [ Uint32Array, 'uint' ],
- [ Float32Array, 'float' ]
- ] );
- const toFloat = ( value ) => {
- value = Number( value );
- return value + ( value % 1 ? '' : '.0' );
- };
- class NodeBuilder {
- constructor( object, renderer, parser, scene = null, material = null ) {
- this.object = object;
- this.material = material || ( object && object.material ) || null;
- this.geometry = ( object && object.geometry ) || null;
- this.renderer = renderer;
- this.parser = parser;
- this.scene = scene;
- this.nodes = [];
- this.updateNodes = [];
- this.updateBeforeNodes = [];
- this.hashNodes = {};
- this.lightsNode = null;
- this.environmentNode = null;
- this.fogNode = null;
- this.clippingContext = null;
- this.vertexShader = null;
- this.fragmentShader = null;
- this.computeShader = null;
- this.flowNodes = { vertex: [], fragment: [], compute: [] };
- this.flowCode = { vertex: '', fragment: '', compute: [] };
- this.uniforms = { vertex: [], fragment: [], compute: [], index: 0 };
- this.structs = { vertex: [], fragment: [], compute: [], index: 0 };
- this.bindings = { vertex: [], fragment: [], compute: [] };
- this.bindingsOffset = { vertex: 0, fragment: 0, compute: 0 };
- this.bindingsArray = null;
- this.attributes = [];
- this.bufferAttributes = [];
- this.varyings = [];
- this.codes = {};
- this.vars = {};
- this.flow = { code: '' };
- this.chaining = [];
- this.stack = stack();
- this.stacks = [];
- this.tab = '\t';
- this.currentFunctionNode = null;
- this.context = {
- keywords: new NodeKeywords(),
- material: this.material
- };
- this.cache = new NodeCache();
- this.globalCache = this.cache;
- this.flowsData = new WeakMap();
- this.shaderStage = null;
- this.buildStage = null;
- }
- createRenderTarget( width, height, options ) {
- return new RenderTarget( width, height, options );
- }
- createCubeRenderTarget( size, options ) {
- return new CubeRenderTarget( size, options );
- }
- createPMREMGenerator() {
- // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support
- return new PMREMGenerator( this.renderer );
- }
- includes( node ) {
- return this.nodes.includes( node );
- }
- _getSharedBindings( bindings ) {
- const shared = [];
- for ( const binding of bindings ) {
- if ( binding.shared === true ) {
- // nodes is the chainmap key
- const nodes = binding.getNodes();
- let sharedBinding = uniformsGroupCache.get( nodes );
- if ( sharedBinding === undefined ) {
- uniformsGroupCache.set( nodes, binding );
- sharedBinding = binding;
- }
- shared.push( sharedBinding );
- } else {
- shared.push( binding );
- }
- }
- return shared;
- }
- getBindings() {
- let bindingsArray = this.bindingsArray;
- if ( bindingsArray === null ) {
- const bindings = this.bindings;
- this.bindingsArray = bindingsArray = this._getSharedBindings( ( this.material !== null ) ? [ ...bindings.vertex, ...bindings.fragment ] : bindings.compute );
- }
- return bindingsArray;
- }
- setHashNode( node, hash ) {
- this.hashNodes[ hash ] = node;
- }
- addNode( node ) {
- if ( this.nodes.includes( node ) === false ) {
- this.nodes.push( node );
- this.setHashNode( node, node.getHash( this ) );
- }
- }
- buildUpdateNodes() {
- for ( const node of this.nodes ) {
- const updateType = node.getUpdateType();
- const updateBeforeType = node.getUpdateBeforeType();
- if ( updateType !== NodeUpdateType.NONE ) {
- this.updateNodes.push( node.getSelf() );
- }
- if ( updateBeforeType !== NodeUpdateType.NONE ) {
- this.updateBeforeNodes.push( node );
- }
- }
- }
- get currentNode() {
- return this.chaining[ this.chaining.length - 1 ];
- }
- addChain( node ) {
- /*
- if ( this.chaining.indexOf( node ) !== - 1 ) {
- console.warn( 'Recursive node: ', node );
- }
- */
- this.chaining.push( node );
- }
- removeChain( node ) {
- const lastChain = this.chaining.pop();
- if ( lastChain !== node ) {
- throw new Error( 'NodeBuilder: Invalid node chaining!' );
- }
- }
- getMethod( method ) {
- return method;
- }
- getNodeFromHash( hash ) {
- return this.hashNodes[ hash ];
- }
- addFlow( shaderStage, node ) {
- this.flowNodes[ shaderStage ].push( node );
- return node;
- }
- setContext( context ) {
- this.context = context;
- }
- getContext() {
- return this.context;
- }
- setCache( cache ) {
- this.cache = cache;
- }
- getCache() {
- return this.cache;
- }
- isAvailable( /*name*/ ) {
- return false;
- }
- getVertexIndex() {
- console.warn( 'Abstract function.' );
- }
- getInstanceIndex() {
- console.warn( 'Abstract function.' );
- }
- getFrontFacing() {
- console.warn( 'Abstract function.' );
- }
- getFragCoord() {
- console.warn( 'Abstract function.' );
- }
- isFlipY() {
- return false;
- }
- generateTexture( /* texture, textureProperty, uvSnippet */ ) {
- console.warn( 'Abstract function.' );
- }
- generateTextureLod( /* texture, textureProperty, uvSnippet, levelSnippet */ ) {
- console.warn( 'Abstract function.' );
- }
- generateConst( type, value = null ) {
- if ( value === null ) {
- if ( type === 'float' || type === 'int' || type === 'uint' ) value = 0;
- else if ( type === 'bool' ) value = false;
- else if ( type === 'color' ) value = new Color();
- else if ( type === 'vec2' ) value = new Vector2();
- else if ( type === 'vec3' ) value = new Vector3();
- else if ( type === 'vec4' ) value = new Vector4();
- }
- if ( type === 'float' ) return toFloat( value );
- if ( type === 'int' ) return `${ Math.round( value ) }`;
- if ( type === 'uint' ) return value >= 0 ? `${ Math.round( value ) }u` : '0u';
- if ( type === 'bool' ) return value ? 'true' : 'false';
- if ( type === 'color' ) return `${ this.getType( 'vec3' ) }( ${ toFloat( value.r ) }, ${ toFloat( value.g ) }, ${ toFloat( value.b ) } )`;
- const typeLength = this.getTypeLength( type );
- const componentType = this.getComponentType( type );
- const generateConst = value => this.generateConst( componentType, value );
- if ( typeLength === 2 ) {
- return `${ this.getType( type ) }( ${ generateConst( value.x ) }, ${ generateConst( value.y ) } )`;
- } else if ( typeLength === 3 ) {
- return `${ this.getType( type ) }( ${ generateConst( value.x ) }, ${ generateConst( value.y ) }, ${ generateConst( value.z ) } )`;
- } else if ( typeLength === 4 ) {
- return `${ this.getType( type ) }( ${ generateConst( value.x ) }, ${ generateConst( value.y ) }, ${ generateConst( value.z ) }, ${ generateConst( value.w ) } )`;
- } else if ( typeLength > 4 && value && ( value.isMatrix3 || value.isMatrix4 ) ) {
- return `${ this.getType( type ) }( ${ value.elements.map( generateConst ).join( ', ' ) } )`;
- } else if ( typeLength > 4 ) {
- return `${ this.getType( type ) }()`;
- }
- throw new Error( `NodeBuilder: Type '${type}' not found in generate constant attempt.` );
- }
- getType( type ) {
- if ( type === 'color' ) return 'vec3';
- return type;
- }
- generateMethod( method ) {
- return method;
- }
- hasGeometryAttribute( name ) {
- return this.geometry && this.geometry.getAttribute( name ) !== undefined;
- }
- getAttribute( name, type ) {
- const attributes = this.attributes;
- // find attribute
- for ( const attribute of attributes ) {
- if ( attribute.name === name ) {
- return attribute;
- }
- }
- // create a new if no exist
- const attribute = new NodeAttribute( name, type );
- attributes.push( attribute );
- return attribute;
- }
- getPropertyName( node/*, shaderStage*/ ) {
- return node.name;
- }
- isVector( type ) {
- return /vec\d/.test( type );
- }
- isMatrix( type ) {
- return /mat\d/.test( type );
- }
- isReference( type ) {
- return type === 'void' || type === 'property' || type === 'sampler' || type === 'texture' || type === 'cubeTexture' || type === 'storageTexture';
- }
- needsColorSpaceToLinear( /*texture*/ ) {
- return false;
- }
- getComponentTypeFromTexture( texture ) {
- const type = texture.type;
- if ( texture.isDataTexture ) {
- if ( type === IntType ) return 'int';
- if ( type === UnsignedIntType ) return 'uint';
- }
- return 'float';
- }
- getComponentType( type ) {
- type = this.getVectorType( type );
- if ( type === 'float' || type === 'bool' || type === 'int' || type === 'uint' ) return type;
- const componentType = /(b|i|u|)(vec|mat)([2-4])/.exec( type );
- if ( componentType === null ) return null;
- if ( componentType[ 1 ] === 'b' ) return 'bool';
- if ( componentType[ 1 ] === 'i' ) return 'int';
- if ( componentType[ 1 ] === 'u' ) return 'uint';
- return 'float';
- }
- getVectorType( type ) {
- if ( type === 'color' ) return 'vec3';
- if ( type === 'texture' || type === 'cubeTexture' || type === 'storageTexture' ) return 'vec4';
- return type;
- }
- getTypeFromLength( length, componentType = 'float' ) {
- if ( length === 1 ) return componentType;
- const baseType = typeFromLength.get( length );
- const prefix = componentType === 'float' ? '' : componentType[ 0 ];
- return prefix + baseType;
- }
- getTypeFromArray( array ) {
- return typeFromArray.get( array.constructor );
- }
- getTypeFromAttribute( attribute ) {
- let dataAttribute = attribute;
- if ( attribute.isInterleavedBufferAttribute ) dataAttribute = attribute.data;
- const array = dataAttribute.array;
- const itemSize = attribute.itemSize;
- const normalized = attribute.normalized;
- let arrayType;
- if ( ! ( attribute instanceof Float16BufferAttribute ) && normalized !== true ) {
- arrayType = this.getTypeFromArray( array );
- }
- return this.getTypeFromLength( itemSize, arrayType );
- }
- getTypeLength( type ) {
- const vecType = this.getVectorType( type );
- const vecNum = /vec([2-4])/.exec( vecType );
- if ( vecNum !== null ) return Number( vecNum[ 1 ] );
- if ( vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint' ) return 1;
- if ( /mat2/.test( type ) === true ) return 4;
- if ( /mat3/.test( type ) === true ) return 9;
- if ( /mat4/.test( type ) === true ) return 16;
- return 0;
- }
- getVectorFromMatrix( type ) {
- return type.replace( 'mat', 'vec' );
- }
- changeComponentType( type, newComponentType ) {
- return this.getTypeFromLength( this.getTypeLength( type ), newComponentType );
- }
- getIntegerType( type ) {
- const componentType = this.getComponentType( type );
- if ( componentType === 'int' || componentType === 'uint' ) return type;
- return this.changeComponentType( type, 'int' );
- }
- addStack() {
- this.stack = stack( this.stack );
- this.stacks.push( getCurrentStack() || this.stack );
- setCurrentStack( this.stack );
- return this.stack;
- }
- removeStack() {
- const lastStack = this.stack;
- this.stack = lastStack.parent;
- setCurrentStack( this.stacks.pop() );
- return lastStack;
- }
- getDataFromNode( node, shaderStage = this.shaderStage, cache = null ) {
- cache = cache === null ? ( node.isGlobal( this ) ? this.globalCache : this.cache ) : cache;
- let nodeData = cache.getNodeData( node );
- if ( nodeData === undefined ) {
- nodeData = {};
- cache.setNodeData( node, nodeData );
- }
- if ( nodeData[ shaderStage ] === undefined ) nodeData[ shaderStage ] = {};
- return nodeData[ shaderStage ];
- }
- getNodeProperties( node, shaderStage = 'any' ) {
- const nodeData = this.getDataFromNode( node, shaderStage );
- return nodeData.properties || ( nodeData.properties = { outputNode: null } );
- }
- getBufferAttributeFromNode( node, type ) {
- const nodeData = this.getDataFromNode( node );
- let bufferAttribute = nodeData.bufferAttribute;
- if ( bufferAttribute === undefined ) {
- const index = this.uniforms.index ++;
- bufferAttribute = new NodeAttribute( 'nodeAttribute' + index, type, node );
- this.bufferAttributes.push( bufferAttribute );
- nodeData.bufferAttribute = bufferAttribute;
- }
- return bufferAttribute;
- }
- getStructTypeFromNode( node, shaderStage = this.shaderStage ) {
- const nodeData = this.getDataFromNode( node, shaderStage );
- if ( nodeData.structType === undefined ) {
- const index = this.structs.index ++;
- node.name = `StructType${ index }`;
- this.structs[ shaderStage ].push( node );
- nodeData.structType = node;
- }
- return node;
- }
- getUniformFromNode( node, type, shaderStage = this.shaderStage, name = null ) {
- const nodeData = this.getDataFromNode( node, shaderStage, this.globalCache );
- let nodeUniform = nodeData.uniform;
- if ( nodeUniform === undefined ) {
- const index = this.uniforms.index ++;
- nodeUniform = new NodeUniform( name || ( 'nodeUniform' + index ), type, node );
- this.uniforms[ shaderStage ].push( nodeUniform );
- nodeData.uniform = nodeUniform;
- }
- return nodeUniform;
- }
- getVarFromNode( node, name = null, type = node.getNodeType( this ), shaderStage = this.shaderStage ) {
- const nodeData = this.getDataFromNode( node, shaderStage );
- let nodeVar = nodeData.variable;
- if ( nodeVar === undefined ) {
- const vars = this.vars[ shaderStage ] || ( this.vars[ shaderStage ] = [] );
- if ( name === null ) name = 'nodeVar' + vars.length;
- nodeVar = new NodeVar( name, type );
- vars.push( nodeVar );
- nodeData.variable = nodeVar;
- }
- return nodeVar;
- }
- getVaryingFromNode( node, name = null, type = node.getNodeType( this ) ) {
- const nodeData = this.getDataFromNode( node, 'any' );
- let nodeVarying = nodeData.varying;
- if ( nodeVarying === undefined ) {
- const varyings = this.varyings;
- const index = varyings.length;
- if ( name === null ) name = 'nodeVarying' + index;
- nodeVarying = new NodeVarying( name, type );
- varyings.push( nodeVarying );
- nodeData.varying = nodeVarying;
- }
- return nodeVarying;
- }
- getCodeFromNode( node, type, shaderStage = this.shaderStage ) {
- const nodeData = this.getDataFromNode( node );
- let nodeCode = nodeData.code;
- if ( nodeCode === undefined ) {
- const codes = this.codes[ shaderStage ] || ( this.codes[ shaderStage ] = [] );
- const index = codes.length;
- nodeCode = new NodeCode( 'nodeCode' + index, type );
- codes.push( nodeCode );
- nodeData.code = nodeCode;
- }
- return nodeCode;
- }
- addLineFlowCode( code ) {
- if ( code === '' ) return this;
- code = this.tab + code;
- if ( ! /;\s*$/.test( code ) ) {
- code = code + ';\n';
- }
- this.flow.code += code;
- return this;
- }
- addFlowCode( code ) {
- this.flow.code += code;
- return this;
- }
- addFlowTab() {
- this.tab += '\t';
- return this;
- }
- removeFlowTab() {
- this.tab = this.tab.slice( 0, - 1 );
- return this;
- }
- getFlowData( node/*, shaderStage*/ ) {
- return this.flowsData.get( node );
- }
- flowNode( node ) {
- const output = node.getNodeType( this );
- const flowData = this.flowChildNode( node, output );
- this.flowsData.set( node, flowData );
- return flowData;
- }
- buildFunctionNode( shaderNode ) {
- const fn = new FunctionNode();
- const previous = this.currentFunctionNode;
- this.currentFunctionNode = fn;
- fn.code = this.buildFunctionCode( shaderNode );
- this.currentFunctionNode = previous;
- return fn;
- }
- flowShaderNode( shaderNode ) {
- const layout = shaderNode.layout;
- let inputs;
- if ( shaderNode.isArrayInput ) {
- inputs = [];
- for ( const input of layout.inputs ) {
- inputs.push( new ParameterNode( input.type, input.name ) );
- }
- } else {
- inputs = {};
- for ( const input of layout.inputs ) {
- inputs[ input.name ] = new ParameterNode( input.type, input.name );
- }
- }
- //
- shaderNode.layout = null;
- const callNode = shaderNode.call( inputs );
- const flowData = this.flowStagesNode( callNode, layout.type );
- shaderNode.layout = layout;
- return flowData;
- }
- flowStagesNode( node, output = null ) {
- const previousFlow = this.flow;
- const previousVars = this.vars;
- const previousBuildStage = this.buildStage;
- const flow = {
- code: ''
- };
- this.flow = flow;
- this.vars = {};
- for ( const buildStage of defaultBuildStages ) {
- this.setBuildStage( buildStage );
- flow.result = node.build( this, output );
- }
- flow.vars = this.getVars( this.shaderStage );
- this.flow = previousFlow;
- this.vars = previousVars;
- this.setBuildStage( previousBuildStage );
- return flow;
- }
- getFunctionOperator() {
- return null;
- }
- flowChildNode( node, output = null ) {
- const previousFlow = this.flow;
- const flow = {
- code: ''
- };
- this.flow = flow;
- flow.result = node.build( this, output );
- this.flow = previousFlow;
- return flow;
- }
- flowNodeFromShaderStage( shaderStage, node, output = null, propertyName = null ) {
- const previousShaderStage = this.shaderStage;
- this.setShaderStage( shaderStage );
- const flowData = this.flowChildNode( node, output );
- if ( propertyName !== null ) {
- flowData.code += `${ this.tab + propertyName } = ${ flowData.result };\n`;
- }
- this.flowCode[ shaderStage ] = this.flowCode[ shaderStage ] + flowData.code;
- this.setShaderStage( previousShaderStage );
- return flowData;
- }
- getAttributesArray() {
- return this.attributes.concat( this.bufferAttributes );
- }
- getAttributes( /*shaderStage*/ ) {
- console.warn( 'Abstract function.' );
- }
- getVaryings( /*shaderStage*/ ) {
- console.warn( 'Abstract function.' );
- }
- getVar( type, name ) {
- return `${ this.getType( type ) } ${ name }`;
- }
- getVars( shaderStage ) {
- let snippet = '';
- const vars = this.vars[ shaderStage ];
- if ( vars !== undefined ) {
- for ( const variable of vars ) {
- snippet += `${ this.getVar( variable.type, variable.name ) }; `;
- }
- }
- return snippet;
- }
- getUniforms( /*shaderStage*/ ) {
- console.warn( 'Abstract function.' );
- }
- getCodes( shaderStage ) {
- const codes = this.codes[ shaderStage ];
- let code = '';
- if ( codes !== undefined ) {
- for ( const nodeCode of codes ) {
- code += nodeCode.code + '\n';
- }
- }
- return code;
- }
- getHash() {
- return this.vertexShader + this.fragmentShader + this.computeShader;
- }
- setShaderStage( shaderStage ) {
- this.shaderStage = shaderStage;
- }
- getShaderStage() {
- return this.shaderStage;
- }
- setBuildStage( buildStage ) {
- this.buildStage = buildStage;
- }
- getBuildStage() {
- return this.buildStage;
- }
- buildCode() {
- console.warn( 'Abstract function.' );
- }
- build() {
- const { object, material } = this;
- if ( material !== null ) {
- NodeMaterial.fromMaterial( material ).build( this );
- } else {
- this.addFlow( 'compute', object );
- }
- // setup() -> stage 1: create possible new nodes and returns an output reference node
- // analyze() -> stage 2: analyze nodes to possible optimization and validation
- // generate() -> stage 3: generate shader
- for ( const buildStage of defaultBuildStages ) {
- this.setBuildStage( buildStage );
- if ( this.context.vertex && this.context.vertex.isNode ) {
- this.flowNodeFromShaderStage( 'vertex', this.context.vertex );
- }
- for ( const shaderStage of shaderStages ) {
- this.setShaderStage( shaderStage );
- const flowNodes = this.flowNodes[ shaderStage ];
- for ( const node of flowNodes ) {
- if ( buildStage === 'generate' ) {
- this.flowNode( node );
- } else {
- node.build( this );
- }
- }
- }
- }
- this.setBuildStage( null );
- this.setShaderStage( null );
- // stage 4: build code for a specific output
- this.buildCode();
- this.buildUpdateNodes();
- return this;
- }
- getNodeUniform( uniformNode, type ) {
- if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
- if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
- if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
- if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
- if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
- if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
- if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
- throw new Error( `Uniform "${type}" not declared.` );
- }
- createNodeMaterial( type = 'NodeMaterial' ) {
- // TODO: Move Materials.js to outside of the Nodes.js in order to remove this function and improve tree-shaking support
- return createNodeMaterialFromType( type );
- }
- format( snippet, fromType, toType ) {
- fromType = this.getVectorType( fromType );
- toType = this.getVectorType( toType );
- if ( fromType === toType || toType === null || this.isReference( toType ) ) {
- return snippet;
- }
- const fromTypeLength = this.getTypeLength( fromType );
- const toTypeLength = this.getTypeLength( toType );
- if ( fromTypeLength > 4 ) { // fromType is matrix-like
- // @TODO: ignore for now
- return snippet;
- }
- if ( toTypeLength > 4 || toTypeLength === 0 ) { // toType is matrix-like or unknown
- // @TODO: ignore for now
- return snippet;
- }
- if ( fromTypeLength === toTypeLength ) {
- return `${ this.getType( toType ) }( ${ snippet } )`;
- }
- if ( fromTypeLength > toTypeLength ) {
- return this.format( `${ snippet }.${ 'xyz'.slice( 0, toTypeLength ) }`, this.getTypeFromLength( toTypeLength, this.getComponentType( fromType ) ), toType );
- }
- if ( toTypeLength === 4 && fromTypeLength > 1 ) { // toType is vec4-like
- return `${ this.getType( toType ) }( ${ this.format( snippet, fromType, 'vec3' ) }, 1.0 )`;
- }
- if ( fromTypeLength === 2 ) { // fromType is vec2-like and toType is vec3-like
- return `${ this.getType( toType ) }( ${ this.format( snippet, fromType, 'vec2' ) }, 0.0 )`;
- }
- if ( fromTypeLength === 1 && toTypeLength > 1 && fromType[ 0 ] !== toType[ 0 ] ) { // fromType is float-like
- // convert a number value to vector type, e.g:
- // vec3( 1u ) -> vec3( float( 1u ) )
- snippet = `${ this.getType( this.getComponentType( toType ) ) }( ${ snippet } )`;
- }
- return `${ this.getType( toType ) }( ${ snippet } )`; // fromType is float-like
- }
- getSignature() {
- return `// Three.js r${ REVISION } - NodeMaterial System\n`;
- }
- }
- export default NodeBuilder;
|