NodeBuilder.js 12 KB

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