FunctionCallNode.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import TempNode from '../core/TempNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { addNodeElement, nodeArray, nodeObject, nodeObjects } from '../shadernode/ShaderNode.js';
  4. class FunctionCallNode extends TempNode {
  5. constructor( functionNode = null, parameters = {} ) {
  6. super();
  7. this.functionNode = functionNode;
  8. this.parameters = parameters;
  9. }
  10. setParameters( parameters ) {
  11. this.parameters = parameters;
  12. return this;
  13. }
  14. getParameters() {
  15. return this.parameters;
  16. }
  17. getNodeType( builder ) {
  18. return this.functionNode.getNodeType( builder );
  19. }
  20. generate( builder ) {
  21. const params = [];
  22. const functionNode = this.functionNode;
  23. const inputs = functionNode.getInputs( builder );
  24. const parameters = this.parameters;
  25. if ( Array.isArray( parameters ) ) {
  26. for ( let i = 0; i < parameters.length; i ++ ) {
  27. const inputNode = inputs[ i ];
  28. const node = parameters[ i ];
  29. params.push( node.build( builder, inputNode.type ) );
  30. }
  31. } else {
  32. for ( const inputNode of inputs ) {
  33. const node = parameters[ inputNode.name ];
  34. if ( node !== undefined ) {
  35. params.push( node.build( builder, inputNode.type ) );
  36. } else {
  37. throw new Error( `FunctionCallNode: Input '${inputNode.name}' not found in FunctionNode.` );
  38. }
  39. }
  40. }
  41. const functionName = functionNode.build( builder, 'property' );
  42. return `${functionName}( ${params.join( ', ' )} )`;
  43. }
  44. }
  45. export default FunctionCallNode;
  46. export const call = ( func, ...params ) => {
  47. params = params.length > 1 || ( params[ 0 ] && params[ 0 ].isNode === true ) ? nodeArray( params ) : nodeObjects( params[ 0 ] );
  48. return nodeObject( new FunctionCallNode( nodeObject( func ), params ) );
  49. };
  50. addNodeElement( 'call', call );
  51. addNodeClass( 'FunctionCallNode', FunctionCallNode );