NodeBuilder.js 18 KB

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