SplitNode.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Node from '../core/Node.js';
  2. import { vector } from '../core/NodeBuilder.js';
  3. class SplitNode extends Node {
  4. constructor( node, components = 'x' ) {
  5. super();
  6. this.node = node;
  7. this.components = components;
  8. }
  9. getVectorLength() {
  10. let vectorLength = this.components.length;
  11. for ( const c of this.components ) {
  12. vectorLength = Math.max( vector.indexOf( c ) + 1, vectorLength );
  13. }
  14. return vectorLength;
  15. }
  16. getNodeType( builder ) {
  17. return builder.getTypeFromLength( this.components.length );
  18. }
  19. generate( builder ) {
  20. const node = this.node;
  21. const nodeTypeLength = builder.getTypeLength( node.getNodeType( builder ) );
  22. if ( nodeTypeLength > 1 ) {
  23. let type = null;
  24. const componentsLength = this.getVectorLength();
  25. if ( componentsLength >= nodeTypeLength ) {
  26. // need expand the input node
  27. type = builder.getTypeFromLength( this.getVectorLength() );
  28. }
  29. const nodeSnippet = node.build( builder, type );
  30. return `${nodeSnippet}.${this.components}`;
  31. } else {
  32. // ignore components if node is a float
  33. return node.build( builder );
  34. }
  35. }
  36. }
  37. export default SplitNode;