NodeBuilder.js 11 KB

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