NodeBuilder.js 17 KB

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