StructVarNode.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import Node from './Node.js';
  2. import FloatNode from '../inputs/FloatNode.js';
  3. const zeroValue = new FloatNode( 0 ).setConst( true );
  4. class StructVarNode extends Node {
  5. constructor( struct, inputs = {} ) {
  6. super();
  7. this.struct = struct;
  8. this.inputs = inputs;
  9. }
  10. getType( builder ) {
  11. return this.struct.getType( builder );
  12. }
  13. generate( builder, output ) {
  14. const type = this.getType( builder );
  15. const struct = this.struct;
  16. const inputs = this.inputs;
  17. const structInputs = this.struct.inputs;
  18. const nodeData = builder.getDataFromNode( this );
  19. let property = nodeData.property;
  20. if ( property === undefined ) {
  21. property = struct.build( builder, 'var' );
  22. const inputsSnippets = [];
  23. for ( const inputName in structInputs ) {
  24. const inputType = structInputs[ inputName ];
  25. const input = inputs[ inputName ];
  26. let inputSnippet = null;
  27. if ( input !== undefined ) {
  28. inputSnippet = input.build( builder, inputType );
  29. } else {
  30. inputSnippet = zeroValue.build( builder, inputType );
  31. }
  32. inputsSnippets.push( inputSnippet );
  33. }
  34. builder.addFlowCode( `${type} ${property} = ${type}( ${inputsSnippets.join( ', ' )} )` );
  35. nodeData.property = property;
  36. }
  37. return builder.format( property, type, output );
  38. }
  39. }
  40. export default StructVarNode;