NodeBuilder.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import NodeUniform from './NodeUniform.js';
  2. class NodeBuilder {
  3. constructor( material, renderer ) {
  4. this.material = material;
  5. this.renderer = renderer;
  6. this.nodes = [];
  7. this.updateNodes = [];
  8. this.vertexShader = null;
  9. this.fragmentShader = null;
  10. this.slots = { vertex: [], fragment: [] };
  11. this.defines = { vertex: {}, fragment: {} };
  12. this.uniforms = { vertex: [], fragment: [] };
  13. this.attributes = {};
  14. this.attributeCount = 0;
  15. this.nodesData = new WeakMap();
  16. this.shaderStage = null;
  17. }
  18. addNode( node ) {
  19. if ( this.nodes.indexOf( node ) === - 1 ) {
  20. if ( node.needsUpdate === true ) {
  21. this.updateNodes.push( node );
  22. }
  23. this.nodes.push( node );
  24. }
  25. }
  26. addSlot( shaderStage, slot ) {
  27. this.slots[ shaderStage ].push( slot );
  28. }
  29. define( shaderStage, name, value = '' ) {
  30. this.defines[ shaderStage ][ name ] = value;
  31. }
  32. getTexture( /* textureProperty, uvSnippet */ ) {
  33. }
  34. getConst( type, value ) {
  35. if ( type === 'float' ) return value + ( value % 1 ? '' : '.0' );
  36. if ( type === 'vec2' ) return `vec2( ${value.x}, ${value.y} )`;
  37. if ( type === 'vec3' ) return `vec3( ${value.x}, ${value.y}, ${value.z} )`;
  38. if ( type === 'vec4' ) return `vec4( ${value.x}, ${value.y}, ${value.z}, ${value.w} )`;
  39. if ( type === 'color' ) return `vec3( ${value.r}, ${value.g}, ${value.b} )`;
  40. throw new Error( `Type '${type}' not found in generate constant attempt.` );
  41. }
  42. getAttribute( type, name, property = null ) {
  43. let attribute = this.attributes[ name ];
  44. if ( attribute === undefined ) {
  45. const index = this.attributeCount ++;
  46. if ( property === null ) {
  47. property = `node_A${index}`;
  48. }
  49. attribute = {
  50. type,
  51. name,
  52. index,
  53. property
  54. };
  55. this.attributes[ name ] = attribute;
  56. }
  57. return attribute;
  58. }
  59. getPropertyName( nodeUniform ) {
  60. return nodeUniform.name;
  61. }
  62. getVectorType( type ) {
  63. if ( type === 'color' ) return 'vec3';
  64. else if ( type === 'texture' ) return 'vec4';
  65. return type;
  66. }
  67. getTypeFromLength( type ) {
  68. if ( type === 1 ) return 'float';
  69. if ( type === 2 ) return 'vec2';
  70. if ( type === 3 ) return 'vec3';
  71. if ( type === 4 ) return 'vec4';
  72. return 0;
  73. }
  74. getTypeLength( type ) {
  75. type = this.getVectorType( type );
  76. if ( type === 'float' ) return 1;
  77. if ( type === 'vec2' ) return 2;
  78. if ( type === 'vec3' ) return 3;
  79. if ( type === 'vec4' ) return 4;
  80. return 0;
  81. }
  82. getDataFromNode( node, shaderStage = null ) {
  83. let nodeData = this.nodesData.get( node );
  84. if ( nodeData === undefined ) {
  85. nodeData = { vertex: {}, fragment: {} };
  86. this.nodesData.set( node, nodeData );
  87. }
  88. return shaderStage ? nodeData[ shaderStage ] : nodeData;
  89. }
  90. getUniformFromNode( node, shaderStage, type ) {
  91. const nodeData = this.getDataFromNode( node, shaderStage );
  92. let nodeUniform = nodeData.uniform;
  93. if ( nodeUniform === undefined ) {
  94. const uniforms = this.uniforms[ shaderStage ];
  95. const index = uniforms.length;
  96. nodeUniform = new NodeUniform( 'nodeU' + index, type, node );
  97. uniforms.push( nodeUniform );
  98. nodeData.uniform = nodeUniform;
  99. }
  100. return nodeUniform;
  101. }
  102. /*
  103. analyzeNode( node ) {
  104. }
  105. */
  106. flowNode( node, output ) {
  107. const flowData = {};
  108. flowData.result = node.build( this, output );
  109. return flowData;
  110. }
  111. _buildDefines( shader ) {
  112. const defines = this.defines[ shader ];
  113. let code = '';
  114. for ( const name in defines ) {
  115. code += `#define ${name} ${defines[ name ]}\n`;
  116. }
  117. return code;
  118. }
  119. getAttributesBodySnippet( /*shaderStage*/ ) {
  120. }
  121. getAttributesHeaderSnippet( /*shaderStage*/ ) {
  122. }
  123. getUniformsHeaderSnippet( shaderStage ) {
  124. const uniforms = this.uniforms[ shaderStage ];
  125. let snippet = '';
  126. for ( const uniform of uniforms ) {
  127. snippet += `${uniform.type} ${uniform.name}; `;
  128. }
  129. return snippet;
  130. }
  131. format( snippet, fromType, toType ) {
  132. fromType = this.getVectorType( fromType );
  133. toType = this.getVectorType( toType );
  134. const typeToType = `${fromType} to ${toType}`;
  135. switch ( typeToType ) {
  136. case 'float to vec2' : return `vec2( ${snippet} )`;
  137. case 'float to vec3' : return `vec3( ${snippet} )`;
  138. case 'float to vec4' : return `vec4( vec3( ${snippet} ), 1.0 )`;
  139. case 'vec2 to float' : return `${snippet}.x`;
  140. case 'vec2 to vec3' : return `vec3( ${snippet}.x, ${snippet}.y, 0.0 )`;
  141. case 'vec2 to vec4' : return `vec4( ${snippet}.x, ${snippet}.y, 0.0, 1.0 )`;
  142. case 'vec3 to float' : return `${snippet}.x`;
  143. case 'vec3 to vec2' : return `${snippet}.xy`;
  144. case 'vec3 to vec4' : return `vec4( ${snippet}.x, ${snippet}.y, ${snippet}.z, 1.0 )`;
  145. case 'vec4 to float' : return `${snippet}.x`;
  146. case 'vec4 to vec2' : return `${snippet}.xy`;
  147. case 'vec4 to vec3' : return `${snippet}.xyz`;
  148. }
  149. return snippet;
  150. }
  151. getHash() {
  152. return this.vertexShader + this.fragmentShader;
  153. }
  154. build() {
  155. const shaderStages = [ 'vertex', 'fragment' ];
  156. const shaderData = {};
  157. for ( const shaderStage of shaderStages ) {
  158. this.shaderStage = shaderStage;
  159. const slots = this.slots[ shaderStage ];
  160. for ( const slot of slots ) {
  161. const flowData = this.flowNode( slot.node, slot.output );
  162. this.define( shaderStage, `NODE_${slot.name}`, flowData.result );
  163. }
  164. }
  165. this.shaderStage = null;
  166. for ( const shaderStage of shaderStages ) {
  167. this.define( shaderStage, 'NODE_HEADER_UNIFORMS', this.getUniformsHeaderSnippet( shaderStage ) );
  168. this.define( shaderStage, 'NODE_HEADER_ATTRIBUTES', this.getAttributesHeaderSnippet( shaderStage ) );
  169. this.define( shaderStage, 'NODE_BODY_ATTRIBUTES', this.getAttributesBodySnippet( shaderStage ) );
  170. shaderData[ shaderStage ] = this._buildDefines( shaderStage );
  171. }
  172. this.vertexShader = shaderData.vertex;
  173. this.fragmentShader = shaderData.fragment;
  174. return this;
  175. }
  176. }
  177. export default NodeBuilder;