NodeBuilder.js 18 KB

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