StackNode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import Node from './Node.js';
  2. import { select } from '../math/ConditionalNode.js';
  3. import { ShaderNode, nodeProxy, getCurrentStack, setCurrentStack } from '../tsl/TSLBase.js';
  4. /**
  5. * TODO
  6. *
  7. * @augments Node
  8. */
  9. class StackNode extends Node {
  10. static get type() {
  11. return 'StackNode';
  12. }
  13. /**
  14. * Constructs a new stack node.
  15. *
  16. * @param {StackNode?} [parent=null] - The parent stack node.
  17. */
  18. constructor( parent = null ) {
  19. super();
  20. /**
  21. * List of nodes.
  22. *
  23. * @type {Array<Node>}
  24. */
  25. this.nodes = [];
  26. /**
  27. * The output node.
  28. *
  29. * @type {Node?}
  30. * @default null
  31. */
  32. this.outputNode = null;
  33. /**
  34. * The parent stack node.
  35. *
  36. * @type {StackNode}
  37. * @default null
  38. */
  39. this.parent = parent;
  40. /**
  41. * The current conditional node.
  42. *
  43. * @private
  44. * @type {ConditionalNode}
  45. * @default null
  46. */
  47. this._currentCond = null;
  48. /**
  49. * This flag can be used for type testing.
  50. *
  51. * @type {Boolean}
  52. * @readonly
  53. * @default true
  54. */
  55. this.isStackNode = true;
  56. }
  57. getNodeType( builder ) {
  58. return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
  59. }
  60. /**
  61. * Adds a node to this stack.
  62. *
  63. * @param {Node} node - The node to add.
  64. * @return {StackNode} A reference to this stack node.
  65. */
  66. add( node ) {
  67. this.nodes.push( node );
  68. return this;
  69. }
  70. /**
  71. * Represent an `if` statement in TSL.
  72. *
  73. * @param {Node} boolNode - Represents the condition.
  74. * @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
  75. * @return {StackNode} A reference to this stack node.
  76. */
  77. If( boolNode, method ) {
  78. const methodNode = new ShaderNode( method );
  79. this._currentCond = select( boolNode, methodNode );
  80. return this.add( this._currentCond );
  81. }
  82. /**
  83. * Represent an `elseif` statement in TSL.
  84. *
  85. * @param {Node} boolNode - Represents the condition.
  86. * @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
  87. * @return {StackNode} A reference to this stack node.
  88. */
  89. ElseIf( boolNode, method ) {
  90. const methodNode = new ShaderNode( method );
  91. const ifNode = select( boolNode, methodNode );
  92. this._currentCond.elseNode = ifNode;
  93. this._currentCond = ifNode;
  94. return this;
  95. }
  96. /**
  97. * Represent an `else` statement in TSL.
  98. *
  99. * @param {Function} method - TSL code which is executed in the `else` case.
  100. * @return {StackNode} A reference to this stack node.
  101. */
  102. Else( method ) {
  103. this._currentCond.elseNode = new ShaderNode( method );
  104. return this;
  105. }
  106. build( builder, ...params ) {
  107. const previousStack = getCurrentStack();
  108. setCurrentStack( this );
  109. for ( const node of this.nodes ) {
  110. node.build( builder, 'void' );
  111. }
  112. setCurrentStack( previousStack );
  113. return this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
  114. }
  115. //
  116. else( ...params ) { // @deprecated, r168
  117. console.warn( 'TSL.StackNode: .else() has been renamed to .Else().' );
  118. return this.Else( ...params );
  119. }
  120. elseif( ...params ) { // @deprecated, r168
  121. console.warn( 'TSL.StackNode: .elseif() has been renamed to .ElseIf().' );
  122. return this.ElseIf( ...params );
  123. }
  124. }
  125. export default StackNode;
  126. export const stack = /*@__PURE__*/ nodeProxy( StackNode );
粤ICP备19079148号