StackNode.js 1.6 KB

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