NodeBuilder.js 18 KB

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