NodeBuilder.js 12 KB

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