SplitNode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. serialize( data ) {
  37. super.serialize( data );
  38. data.components = this.components;
  39. }
  40. deserialize( data ) {
  41. super.deserialize( data );
  42. this.components = data.components;
  43. }
  44. }
  45. export default SplitNode;