StackNode.js 1.5 KB

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