NodeBuilder.js 12 KB

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