FunctionNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import CodeNode from './CodeNode.js';
  2. import { addNodeClass } from '../core/Node.js';
  3. import { nodeObject } from '../shadernode/ShaderNode.js';
  4. class FunctionNode extends CodeNode {
  5. constructor( code = '', includes = [] ) {
  6. super( code, includes );
  7. this.keywords = {};
  8. }
  9. getNodeType( builder ) {
  10. return this.getNodeFunction( builder ).type;
  11. }
  12. getInputs( builder ) {
  13. return this.getNodeFunction( builder ).inputs;
  14. }
  15. getNodeFunction( builder ) {
  16. const nodeData = builder.getDataFromNode( this );
  17. let nodeFunction = nodeData.nodeFunction;
  18. if ( nodeFunction === undefined ) {
  19. nodeFunction = builder.parser.parseFunction( this.code );
  20. nodeData.nodeFunction = nodeFunction;
  21. }
  22. return nodeFunction;
  23. }
  24. generate( builder, output ) {
  25. super.generate( builder );
  26. const nodeFunction = this.getNodeFunction( builder );
  27. const name = nodeFunction.name;
  28. const type = nodeFunction.type;
  29. const nodeCode = builder.getCodeFromNode( this, type );
  30. if ( name !== '' ) {
  31. // use a custom property name
  32. nodeCode.name = name;
  33. }
  34. const propertyName = builder.getPropertyName( nodeCode );
  35. let code = this.getNodeFunction( builder ).getCode( propertyName );
  36. const keywords = this.keywords;
  37. const keywordsProperties = Object.keys( keywords );
  38. if ( keywordsProperties.length > 0 ) {
  39. for ( const property of keywordsProperties ) {
  40. const propertyRegExp = new RegExp( `\\b${property}\\b`, 'g' );
  41. const nodeProperty = keywords[ property ].build( builder, 'property' );
  42. code = code.replace( propertyRegExp, nodeProperty );
  43. }
  44. }
  45. nodeCode.code = code;
  46. if ( output === 'property' ) {
  47. return propertyName;
  48. } else {
  49. return builder.format( `${ propertyName }()`, type, output );
  50. }
  51. }
  52. }
  53. export default FunctionNode;
  54. export const func = ( code, includes ) => nodeObject( new FunctionNode( code, includes ) );
  55. export const fn = ( code, includes ) => func( code, includes ).call;
  56. addNodeClass( FunctionNode );