NodeBuilder.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. import {
  2. CubeReflectionMapping,
  3. CubeRefractionMapping,
  4. CubeUVReflectionMapping,
  5. CubeUVRefractionMapping,
  6. LinearEncoding,
  7. GammaEncoding
  8. } from '../../../../build/three.module.js';
  9. import { NodeUniform } from './NodeUniform.js';
  10. import { NodeUtils } from './NodeUtils.js';
  11. import { NodeLib } from './NodeLib.js';
  12. import { FunctionNode } from './FunctionNode.js';
  13. import { ConstNode } from './ConstNode.js';
  14. import { StructNode } from './StructNode.js';
  15. import { Vector2Node } from '../inputs/Vector2Node.js';
  16. import { Vector3Node } from '../inputs/Vector3Node.js';
  17. import { Vector4Node } from '../inputs/Vector4Node.js';
  18. import { TextureNode } from '../inputs/TextureNode.js';
  19. import { CubeTextureNode } from '../inputs/CubeTextureNode.js';
  20. import { TextureCubeNode } from '../misc/TextureCubeNode.js';
  21. const elements = NodeUtils.elements,
  22. constructors = [ 'float', 'vec2', 'vec3', 'vec4' ],
  23. convertFormatToType = {
  24. float: 'f',
  25. vec2: 'v2',
  26. vec3: 'v3',
  27. vec4: 'v4',
  28. mat4: 'v4',
  29. int: 'i',
  30. bool: 'b'
  31. },
  32. convertTypeToFormat = {
  33. t: 'sampler2D',
  34. tc: 'samplerCube',
  35. b: 'bool',
  36. i: 'int',
  37. f: 'float',
  38. c: 'vec3',
  39. v2: 'vec2',
  40. v3: 'vec3',
  41. v4: 'vec4',
  42. m3: 'mat3',
  43. m4: 'mat4'
  44. };
  45. class NodeBuilder {
  46. constructor() {
  47. this.slots = [];
  48. this.caches = [];
  49. this.contexts = [];
  50. this.keywords = {};
  51. this.nodeData = {};
  52. this.requires = {
  53. uv: [],
  54. color: [],
  55. lights: false,
  56. fog: false,
  57. transparent: false,
  58. irradiance: false
  59. };
  60. this.includes = {
  61. consts: [],
  62. functions: [],
  63. structs: []
  64. };
  65. this.attributes = {};
  66. this.prefixCode = [
  67. '#ifdef TEXTURE_LOD_EXT',
  68. ' #define texCube(a, b) textureCube(a, b)',
  69. ' #define texCubeBias(a, b, c) textureCubeLodEXT(a, b, c)',
  70. ' #define tex2D(a, b) texture2D(a, b)',
  71. ' #define tex2DBias(a, b, c) texture2DLodEXT(a, b, c)',
  72. '#else',
  73. ' #define texCube(a, b) textureCube(a, b)',
  74. ' #define texCubeBias(a, b, c) textureCube(a, b, c)',
  75. ' #define tex2D(a, b) texture2D(a, b)',
  76. ' #define tex2DBias(a, b, c) texture2D(a, b, c)',
  77. '#endif',
  78. '#include <packing>',
  79. '#include <common>'
  80. ].join( '\n' );
  81. this.parsCode = {
  82. vertex: '',
  83. fragment: ''
  84. };
  85. this.code = {
  86. vertex: '',
  87. fragment: ''
  88. };
  89. this.nodeCode = {
  90. vertex: '',
  91. fragment: ''
  92. };
  93. this.resultCode = {
  94. vertex: '',
  95. fragment: ''
  96. };
  97. this.finalCode = {
  98. vertex: '',
  99. fragment: ''
  100. };
  101. this.inputs = {
  102. uniforms: {
  103. list: [],
  104. vertex: [],
  105. fragment: []
  106. },
  107. vars: {
  108. varying: [],
  109. vertex: [],
  110. fragment: []
  111. }
  112. };
  113. // send to material
  114. this.defines = {};
  115. this.uniforms = {};
  116. this.extensions = {};
  117. this.updaters = [];
  118. this.nodes = [];
  119. // --
  120. this.analyzing = false;
  121. }
  122. build( vertex, fragment ) {
  123. this.buildShader( 'vertex', vertex );
  124. this.buildShader( 'fragment', fragment );
  125. for ( let i = 0; i < this.requires.uv.length; i ++ ) {
  126. if ( this.requires.uv[ i ] ) {
  127. const uvIndex = i > 0 ? i + 1 : '';
  128. this.addVaryCode( 'varying vec2 vUv' + uvIndex + ';' );
  129. if ( i > 0 ) {
  130. this.addVertexParsCode( 'attribute vec2 uv' + uvIndex + ';' );
  131. }
  132. this.addVertexFinalCode( 'vUv' + uvIndex + ' = uv' + uvIndex + ';' );
  133. }
  134. }
  135. if ( this.requires.color[ 0 ] ) {
  136. this.addVaryCode( 'varying vec4 vColor;' );
  137. this.addVertexParsCode( 'attribute vec4 color;' );
  138. this.addVertexFinalCode( 'vColor = color;' );
  139. }
  140. if ( this.requires.color[ 1 ] ) {
  141. this.addVaryCode( 'varying vec4 vColor2;' );
  142. this.addVertexParsCode( 'attribute vec4 color2;' );
  143. this.addVertexFinalCode( 'vColor2 = color2;' );
  144. }
  145. if ( this.requires.position ) {
  146. this.addVaryCode( 'varying vec3 vPosition;' );
  147. this.addVertexFinalCode( 'vPosition = transformed;' );
  148. }
  149. if ( this.requires.worldPosition ) {
  150. this.addVaryCode( 'varying vec3 vWPosition;' );
  151. this.addVertexFinalCode( 'vWPosition = ( modelMatrix * vec4( transformed, 1.0 ) ).xyz;' );
  152. }
  153. if ( this.requires.normal ) {
  154. this.addVaryCode( 'varying vec3 vObjectNormal;' );
  155. this.addVertexFinalCode( 'vObjectNormal = normal;' );
  156. }
  157. if ( this.requires.worldNormal ) {
  158. this.addVaryCode( 'varying vec3 vWNormal;' );
  159. this.addVertexFinalCode( 'vWNormal = inverseTransformDirection( transformedNormal, viewMatrix ).xyz;' );
  160. }
  161. return this;
  162. }
  163. buildShader( shader, node ) {
  164. this.resultCode[ shader ] = node.build( this.setShader( shader ), 'v4' );
  165. }
  166. setMaterial( material, renderer ) {
  167. this.material = material;
  168. this.renderer = renderer;
  169. this.requires.lights = material.lights;
  170. this.requires.fog = material.fog;
  171. this.mergeDefines( material.defines );
  172. return this;
  173. }
  174. addFlow( slot, cache, context ) {
  175. return this.addSlot( slot ).addCache( cache ).addContext( context );
  176. }
  177. removeFlow() {
  178. return this.removeSlot().removeCache().removeContext();
  179. }
  180. addCache( name ) {
  181. this.cache = name || '';
  182. this.caches.push( this.cache );
  183. return this;
  184. }
  185. removeCache() {
  186. this.caches.pop();
  187. this.cache = this.caches[ this.caches.length - 1 ] || '';
  188. return this;
  189. }
  190. addContext( context ) {
  191. this.context = Object.assign( {}, this.context, context );
  192. this.context.extra = this.context.extra || {};
  193. this.contexts.push( this.context );
  194. return this;
  195. }
  196. removeContext() {
  197. this.contexts.pop();
  198. this.context = this.contexts[ this.contexts.length - 1 ] || {};
  199. return this;
  200. }
  201. addSlot( name = '' ) {
  202. this.slot = name;
  203. this.slots.push( this.slot );
  204. return this;
  205. }
  206. removeSlot() {
  207. this.slots.pop();
  208. this.slot = this.slots[ this.slots.length - 1 ] || '';
  209. return this;
  210. }
  211. addVertexCode( code ) {
  212. this.addCode( code, 'vertex' );
  213. }
  214. addFragmentCode( code ) {
  215. this.addCode( code, 'fragment' );
  216. }
  217. addCode( code, shader ) {
  218. this.code[ shader || this.shader ] += code + '\n';
  219. }
  220. addVertexNodeCode( code ) {
  221. this.addNodeCode( code, 'vertex' );
  222. }
  223. addFragmentNodeCode( code ) {
  224. this.addNodeCode( code, 'fragment' );
  225. }
  226. addNodeCode( code, shader ) {
  227. this.nodeCode[ shader || this.shader ] += code + '\n';
  228. }
  229. clearNodeCode( shader ) {
  230. shader = shader || this.shader;
  231. const code = this.nodeCode[ shader ];
  232. this.nodeCode[ shader ] = '';
  233. return code;
  234. }
  235. clearVertexNodeCode( ) {
  236. return this.clearNodeCode( 'vertex' );
  237. }
  238. clearFragmentNodeCode( ) {
  239. return this.clearNodeCode( 'fragment' );
  240. }
  241. addVertexFinalCode( code ) {
  242. this.addFinalCode( code, 'vertex' );
  243. }
  244. addFragmentFinalCode( code ) {
  245. this.addFinalCode( code, 'fragment' );
  246. }
  247. addFinalCode( code, shader ) {
  248. this.finalCode[ shader || this.shader ] += code + '\n';
  249. }
  250. addVertexParsCode( code ) {
  251. this.addParsCode( code, 'vertex' );
  252. }
  253. addFragmentParsCode( code ) {
  254. this.addParsCode( code, 'fragment' );
  255. }
  256. addParsCode( code, shader ) {
  257. this.parsCode[ shader || this.shader ] += code + '\n';
  258. }
  259. addVaryCode( code ) {
  260. this.addVertexParsCode( code );
  261. this.addFragmentParsCode( code );
  262. }
  263. isCache( name ) {
  264. return this.caches.indexOf( name ) !== - 1;
  265. }
  266. isSlot( name ) {
  267. return this.slots.indexOf( name ) !== - 1;
  268. }
  269. define( name, value ) {
  270. this.defines[ name ] = value === undefined ? 1 : value;
  271. }
  272. require( name ) {
  273. this.requires[ name ] = true;
  274. }
  275. isDefined( name ) {
  276. return this.defines[ name ] !== undefined;
  277. }
  278. getVar( uuid, type, ns, shader = 'varying', prefix = 'V', label = '' ) {
  279. const vars = this.getVars( shader );
  280. let data = vars[ uuid ];
  281. if ( ! data ) {
  282. const index = vars.length,
  283. name = ns ? ns : 'node' + prefix + index + ( label ? '_' + label : '' );
  284. data = { name: name, type: type };
  285. vars.push( data );
  286. vars[ uuid ] = data;
  287. }
  288. return data;
  289. }
  290. getTempVar( uuid, type, ns, label ) {
  291. return this.getVar( uuid, type, ns, this.shader, 'T', label );
  292. }
  293. getAttribute( name, type ) {
  294. if ( ! this.attributes[ name ] ) {
  295. const varying = this.getVar( name, type );
  296. this.addVertexParsCode( 'attribute ' + type + ' ' + name + ';' );
  297. this.addVertexFinalCode( varying.name + ' = ' + name + ';' );
  298. this.attributes[ name ] = { varying: varying, name: name, type: type };
  299. }
  300. return this.attributes[ name ];
  301. }
  302. getCode( shader ) {
  303. return [
  304. this.prefixCode,
  305. this.parsCode[ shader ],
  306. this.getVarListCode( this.getVars( 'varying' ), 'varying' ),
  307. this.getVarListCode( this.inputs.uniforms[ shader ], 'uniform' ),
  308. this.getIncludesCode( 'consts', shader ),
  309. this.getIncludesCode( 'structs', shader ),
  310. this.getIncludesCode( 'functions', shader ),
  311. 'void main() {',
  312. this.getVarListCode( this.getVars( shader ) ),
  313. this.code[ shader ],
  314. this.resultCode[ shader ],
  315. this.finalCode[ shader ],
  316. '}'
  317. ].join( '\n' );
  318. }
  319. getVarListCode( vars, prefix = '' ) {
  320. let code = '';
  321. for ( let i = 0, l = vars.length; i < l; ++ i ) {
  322. const nVar = vars[ i ],
  323. type = nVar.type,
  324. name = nVar.name;
  325. const formatType = this.getFormatByType( type );
  326. if ( formatType === undefined ) {
  327. throw new Error( 'Node pars ' + formatType + ' not found.' );
  328. }
  329. code += prefix + ' ' + formatType + ' ' + name + ';\n';
  330. }
  331. return code;
  332. }
  333. getVars( shader ) {
  334. return this.inputs.vars[ shader || this.shader ];
  335. }
  336. getNodeData( node ) {
  337. const uuid = node.isNode ? node.uuid : node;
  338. return this.nodeData[ uuid ] = this.nodeData[ uuid ] || {};
  339. }
  340. createUniform( shader, type, node, ns, needsUpdate, label ) {
  341. const uniforms = this.inputs.uniforms,
  342. index = uniforms.list.length;
  343. const uniform = new NodeUniform( {
  344. type: type,
  345. name: ns ? ns : 'nodeU' + index + ( label ? '_' + label : '' ),
  346. node: node,
  347. needsUpdate: needsUpdate
  348. } );
  349. uniforms.list.push( uniform );
  350. uniforms[ shader ].push( uniform );
  351. uniforms[ shader ][ uniform.name ] = uniform;
  352. this.uniforms[ uniform.name ] = uniform;
  353. return uniform;
  354. }
  355. createVertexUniform( type, node, ns, needsUpdate, label ) {
  356. return this.createUniform( 'vertex', type, node, ns, needsUpdate, label );
  357. }
  358. createFragmentUniform( type, node, ns, needsUpdate, label ) {
  359. return this.createUniform( 'fragment', type, node, ns, needsUpdate, label );
  360. }
  361. include( node, parent, source ) {
  362. let includesStruct;
  363. node = typeof node === 'string' ? NodeLib.get( node ) : node;
  364. if ( this.context.include === false ) {
  365. return node.name;
  366. }
  367. if ( node instanceof FunctionNode ) {
  368. includesStruct = this.includes.functions;
  369. } else if ( node instanceof ConstNode ) {
  370. includesStruct = this.includes.consts;
  371. } else if ( node instanceof StructNode ) {
  372. includesStruct = this.includes.structs;
  373. }
  374. const includes = includesStruct[ this.shader ] = includesStruct[ this.shader ] || [];
  375. if ( node ) {
  376. let included = includes[ node.name ];
  377. if ( ! included ) {
  378. included = includes[ node.name ] = {
  379. node: node,
  380. deps: []
  381. };
  382. includes.push( included );
  383. included.src = node.build( this, 'source' );
  384. }
  385. if ( node instanceof FunctionNode && parent && includes[ parent.name ] && includes[ parent.name ].deps.indexOf( node ) == - 1 ) {
  386. includes[ parent.name ].deps.push( node );
  387. if ( node.includes && node.includes.length ) {
  388. let i = 0;
  389. do {
  390. this.include( node.includes[ i ++ ], parent );
  391. } while ( i < node.includes.length );
  392. }
  393. }
  394. if ( source ) {
  395. included.src = source;
  396. }
  397. return node.name;
  398. } else {
  399. throw new Error( 'Include not found.' );
  400. }
  401. }
  402. colorToVectorProperties( color ) {
  403. return color.replace( 'r', 'x' ).replace( 'g', 'y' ).replace( 'b', 'z' ).replace( 'a', 'w' );
  404. }
  405. colorToVector( color ) {
  406. return color.replace( /c/g, 'v3' );
  407. }
  408. getIncludes( type, shader ) {
  409. return this.includes[ type ][ shader || this.shader ];
  410. }
  411. getIncludesCode( type, shader ) {
  412. let includes = this.getIncludes( type, shader );
  413. if ( ! includes ) return '';
  414. let code = '';
  415. includes = includes.sort( sortByPosition );
  416. for ( let i = 0; i < includes.length; i ++ ) {
  417. if ( includes[ i ].src ) code += includes[ i ].src + '\n';
  418. }
  419. return code;
  420. }
  421. getConstructorFromLength( len ) {
  422. return constructors[ len - 1 ];
  423. }
  424. isTypeMatrix( format ) {
  425. return /^m/.test( format );
  426. }
  427. getTypeLength( type ) {
  428. if ( type === 'f' ) return 1;
  429. return parseInt( this.colorToVector( type ).substr( 1 ) );
  430. }
  431. getTypeFromLength( len ) {
  432. if ( len === 1 ) return 'f';
  433. return 'v' + len;
  434. }
  435. findNode() {
  436. for ( let i = 0; i < arguments.length; i ++ ) {
  437. const nodeCandidate = arguments[ i ];
  438. if ( nodeCandidate !== undefined && nodeCandidate.isNode ) {
  439. return nodeCandidate;
  440. }
  441. }
  442. }
  443. resolve() {
  444. for ( let i = 0; i < arguments.length; i ++ ) {
  445. const nodeCandidate = arguments[ i ];
  446. if ( nodeCandidate !== undefined ) {
  447. if ( nodeCandidate.isNode ) {
  448. return nodeCandidate;
  449. } else if ( nodeCandidate.isTexture ) {
  450. switch ( nodeCandidate.mapping ) {
  451. case CubeReflectionMapping:
  452. case CubeRefractionMapping:
  453. return new CubeTextureNode( nodeCandidate );
  454. break;
  455. case CubeUVReflectionMapping:
  456. case CubeUVRefractionMapping:
  457. return new TextureCubeNode( new TextureNode( nodeCandidate ) );
  458. break;
  459. default:
  460. return new TextureNode( nodeCandidate );
  461. }
  462. } else if ( nodeCandidate.isVector2 ) {
  463. return new Vector2Node( nodeCandidate );
  464. } else if ( nodeCandidate.isVector3 ) {
  465. return new Vector3Node( nodeCandidate );
  466. } else if ( nodeCandidate.isVector4 ) {
  467. return new Vector4Node( nodeCandidate );
  468. }
  469. }
  470. }
  471. }
  472. format( code, from, to ) {
  473. const typeToType = this.colorToVector( to + ' <- ' + from );
  474. switch ( typeToType ) {
  475. case 'f <- v2' : return code + '.x';
  476. case 'f <- v3' : return code + '.x';
  477. case 'f <- v4' : return code + '.x';
  478. case 'f <- i' :
  479. case 'f <- b' : return 'float( ' + code + ' )';
  480. case 'v2 <- f' : return 'vec2( ' + code + ' )';
  481. case 'v2 <- v3': return code + '.xy';
  482. case 'v2 <- v4': return code + '.xy';
  483. case 'v2 <- i' :
  484. case 'v2 <- b' : return 'vec2( float( ' + code + ' ) )';
  485. case 'v3 <- f' : return 'vec3( ' + code + ' )';
  486. case 'v3 <- v2': return 'vec3( ' + code + ', 0.0 )';
  487. case 'v3 <- v4': return code + '.xyz';
  488. case 'v3 <- i' :
  489. case 'v3 <- b' : return 'vec2( float( ' + code + ' ) )';
  490. case 'v4 <- f' : return 'vec4( ' + code + ' )';
  491. case 'v4 <- v2': return 'vec4( ' + code + ', 0.0, 1.0 )';
  492. case 'v4 <- v3': return 'vec4( ' + code + ', 1.0 )';
  493. case 'v4 <- i' :
  494. case 'v4 <- b' : return 'vec4( float( ' + code + ' ) )';
  495. case 'i <- f' :
  496. case 'i <- b' : return 'int( ' + code + ' )';
  497. case 'i <- v2' : return 'int( ' + code + '.x )';
  498. case 'i <- v3' : return 'int( ' + code + '.x )';
  499. case 'i <- v4' : return 'int( ' + code + '.x )';
  500. case 'b <- f' : return '( ' + code + ' != 0.0 )';
  501. case 'b <- v2' : return '( ' + code + ' != vec2( 0.0 ) )';
  502. case 'b <- v3' : return '( ' + code + ' != vec3( 0.0 ) )';
  503. case 'b <- v4' : return '( ' + code + ' != vec4( 0.0 ) )';
  504. case 'b <- i' : return '( ' + code + ' != 0 )';
  505. }
  506. return code;
  507. }
  508. getTypeByFormat( format ) {
  509. return convertFormatToType[ format ] || format;
  510. }
  511. getFormatByType( type ) {
  512. return convertTypeToFormat[ type ] || type;
  513. }
  514. getUuid( uuid, useCache ) {
  515. useCache = useCache !== undefined ? useCache : true;
  516. if ( useCache && this.cache ) uuid = this.cache + '-' + uuid;
  517. return uuid;
  518. }
  519. getElementByIndex( index ) {
  520. return elements[ index ];
  521. }
  522. getIndexByElement( elm ) {
  523. return elements.indexOf( elm );
  524. }
  525. isShader( shader ) {
  526. return this.shader === shader;
  527. }
  528. setShader( shader ) {
  529. this.shader = shader;
  530. return this;
  531. }
  532. mergeDefines( defines ) {
  533. for ( const name in defines ) {
  534. this.defines[ name ] = defines[ name ];
  535. }
  536. return this.defines;
  537. }
  538. mergeUniform( uniforms ) {
  539. for ( const name in uniforms ) {
  540. this.uniforms[ name ] = uniforms[ name ];
  541. }
  542. return this.uniforms;
  543. }
  544. getTextureEncodingFromMap( map ) {
  545. let encoding;
  546. if ( ! map ) {
  547. encoding = LinearEncoding;
  548. } else if ( map.isTexture ) {
  549. encoding = map.encoding;
  550. } else if ( map.isWebGLRenderTarget ) {
  551. console.warn( 'THREE.WebGLPrograms.getTextureEncodingFromMap: don\'t use render targets as textures. Use their .texture property instead.' );
  552. encoding = map.texture.encoding;
  553. }
  554. if ( encoding === LinearEncoding && this.context.gamma ) {
  555. encoding = GammaEncoding;
  556. }
  557. return encoding;
  558. }
  559. }
  560. function sortByPosition( a, b ) {
  561. return a.deps.length - b.deps.length;
  562. }
  563. export { NodeBuilder };