JoinNode.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { TempNode } from '../core/TempNode.js';
  2. import { NodeUtils } from '../core/NodeUtils.js';
  3. const inputs = NodeUtils.elements;
  4. class JoinNode extends TempNode {
  5. constructor( x, y, z, w ) {
  6. super( 'f' );
  7. this.x = x;
  8. this.y = y;
  9. this.z = z;
  10. this.w = w;
  11. }
  12. getNumElements() {
  13. let i = inputs.length;
  14. while ( i -- ) {
  15. if ( this[ inputs[ i ] ] !== undefined ) {
  16. ++ i;
  17. break;
  18. }
  19. }
  20. return Math.max( i, 2 );
  21. }
  22. getType( builder ) {
  23. return builder.getTypeFromLength( this.getNumElements() );
  24. }
  25. generate( builder, output ) {
  26. const type = this.getType( builder ),
  27. length = this.getNumElements(),
  28. outputs = [];
  29. for ( let i = 0; i < length; i ++ ) {
  30. const elm = this[ inputs[ i ] ];
  31. outputs.push( elm ? elm.build( builder, 'f' ) : '0.0' );
  32. }
  33. const code = ( length > 1 ? builder.getConstructorFromLength( length ) : '' ) + '( ' + outputs.join( ', ' ) + ' )';
  34. return builder.format( code, type, output );
  35. }
  36. copy( source ) {
  37. super.copy( source );
  38. for ( const prop in source.inputs ) {
  39. this[ prop ] = source.inputs[ prop ];
  40. }
  41. return this;
  42. }
  43. toJSON( meta ) {
  44. let data = this.getJSONNode( meta );
  45. if ( ! data ) {
  46. data = this.createJSONNode( meta );
  47. data.inputs = {};
  48. const length = this.getNumElements();
  49. for ( let i = 0; i < length; i ++ ) {
  50. const elm = this[ inputs[ i ] ];
  51. if ( elm ) {
  52. data.inputs[ inputs[ i ] ] = elm.toJSON( meta ).uuid;
  53. }
  54. }
  55. }
  56. return data;
  57. }
  58. }
  59. JoinNode.prototype.nodeType = 'Join';
  60. export { JoinNode };