NodeBuilder.js 11 KB

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