NodeBuilder.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. import NodeUniform from './NodeUniform.js';
  2. import NodeAttribute from './NodeAttribute.js';
  3. import NodeVary from './NodeVary.js';
  4. import NodeVar from './NodeVar.js';
  5. import NodeCode from './NodeCode.js';
  6. import NodeKeywords from './NodeKeywords.js';
  7. import { NodeUpdateType } from './constants.js';
  8. import { LinearEncoding } from 'three';
  9. class NodeBuilder {
  10. constructor( object, renderer, parser ) {
  11. this.object = object;
  12. this.material = object.material;
  13. this.renderer = renderer;
  14. this.parser = parser;
  15. this.nodes = [];
  16. this.updateNodes = [];
  17. this.hashNodes = {};
  18. this.vertexShader = null;
  19. this.fragmentShader = null;
  20. this.slots = { vertex: [], fragment: [] };
  21. this.defines = { vertex: {}, fragment: {} };
  22. this.uniforms = { vertex: [], fragment: [], index: 0 };
  23. this.codes = { vertex: [], fragment: [] };
  24. this.attributes = [];
  25. this.varys = [];
  26. this.vars = { vertex: [], fragment: [] };
  27. this.flow = { code: '' };
  28. this.stack = [];
  29. this.context = {
  30. keywords: new NodeKeywords(),
  31. material: object.material
  32. };
  33. this.nodesData = new WeakMap();
  34. this.shaderStage = null;
  35. this.slot = null;
  36. }
  37. addStack( node ) {
  38. /*
  39. if ( this.stack.indexOf( node ) !== - 1 ) {
  40. console.warn( 'Recursive node: ', node );
  41. }
  42. */
  43. this.stack.push( node );
  44. }
  45. removeStack( node ) {
  46. const lastStack = this.stack.pop();
  47. if ( lastStack !== node ) {
  48. throw new Error( 'NodeBuilder: Invalid node stack!' );
  49. }
  50. }
  51. addNode( node ) {
  52. if ( this.nodes.indexOf( node ) === - 1 ) {
  53. const updateType = node.getUpdateType( this );
  54. if ( updateType !== NodeUpdateType.None ) {
  55. this.updateNodes.push( node );
  56. }
  57. this.nodes.push( node );
  58. this.hashNodes[ node.getHash( this ) ] = node;
  59. }
  60. }
  61. getNodeFromHash( hash ) {
  62. return this.hashNodes[ hash ];
  63. }
  64. addSlot( shaderStage, slot ) {
  65. this.slots[ shaderStage ].push( slot );
  66. }
  67. define( shaderStage, name, value = '' ) {
  68. this.defines[ shaderStage ][ name ] = value;
  69. }
  70. setContext( context ) {
  71. this.context = context;
  72. }
  73. getContext() {
  74. return this.context;
  75. }
  76. getContextValue( name ) {
  77. return this.context[ name ];
  78. }
  79. getTexture( /* textureProperty, uvSnippet, biasSnippet = null */ ) {
  80. console.warn( 'Abstract function.' );
  81. }
  82. getCubeTexture( /* textureProperty, uvSnippet, biasSnippet = null */ ) {
  83. console.warn( 'Abstract function.' );
  84. }
  85. getConst( type, value ) {
  86. if ( type === 'float' ) return value + ( value % 1 ? '' : '.0' );
  87. if ( type === 'vec2' ) return `${ this.getType( 'vec2' ) }( ${value.x}, ${value.y} )`;
  88. if ( type === 'vec3' ) return `${ this.getType( 'vec3' ) }( ${value.x}, ${value.y}, ${value.z} )`;
  89. if ( type === 'vec4' ) return `${ this.getType( 'vec4' ) }( ${value.x}, ${value.y}, ${value.z}, ${value.w} )`;
  90. if ( type === 'color' ) return `${ this.getType( 'vec3' ) }( ${value.r}, ${value.g}, ${value.b} )`;
  91. throw new Error( `NodeBuilder: Type '${type}' not found in generate constant attempt.` );
  92. }
  93. getType( type ) {
  94. return type;
  95. }
  96. getAttribute( name, type ) {
  97. const attributes = this.attributes;
  98. // find attribute
  99. for ( const attribute of attributes ) {
  100. if ( attribute.name === name ) {
  101. return attribute;
  102. }
  103. }
  104. // create a new if no exist
  105. const attribute = new NodeAttribute( name, type );
  106. attributes.push( attribute );
  107. return attribute;
  108. }
  109. getPropertyName( node ) {
  110. return node.name;
  111. }
  112. isVector( type ) {
  113. return /vec\d/.test( type );
  114. }
  115. isMatrix( type ) {
  116. return /mat\d/.test( type );
  117. }
  118. isShaderStage( shaderStage ) {
  119. return this.shaderStage === shaderStage;
  120. }
  121. getTextureEncodingFromMap( map ) {
  122. let encoding;
  123. if ( map && map.isTexture ) {
  124. encoding = map.encoding;
  125. } else if ( map && map.isWebGLRenderTarget ) {
  126. encoding = map.texture.encoding;
  127. } else {
  128. encoding = LinearEncoding;
  129. }
  130. return encoding;
  131. }
  132. getVectorType( type ) {
  133. if ( type === 'color' ) return 'vec3';
  134. if ( type === 'texture' ) return 'vec4';
  135. return type;
  136. }
  137. getTypeFromLength( type ) {
  138. if ( type === 1 ) return 'float';
  139. if ( type === 2 ) return 'vec2';
  140. if ( type === 3 ) return 'vec3';
  141. if ( type === 4 ) return 'vec4';
  142. return 0;
  143. }
  144. getTypeLength( type ) {
  145. const vecType = this.getVectorType( type );
  146. if ( vecType === 'float' ) return 1;
  147. if ( vecType === 'vec2' ) return 2;
  148. if ( vecType === 'vec3' ) return 3;
  149. if ( vecType === 'vec4' ) return 4;
  150. return 0;
  151. }
  152. getVectorFromMatrix( type ) {
  153. return 'vec' + type.substr( 3 );
  154. }
  155. getDataFromNode( node, shaderStage = this.shaderStage ) {
  156. let nodeData = this.nodesData.get( node );
  157. if ( nodeData === undefined ) {
  158. nodeData = { vertex: {}, fragment: {} };
  159. this.nodesData.set( node, nodeData );
  160. }
  161. return shaderStage !== null ? nodeData[ shaderStage ] : nodeData;
  162. }
  163. getUniformFromNode( node, shaderStage, type ) {
  164. const nodeData = this.getDataFromNode( node, shaderStage );
  165. let nodeUniform = nodeData.uniform;
  166. if ( nodeUniform === undefined ) {
  167. const index = this.uniforms.index ++;
  168. nodeUniform = new NodeUniform( 'nodeUniform' + index, type, node );
  169. this.uniforms[ shaderStage ].push( nodeUniform );
  170. nodeData.uniform = nodeUniform;
  171. }
  172. return nodeUniform;
  173. }
  174. getVarFromNode( node, type, shaderStage = this.shaderStage ) {
  175. const nodeData = this.getDataFromNode( node, shaderStage );
  176. let nodeVar = nodeData.variable;
  177. if ( nodeVar === undefined ) {
  178. const vars = this.vars[ shaderStage ];
  179. const index = vars.length;
  180. nodeVar = new NodeVar( 'nodeVar' + index, type );
  181. vars.push( nodeVar );
  182. nodeData.variable = nodeVar;
  183. }
  184. return nodeVar;
  185. }
  186. getVaryFromNode( node, type ) {
  187. const nodeData = this.getDataFromNode( node, null );
  188. let nodeVary = nodeData.vary;
  189. if ( nodeVary === undefined ) {
  190. const varys = this.varys;
  191. const index = varys.length;
  192. nodeVary = new NodeVary( 'nodeVary' + index, type );
  193. varys.push( nodeVary );
  194. nodeData.vary = nodeVary;
  195. }
  196. return nodeVary;
  197. }
  198. getCodeFromNode( node, type, shaderStage = this.shaderStage ) {
  199. const nodeData = this.getDataFromNode( node );
  200. let nodeCode = nodeData.code;
  201. if ( nodeCode === undefined ) {
  202. const codes = this.codes[ shaderStage ];
  203. const index = codes.length;
  204. nodeCode = new NodeCode( 'nodeCode' + index, type );
  205. codes.push( nodeCode );
  206. nodeData.code = nodeCode;
  207. }
  208. return nodeCode;
  209. }
  210. addFlowCode( code ) {
  211. if ( ! /;\s*$/.test( code ) ) {
  212. code += ';';
  213. }
  214. this.flow.code += code + ' ';
  215. }
  216. flowSlot( slot, shaderStage = this.shaderStage ) {
  217. this.slot = slot;
  218. const flowData = this.flowNode( slot.node, slot.output );
  219. this.define( shaderStage, `NODE_CODE_${slot.name}`, flowData.code );
  220. this.define( shaderStage, `NODE_${slot.name}`, flowData.result );
  221. this.slot = null;
  222. }
  223. flowNode( node, output = null ) {
  224. const previousFlow = this.flow;
  225. const flow = {
  226. code: '',
  227. };
  228. this.flow = flow;
  229. flow.result = node.build( this, output );
  230. this.flow = previousFlow;
  231. return flow;
  232. }
  233. flowNodeFromShaderStage( shaderStage, node, output = null, propertyName = null ) {
  234. const previousShaderStage = this.shaderStage;
  235. this.setShaderStage( shaderStage );
  236. const flowData = this.flowNode( node, output );
  237. if ( propertyName !== null ) {
  238. flowData.code += `${propertyName} = ${flowData.result}; `;
  239. }
  240. const shaderStageCode = this.defines[ shaderStage ][ 'NODE_CODE' ] + flowData.code;
  241. this.define( shaderStage, 'NODE_CODE', shaderStageCode );
  242. this.setShaderStage( previousShaderStage );
  243. return flowData;
  244. }
  245. getDefines( shaderStage ) {
  246. const defines = this.defines[ shaderStage ];
  247. let code = '';
  248. for ( const name in defines ) {
  249. code += `#define ${name} ${defines[ name ]}\n`;
  250. }
  251. return code;
  252. }
  253. getAttributes( shaderStage ) {
  254. let snippet = '';
  255. if ( shaderStage === 'vertex' ) {
  256. const attributes = this.attributes;
  257. for ( let index = 0; index < attributes.length; index ++ ) {
  258. const attribute = attributes[ index ];
  259. snippet += `layout(location = ${index}) in ${attribute.type} ${attribute.name}; `;
  260. }
  261. }
  262. return snippet;
  263. }
  264. getVarys( /*shaderStage*/ ) {
  265. console.warn( 'Abstract function.' );
  266. }
  267. getVars( shaderStage ) {
  268. let snippet = '';
  269. const vars = this.vars[ shaderStage ];
  270. for ( let index = 0; index < vars.length; index ++ ) {
  271. const variable = vars[ index ];
  272. snippet += `${variable.type} ${variable.name}; `;
  273. }
  274. return snippet;
  275. }
  276. getUniforms( /*shaderStage*/ ) {
  277. console.warn( 'Abstract function.' );
  278. }
  279. getCodes( shaderStage ) {
  280. const codes = this.codes[ shaderStage ];
  281. let code = '';
  282. for ( const nodeCode of codes ) {
  283. code += nodeCode.code + '\n';
  284. }
  285. return code;
  286. }
  287. getHash() {
  288. return this.vertexShader + this.fragmentShader;
  289. }
  290. getShaderStage() {
  291. return this.shaderStage;
  292. }
  293. setShaderStage( shaderStage ) {
  294. this.shaderStage = shaderStage;
  295. }
  296. build() {
  297. const shaderStages = [ 'fragment', 'vertex' ];
  298. const shaderData = {};
  299. for ( const shaderStage of shaderStages ) {
  300. this.define( shaderStage, 'NODE_CODE', '' );
  301. }
  302. if ( this.context.vertex && this.context.vertex.isNode === true ) {
  303. this.flowNodeFromShaderStage( 'vertex', this.context.vertex );
  304. }
  305. for ( const shaderStage of shaderStages ) {
  306. this.setShaderStage( shaderStage );
  307. const slots = this.slots[ shaderStage ];
  308. for ( const slot of slots ) {
  309. this.flowSlot( slot, shaderStage );
  310. }
  311. }
  312. this.setShaderStage( null );
  313. for ( const shaderStage of shaderStages ) {
  314. const defines = this.getDefines( shaderStage );
  315. const uniforms = this.getUniforms( shaderStage );
  316. const attributes = this.getAttributes( shaderStage );
  317. const varys = this.getVarys( shaderStage );
  318. const vars = this.getVars( shaderStage );
  319. const codes = this.getCodes( shaderStage );
  320. shaderData[ shaderStage ] = `
  321. // <node_builder>
  322. #define NODE_MATERIAL
  323. // defines
  324. ${defines}
  325. // uniforms
  326. ${uniforms}
  327. // attributes
  328. ${attributes}
  329. // varys
  330. ${varys}
  331. // vars
  332. ${vars}
  333. // codes
  334. ${codes}
  335. // </node_builder>
  336. `;
  337. }
  338. this.vertexShader = shaderData.vertex;
  339. this.fragmentShader = shaderData.fragment;
  340. return this;
  341. }
  342. format( snippet, fromType, toType ) {
  343. fromType = this.getVectorType( fromType );
  344. toType = this.getVectorType( toType );
  345. const typeToType = `${fromType} to ${toType}`;
  346. switch ( typeToType ) {
  347. case 'int to float' : return `${ this.getType( 'float' ) }( ${ snippet } )`;
  348. case 'int to vec2' : return `${ this.getType( 'vec2' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
  349. case 'int to vec3' : return `${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) )`;
  350. case 'int to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ this.getType( 'float' ) }( ${ snippet } ) ), 1.0 )`;
  351. case 'float to int' : return `${ this.getType( 'int' ) }( ${ snippet } )`;
  352. case 'float to vec2' : return `${ this.getType( 'vec2' ) }( ${ snippet } )`;
  353. case 'float to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet } )`;
  354. case 'float to vec4' : return `${ this.getType( 'vec4' ) }( ${ this.getType( 'vec3' ) }( ${ snippet } ), 1.0 )`;
  355. case 'vec2 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
  356. case 'vec2 to float' : return `${ snippet }.x`;
  357. case 'vec2 to vec3' : return `${ this.getType( 'vec3' ) }( ${ snippet }, 0.0 )`;
  358. case 'vec2 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }.xy, 0.0, 1.0 )`;
  359. case 'vec3 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
  360. case 'vec3 to float' : return `${ snippet }.x`;
  361. case 'vec3 to vec2' : return `${ snippet }.xy`;
  362. case 'vec3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet }, 1.0 )`;
  363. case 'vec4 to int' : return `${ this.getType( 'int' ) }( ${ snippet }.x )`;
  364. case 'vec4 to float' : return `${ snippet }.x`;
  365. case 'vec4 to vec2' : return `${ snippet }.xy`;
  366. case 'vec4 to vec3' : return `${ snippet }.xyz`;
  367. case 'mat3 to int' : return `${ this.getType( 'int' ) }( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x`;
  368. case 'mat3 to float' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).x`;
  369. case 'mat3 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xy`;
  370. case 'mat3 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ) ).xyz`;
  371. case 'mat3 to vec4' : return `${ this.getType( 'vec4' ) }( ${ snippet } * ${ this.getType( 'vec3' ) }( 1.0 ), 1.0 )`;
  372. case 'mat4 to int' : return `${ this.getType( 'int' ) }( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x`;
  373. case 'mat4 to float' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).x`;
  374. case 'mat4 to vec2' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xy`;
  375. case 'mat4 to vec3' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) ).xyz`;
  376. case 'mat4 to vec4' : return `( ${ snippet } * ${ this.getType( 'vec4' ) }( 1.0 ) )`;
  377. }
  378. return snippet;
  379. }
  380. }
  381. export default NodeBuilder;