NodeBuilder.js 12 KB

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