NodeBuilder.js 20 KB

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