NodeBuilder.js 11 KB

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