NodeBuilder.js 18 KB

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