NodeBuilder.js 7.1 KB

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