NodeBuilder.js 11 KB

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