NodeBuilder.js 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. import NodeUniform from './NodeUniform.js';
  2. import NodeAttribute from './NodeAttribute.js';
  3. import NodeVarying from './NodeVarying.js';
  4. import NodeVar from './NodeVar.js';
  5. import NodeCode from './NodeCode.js';
  6. import NodeKeywords from './NodeKeywords.js';
  7. import NodeCache from './NodeCache.js';
  8. import { createNodeMaterialFromType } from '../materials/NodeMaterial.js';
  9. import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js';
  10. import {
  11. FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
  12. ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
  13. } from '../../renderers/common/nodes/NodeUniform.js';
  14. import { REVISION, RenderTarget, NoColorSpace, LinearEncoding, sRGBEncoding, SRGBColorSpace, Color, Vector2, Vector3, Vector4, Float16BufferAttribute } from 'three';
  15. import { stack } from './StackNode.js';
  16. import { maxMipLevel } from '../utils/MaxMipLevelNode.js';
  17. import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js';
  18. const typeFromLength = new Map( [
  19. [ 2, 'vec2' ],
  20. [ 3, 'vec3' ],
  21. [ 4, 'vec4' ],
  22. [ 9, 'mat3' ],
  23. [ 16, 'mat4' ]
  24. ] );
  25. const typeFromArray = new Map( [
  26. [ Int8Array, 'int' ],
  27. [ Int16Array, 'int' ],
  28. [ Int32Array, 'int' ],
  29. [ Uint8Array, 'uint' ],
  30. [ Uint16Array, 'uint' ],
  31. [ Uint32Array, 'uint' ],
  32. [ Float32Array, 'float' ]
  33. ] );
  34. const isNonPaddingElementArray = new Set( [ Int32Array, Uint32Array, Float32Array ] );
  35. const toFloat = ( value ) => {
  36. value = Number( value );
  37. return value + ( value % 1 ? '' : '.0' );
  38. };
  39. class NodeBuilder {
  40. constructor( object, renderer, parser, scene = null ) {
  41. this.object = object;
  42. this.material = ( object && object.material ) || null;
  43. this.geometry = ( object && object.geometry ) || null;
  44. this.renderer = renderer;
  45. this.parser = parser;
  46. this.scene = scene;
  47. this.nodes = [];
  48. this.updateNodes = [];
  49. this.updateBeforeNodes = [];
  50. this.hashNodes = {};
  51. this.lightsNode = null;
  52. this.environmentNode = null;
  53. this.fogNode = null;
  54. this.toneMappingNode = null;
  55. this.vertexShader = null;
  56. this.fragmentShader = null;
  57. this.computeShader = null;
  58. this.flowNodes = { vertex: [], fragment: [], compute: [] };
  59. this.flowCode = { vertex: '', fragment: '', compute: [] };
  60. this.uniforms = { vertex: [], fragment: [], compute: [], index: 0 };
  61. this.structs = { vertex: [], fragment: [], compute: [], index: 0 };
  62. this.codes = { vertex: [], fragment: [], compute: [] };
  63. this.bindings = { vertex: [], fragment: [], compute: [] };
  64. this.bindingsOffset = { vertex: 0, fragment: 0, compute: 0 };
  65. this.bindingsArray = null;
  66. this.attributes = [];
  67. this.bufferAttributes = [];
  68. this.varyings = [];
  69. this.vars = { vertex: [], fragment: [], compute: [] };
  70. this.flow = { code: '' };
  71. this.chaining = [];
  72. this.stack = stack();
  73. this.tab = '\t';
  74. this.context = {
  75. keywords: new NodeKeywords(),
  76. material: this.material,
  77. getMIPLevelAlgorithmNode: ( textureNode, levelNode ) => levelNode.mul( maxMipLevel( textureNode ) )
  78. };
  79. this.cache = new NodeCache();
  80. this.globalCache = this.cache;
  81. this.flowsData = new WeakMap();
  82. this.shaderStage = null;
  83. this.buildStage = null;
  84. }
  85. getRenderTarget( width, height, options ) {
  86. return new RenderTarget( width, height, options );
  87. }
  88. getCubeRenderTarget( size, options ) {
  89. return new CubeRenderTarget( size, options );
  90. }
  91. includes( node ) {
  92. return this.nodes.includes( node );
  93. }
  94. getBindings() {
  95. let bindingsArray = this.bindingsArray;
  96. if ( bindingsArray === null ) {
  97. const bindings = this.bindings;
  98. this.bindingsArray = bindingsArray = ( this.material !== null ) ? [ ...bindings.vertex, ...bindings.fragment ] : bindings.compute;
  99. }
  100. return bindingsArray;
  101. }
  102. setHashNode( node, hash ) {
  103. this.hashNodes[ hash ] = node;
  104. }
  105. addNode( node ) {
  106. if ( this.nodes.includes( node ) === false ) {
  107. this.nodes.push( node );
  108. this.setHashNode( node, node.getHash( this ) );
  109. }
  110. }
  111. buildUpdateNodes() {
  112. for ( const node of this.nodes ) {
  113. const updateType = node.getUpdateType();
  114. const updateBeforeType = node.getUpdateBeforeType();
  115. if ( updateType !== NodeUpdateType.NONE ) {
  116. this.updateNodes.push( node.getSelf() );
  117. }
  118. if ( updateBeforeType !== NodeUpdateType.NONE ) {
  119. this.updateBeforeNodes.push( node );
  120. }
  121. }
  122. }
  123. get currentNode() {
  124. return this.chaining[ this.chaining.length - 1 ];
  125. }
  126. addChain( node ) {
  127. /*
  128. if ( this.chaining.indexOf( node ) !== - 1 ) {
  129. console.warn( 'Recursive node: ', node );
  130. }
  131. */
  132. this.chaining.push( node );
  133. }
  134. removeChain( node ) {
  135. const lastChain = this.chaining.pop();
  136. if ( lastChain !== node ) {
  137. throw new Error( 'NodeBuilder: Invalid node chaining!' );
  138. }
  139. }
  140. getMethod( method ) {
  141. return method;
  142. }
  143. getNodeFromHash( hash ) {
  144. return this.hashNodes[ hash ];
  145. }
  146. addFlow( shaderStage, node ) {
  147. this.flowNodes[ shaderStage ].push( node );
  148. return node;
  149. }
  150. setContext( context ) {
  151. this.context = context;
  152. }
  153. getContext() {
  154. return this.context;
  155. }
  156. setCache( cache ) {
  157. this.cache = cache;
  158. }
  159. getCache() {
  160. return this.cache;
  161. }
  162. isAvailable( /*name*/ ) {
  163. return false;
  164. }
  165. getVertexIndex() {
  166. console.warn( 'Abstract function.' );
  167. }
  168. getInstanceIndex() {
  169. console.warn( 'Abstract function.' );
  170. }
  171. getFrontFacing() {
  172. console.warn( 'Abstract function.' );
  173. }
  174. getFragCoord() {
  175. console.warn( 'Abstract function.' );
  176. }
  177. isFlipY() {
  178. return false;
  179. }
  180. getTexture( /* texture, textureProperty, uvSnippet */ ) {
  181. console.warn( 'Abstract function.' );
  182. }
  183. getTextureLevel( /* texture, textureProperty, uvSnippet, levelSnippet */ ) {
  184. console.warn( 'Abstract function.' );
  185. }
  186. // @TODO: rename to .generateConst()
  187. getConst( type, value = null ) {
  188. if ( value === null ) {
  189. if ( type === 'float' || type === 'int' || type === 'uint' ) value = 0;
  190. else if ( type === 'bool' ) value = false;
  191. else if ( type === 'color' ) value = new Color();
  192. else if ( type === 'vec2' ) value = new Vector2();
  193. else if ( type === 'vec3' ) value = new Vector3();
  194. else if ( type === 'vec4' ) value = new Vector4();
  195. }
  196. if ( type === 'float' ) return toFloat( value );
  197. if ( type === 'int' ) return `${ Math.round( value ) }`;
  198. if ( type === 'uint' ) return value >= 0 ? `${ Math.round( value ) }u` : '0u';
  199. if ( type === 'bool' ) return value ? 'true' : 'false';
  200. if ( type === 'color' ) return `${ this.getType( 'vec3' ) }( ${ toFloat( value.r ) }, ${ toFloat( value.g ) }, ${ toFloat( value.b ) } )`;
  201. const typeLength = this.getTypeLength( type );
  202. const componentType = this.getComponentType( type );
  203. const getConst = value => this.getConst( componentType, value );
  204. if ( typeLength === 2 ) {
  205. return `${ this.getType( type ) }( ${ getConst( value.x ) }, ${ getConst( value.y ) } )`;
  206. } else if ( typeLength === 3 ) {
  207. return `${ this.getType( type ) }( ${ getConst( value.x ) }, ${ getConst( value.y ) }, ${ getConst( value.z ) } )`;
  208. } else if ( typeLength === 4 ) {
  209. return `${ this.getType( type ) }( ${ getConst( value.x ) }, ${ getConst( value.y ) }, ${ getConst( value.z ) }, ${ getConst( value.w ) } )`;
  210. } else if ( typeLength > 4 && value && ( value.isMatrix3 || value.isMatrix4 ) ) {
  211. return `${ this.getType( type ) }( ${ value.elements.map( getConst ).join( ', ' ) } )`;
  212. } else if ( typeLength > 4 ) {
  213. return `${ this.getType( type ) }()`;
  214. }
  215. throw new Error( `NodeBuilder: Type '${type}' not found in generate constant attempt.` );
  216. }
  217. getType( type ) {
  218. if ( type === 'color' ) return 'vec3';
  219. return type;
  220. }
  221. generateMethod( method ) {
  222. return method;
  223. }
  224. hasGeometryAttribute( name ) {
  225. return this.geometry && this.geometry.getAttribute( name ) !== undefined;
  226. }
  227. getAttribute( name, type ) {
  228. const attributes = this.attributes;
  229. // find attribute
  230. for ( const attribute of attributes ) {
  231. if ( attribute.name === name ) {
  232. return attribute;
  233. }
  234. }
  235. // create a new if no exist
  236. const attribute = new NodeAttribute( name, type );
  237. attributes.push( attribute );
  238. return attribute;
  239. }
  240. getPropertyName( node/*, shaderStage*/ ) {
  241. return node.name;
  242. }
  243. isVector( type ) {
  244. return /vec\d/.test( type );
  245. }
  246. isMatrix( type ) {
  247. return /mat\d/.test( type );
  248. }
  249. isReference( type ) {
  250. return type === 'void' || type === 'property' || type === 'sampler' || type === 'texture' || type === 'cubeTexture';
  251. }
  252. needsColorSpaceToLinear( /*texture*/ ) {
  253. return false;
  254. }
  255. /** @deprecated, r152 */
  256. getTextureEncodingFromMap( map ) {
  257. console.warn( 'THREE.NodeBuilder: Method .getTextureEncodingFromMap replaced by .getTextureColorSpaceFromMap in r152+.' );
  258. return this.getTextureColorSpaceFromMap( map ) === SRGBColorSpace ? sRGBEncoding : LinearEncoding;
  259. }
  260. getTextureColorSpaceFromMap( map ) {
  261. let colorSpace;
  262. if ( map && map.isTexture ) {
  263. colorSpace = map.colorSpace;
  264. } else if ( map && map.isWebGLRenderTarget ) {
  265. colorSpace = map.texture.colorSpace;
  266. } else {
  267. colorSpace = NoColorSpace;
  268. }
  269. return colorSpace;
  270. }
  271. getComponentType( type ) {
  272. type = this.getVectorType( type );
  273. if ( type === 'float' || type === 'bool' || type === 'int' || type === 'uint' ) return type;
  274. const componentType = /(b|i|u|)(vec|mat)([2-4])/.exec( type );
  275. if ( componentType === null ) return null;
  276. if ( componentType[ 1 ] === 'b' ) return 'bool';
  277. if ( componentType[ 1 ] === 'i' ) return 'int';
  278. if ( componentType[ 1 ] === 'u' ) return 'uint';
  279. return 'float';
  280. }
  281. getVectorType( type ) {
  282. if ( type === 'color' ) return 'vec3';
  283. if ( type === 'texture' ) return 'vec4';
  284. return type;
  285. }
  286. getTypeFromLength( length, componentType = 'float' ) {
  287. if ( length === 1 ) return componentType;
  288. const baseType = typeFromLength.get( length );
  289. const prefix = componentType === 'float' ? '' : componentType[ 0 ];
  290. return prefix + baseType;
  291. }
  292. getTypeFromArray( array ) {
  293. return typeFromArray.get( array.constructor );
  294. }
  295. getTypeFromAttribute( attribute ) {
  296. let dataAttribute = attribute;
  297. if ( attribute.isInterleavedBufferAttribute ) dataAttribute = attribute.data;
  298. const array = dataAttribute.array;
  299. const itemSize = isNonPaddingElementArray.has( array.constructor ) ? attribute.itemSize : dataAttribute.stride || attribute.itemSize;
  300. const normalized = attribute.normalized;
  301. let arrayType;
  302. if ( ! ( attribute instanceof Float16BufferAttribute ) && normalized !== true ) {
  303. arrayType = this.getTypeFromArray( array );
  304. }
  305. return this.getTypeFromLength( itemSize, arrayType );
  306. }
  307. getTypeLength( type ) {
  308. const vecType = this.getVectorType( type );
  309. const vecNum = /vec([2-4])/.exec( vecType );
  310. if ( vecNum !== null ) return Number( vecNum[ 1 ] );
  311. if ( vecType === 'float' || vecType === 'bool' || vecType === 'int' || vecType === 'uint' ) return 1;
  312. if ( /mat3/.test( type ) === true ) return 9;
  313. if ( /mat4/.test( type ) === true ) return 16;
  314. return 0;
  315. }
  316. getVectorFromMatrix( type ) {
  317. return type.replace( 'mat', 'vec' );
  318. }
  319. changeComponentType( type, newComponentType ) {
  320. return this.getTypeFromLength( this.getTypeLength( type ), newComponentType );
  321. }
  322. getIntegerType( type ) {
  323. const componentType = this.getComponentType( type );
  324. if ( componentType === 'int' || componentType === 'uint' ) return type;
  325. return this.changeComponentType( type, 'int' );
  326. }
  327. addStack() {
  328. this.stack = stack( this.stack );
  329. return this.stack;
  330. }
  331. removeStack() {
  332. const currentStack = this.stack;
  333. this.stack = currentStack.parent;
  334. return currentStack;
  335. }
  336. getDataFromNode( node, shaderStage = this.shaderStage ) {
  337. const cache = node.isGlobal( this ) ? this.globalCache : this.cache;
  338. let nodeData = cache.getNodeData( node );
  339. if ( nodeData === undefined ) {
  340. nodeData = {};
  341. cache.setNodeData( node, nodeData );
  342. }
  343. if ( nodeData[ shaderStage ] === undefined ) nodeData[ shaderStage ] = {};
  344. return nodeData[ shaderStage ];
  345. }
  346. getNodeProperties( node, shaderStage = 'any' ) {
  347. const nodeData = this.getDataFromNode( node, shaderStage );
  348. return nodeData.properties || ( nodeData.properties = { outputNode: null } );
  349. }
  350. getBufferAttributeFromNode( node, type ) {
  351. const nodeData = this.getDataFromNode( node );
  352. let bufferAttribute = nodeData.bufferAttribute;
  353. if ( bufferAttribute === undefined ) {
  354. const index = this.uniforms.index ++;
  355. bufferAttribute = new NodeAttribute( 'nodeAttribute' + index, type, node );
  356. this.bufferAttributes.push( bufferAttribute );
  357. nodeData.bufferAttribute = bufferAttribute;
  358. }
  359. return bufferAttribute;
  360. }
  361. getStructTypeFromNode( node, shaderStage = this.shaderStage, name = null ) {
  362. const nodeData = this.getDataFromNode( node, shaderStage );
  363. let nodeStruct = nodeData.structType;
  364. if ( nodeStruct === undefined ) {
  365. const index = this.structs.index ++;
  366. node.name = `StructType${index}`;
  367. this.structs[ shaderStage ].push( node );
  368. nodeData.structType = node;
  369. }
  370. return node;
  371. }
  372. getUniformFromNode( node, type, shaderStage = this.shaderStage, name = null ) {
  373. const nodeData = this.getDataFromNode( node, shaderStage );
  374. let nodeUniform = nodeData.uniform;
  375. if ( nodeUniform === undefined ) {
  376. const index = this.uniforms.index ++;
  377. nodeUniform = new NodeUniform( name || ( 'nodeUniform' + index ), type, node );
  378. this.uniforms[ shaderStage ].push( nodeUniform );
  379. nodeData.uniform = nodeUniform;
  380. }
  381. return nodeUniform;
  382. }
  383. getVarFromNode( node, type, shaderStage = this.shaderStage ) {
  384. const nodeData = this.getDataFromNode( node, shaderStage );
  385. let nodeVar = nodeData.variable;
  386. if ( nodeVar === undefined ) {
  387. const vars = this.vars[ shaderStage ];
  388. const index = vars.length;
  389. nodeVar = new NodeVar( 'nodeVar' + index, type );
  390. vars.push( nodeVar );
  391. nodeData.variable = nodeVar;
  392. }
  393. return nodeVar;
  394. }
  395. getVaryingFromNode( node, type ) {
  396. const nodeData = this.getDataFromNode( node, 'any' );
  397. let nodeVarying = nodeData.varying;
  398. if ( nodeVarying === undefined ) {
  399. const varyings = this.varyings;
  400. const index = varyings.length;
  401. nodeVarying = new NodeVarying( 'nodeVarying' + index, type );
  402. varyings.push( nodeVarying );
  403. nodeData.varying = nodeVarying;
  404. }
  405. return nodeVarying;
  406. }
  407. getCodeFromNode( node, type, shaderStage = this.shaderStage ) {
  408. const nodeData = this.getDataFromNode( node );
  409. let nodeCode = nodeData.code;
  410. if ( nodeCode === undefined ) {
  411. const codes = this.codes[ shaderStage ];
  412. const index = codes.length;
  413. nodeCode = new NodeCode( 'nodeCode' + index, type );
  414. codes.push( nodeCode );
  415. nodeData.code = nodeCode;
  416. }
  417. return nodeCode;
  418. }
  419. addLineFlowCode( code ) {
  420. if ( code === '' ) return this;
  421. code = this.tab + code;
  422. if ( ! /;\s*$/.test( code ) ) {
  423. code = code + ';\n';
  424. }
  425. this.flow.code += code;
  426. return this;
  427. }
  428. addFlowCode( code ) {
  429. this.flow.code += code;
  430. return this;
  431. }
  432. addFlowTab() {
  433. this.tab += '\t';
  434. return this;
  435. }
  436. removeFlowTab() {
  437. this.tab = this.tab.slice( 0, - 1 );
  438. return this;
  439. }
  440. getFlowData( node/*, shaderStage*/ ) {
  441. return this.flowsData.get( node );
  442. }
  443. flowNode( node ) {
  444. const output = node.getNodeType( this );
  445. const flowData = this.flowChildNode( node, output );
  446. this.flowsData.set( node, flowData );
  447. return flowData;
  448. }
  449. flowChildNode( node, output = null ) {
  450. const previousFlow = this.flow;
  451. const flow = {
  452. code: '',
  453. };
  454. this.flow = flow;
  455. flow.result = node.build( this, output );
  456. this.flow = previousFlow;
  457. return flow;
  458. }
  459. flowNodeFromShaderStage( shaderStage, node, output = null, propertyName = null ) {
  460. const previousShaderStage = this.shaderStage;
  461. this.setShaderStage( shaderStage );
  462. const flowData = this.flowChildNode( node, output );
  463. if ( propertyName !== null ) {
  464. flowData.code += `${ this.tab + propertyName } = ${ flowData.result };\n`;
  465. }
  466. this.flowCode[ shaderStage ] = this.flowCode[ shaderStage ] + flowData.code;
  467. this.setShaderStage( previousShaderStage );
  468. return flowData;
  469. }
  470. getAttributesArray() {
  471. return this.attributes.concat( this.bufferAttributes );
  472. }
  473. getAttributes( /*shaderStage*/ ) {
  474. console.warn( 'Abstract function.' );
  475. }
  476. getVaryings( /*shaderStage*/ ) {
  477. console.warn( 'Abstract function.' );
  478. }
  479. getVar( type, name ) {
  480. return `${ this.getType( type ) } ${ name }`;
  481. }
  482. getVars( shaderStage ) {
  483. let snippet = '';
  484. const vars = this.vars[ shaderStage ];
  485. for ( const variable of vars ) {
  486. snippet += `${ this.getVar( variable.type, variable.name ) }; `;
  487. }
  488. return snippet;
  489. }
  490. getUniforms( /*shaderStage*/ ) {
  491. console.warn( 'Abstract function.' );
  492. }
  493. getCodes( shaderStage ) {
  494. const codes = this.codes[ shaderStage ];
  495. let code = '';
  496. for ( const nodeCode of codes ) {
  497. code += nodeCode.code + '\n';
  498. }
  499. return code;
  500. }
  501. getHash() {
  502. return this.vertexShader + this.fragmentShader + this.computeShader;
  503. }
  504. setShaderStage( shaderStage ) {
  505. this.shaderStage = shaderStage;
  506. }
  507. getShaderStage() {
  508. return this.shaderStage;
  509. }
  510. setBuildStage( buildStage ) {
  511. this.buildStage = buildStage;
  512. }
  513. getBuildStage() {
  514. return this.buildStage;
  515. }
  516. buildCode() {
  517. console.warn( 'Abstract function.' );
  518. }
  519. build() {
  520. // setup() -> stage 1: create possible new nodes and returns an output reference node
  521. // analyze() -> stage 2: analyze nodes to possible optimization and validation
  522. // generate() -> stage 3: generate shader
  523. for ( const buildStage of defaultBuildStages ) {
  524. this.setBuildStage( buildStage );
  525. if ( this.context.vertex && this.context.vertex.isNode ) {
  526. this.flowNodeFromShaderStage( 'vertex', this.context.vertex );
  527. }
  528. for ( const shaderStage of shaderStages ) {
  529. this.setShaderStage( shaderStage );
  530. const flowNodes = this.flowNodes[ shaderStage ];
  531. for ( const node of flowNodes ) {
  532. if ( buildStage === 'generate' ) {
  533. this.flowNode( node );
  534. } else {
  535. node.build( this );
  536. }
  537. }
  538. }
  539. }
  540. this.setBuildStage( null );
  541. this.setShaderStage( null );
  542. // stage 4: build code for a specific output
  543. this.buildCode();
  544. this.buildUpdateNodes();
  545. return this;
  546. }
  547. getNodeUniform( uniformNode, type ) {
  548. if ( type === 'float' ) return new FloatNodeUniform( uniformNode );
  549. if ( type === 'vec2' ) return new Vector2NodeUniform( uniformNode );
  550. if ( type === 'vec3' ) return new Vector3NodeUniform( uniformNode );
  551. if ( type === 'vec4' ) return new Vector4NodeUniform( uniformNode );
  552. if ( type === 'color' ) return new ColorNodeUniform( uniformNode );
  553. if ( type === 'mat3' ) return new Matrix3NodeUniform( uniformNode );
  554. if ( type === 'mat4' ) return new Matrix4NodeUniform( uniformNode );
  555. throw new Error( `Uniform "${type}" not declared.` );
  556. }
  557. createNodeMaterial( type ) {
  558. return createNodeMaterialFromType( type );
  559. }
  560. format( snippet, fromType, toType ) {
  561. fromType = this.getVectorType( fromType );
  562. toType = this.getVectorType( toType );
  563. if ( fromType === toType || toType === null || this.isReference( toType ) ) {
  564. return snippet;
  565. }
  566. const fromTypeLength = this.getTypeLength( fromType );
  567. const toTypeLength = this.getTypeLength( toType );
  568. if ( fromTypeLength > 4 ) { // fromType is matrix-like
  569. // @TODO: ignore for now
  570. return snippet;
  571. }
  572. if ( toTypeLength > 4 || toTypeLength === 0 ) { // toType is matrix-like or unknown
  573. // @TODO: ignore for now
  574. return snippet;
  575. }
  576. if ( fromTypeLength === toTypeLength ) {
  577. return `${ this.getType( toType ) }( ${ snippet } )`;
  578. }
  579. if ( fromTypeLength > toTypeLength ) {
  580. return this.format( `${ snippet }.${ 'xyz'.slice( 0, toTypeLength ) }`, this.getTypeFromLength( toTypeLength, this.getComponentType( fromType ) ), toType );
  581. }
  582. if ( toTypeLength === 4 ) { // toType is vec4-like
  583. return `${ this.getType( toType ) }( ${ this.format( snippet, fromType, 'vec3' ) }, 1.0 )`;
  584. }
  585. if ( fromTypeLength === 2 ) { // fromType is vec2-like and toType is vec3-like
  586. return `${ this.getType( toType ) }( ${ this.format( snippet, fromType, 'vec2' ) }, 0.0 )`;
  587. }
  588. return `${ this.getType( toType ) }( ${ snippet } )`; // fromType is float-like
  589. }
  590. getSignature() {
  591. return `// Three.js r${ REVISION } - NodeMaterial System\n`;
  592. }
  593. }
  594. export default NodeBuilder;