StackNode.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Node, { addNodeClass } from './Node.js';
  2. import { assign } from '../math/OperatorNode.js';
  3. import { bypass } from '../core/BypassNode.js';
  4. import { expression } from '../code/ExpressionNode.js';
  5. import { cond } from '../math/CondNode.js';
  6. import { nodeProxy, shader } from '../shadernode/ShaderNode.js';
  7. class StackNode extends Node {
  8. constructor( parent = null ) {
  9. super();
  10. this.nodes = [];
  11. this.outputNode = null;
  12. this.parent = parent;
  13. this._currentCond = null;
  14. this.isStackNode = true;
  15. }
  16. getNodeType( builder ) {
  17. return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
  18. }
  19. add( node ) {
  20. this.nodes.push( bypass( expression(), node ) );
  21. return this;
  22. }
  23. if( boolNode, method ) {
  24. const methodNode = shader( method );
  25. this._currentCond = cond( boolNode, methodNode );
  26. return this.add( this._currentCond );
  27. }
  28. elseif( boolNode, method ) {
  29. const methodNode = shader( method );
  30. const ifNode = cond( boolNode, methodNode );
  31. this._currentCond.elseNode = ifNode;
  32. this._currentCond = ifNode;
  33. return this;
  34. }
  35. else( method ) {
  36. this._currentCond.elseNode = shader( method );
  37. return this;
  38. }
  39. assign( targetNode, sourceValue ) {
  40. return this.add( assign( targetNode, sourceValue ) );
  41. }
  42. build( builder, ...params ) {
  43. for ( const node of this.nodes ) {
  44. node.build( builder );
  45. }
  46. return this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
  47. }
  48. }
  49. export default StackNode;
  50. export const stack = nodeProxy( StackNode );
  51. addNodeClass( StackNode );