NodeBuilder.js 11 KB

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