StackNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { loop } from '../utils/LoopNode.js';
  7. import { ShaderNode, nodeProxy } from '../shadernode/ShaderNode.js';
  8. class StackNode extends Node {
  9. constructor( parent = null ) {
  10. super();
  11. this.nodes = [];
  12. this.outputNode = null;
  13. this.parent = parent;
  14. this._currentCond = null;
  15. this.isStackNode = true;
  16. }
  17. getNodeType( builder ) {
  18. return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
  19. }
  20. add( node ) {
  21. this.nodes.push( bypass( expression(), node ) );
  22. return this;
  23. }
  24. if( boolNode, method ) {
  25. const methodNode = new ShaderNode( method );
  26. this._currentCond = cond( boolNode, methodNode );
  27. return this.add( this._currentCond );
  28. }
  29. elseif( boolNode, method ) {
  30. const methodNode = new ShaderNode( method );
  31. const ifNode = cond( boolNode, methodNode );
  32. this._currentCond.elseNode = ifNode;
  33. this._currentCond = ifNode;
  34. return this;
  35. }
  36. else( method ) {
  37. this._currentCond.elseNode = new ShaderNode( method );
  38. return this;
  39. }
  40. assign( targetNode, sourceValue ) {
  41. return this.add( assign( targetNode, sourceValue ) );
  42. }
  43. loop( ...params ) {
  44. return this.add( loop( ...params ) );
  45. }
  46. build( builder, ...params ) {
  47. for ( const node of this.nodes ) {
  48. node.build( builder, 'void' );
  49. }
  50. return this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
  51. }
  52. }
  53. export default StackNode;
  54. export const stack = nodeProxy( StackNode );
  55. addNodeClass( 'StackNode', StackNode );