StackNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 SetNode from '../utils/SetNode.js';
  8. import { ShaderNode, nodeProxy, nodeObject } from '../shadernode/ShaderNode.js';
  9. class StackNode extends Node {
  10. constructor( parent = null ) {
  11. super();
  12. this.nodes = [];
  13. this.outputNode = null;
  14. this.parent = parent;
  15. this._currentCond = null;
  16. this.isStackNode = true;
  17. }
  18. getNodeType( builder ) {
  19. return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
  20. }
  21. add( node ) {
  22. this.nodes.push( bypass( expression(), node ) );
  23. return this;
  24. }
  25. if( boolNode, method ) {
  26. const methodNode = new ShaderNode( method );
  27. this._currentCond = cond( boolNode, methodNode );
  28. return this.add( this._currentCond );
  29. }
  30. elseif( boolNode, method ) {
  31. const methodNode = new ShaderNode( method );
  32. const ifNode = cond( boolNode, methodNode );
  33. this._currentCond.elseNode = ifNode;
  34. this._currentCond = ifNode;
  35. return this;
  36. }
  37. else( method ) {
  38. this._currentCond.elseNode = new ShaderNode( method );
  39. return this;
  40. }
  41. assign( targetNode, sourceValue ) {
  42. sourceValue = nodeObject( sourceValue );
  43. if ( targetNode.isSplitNode ) {
  44. sourceValue = new SetNode( targetNode.node, targetNode.components, sourceValue );
  45. targetNode = targetNode.node;
  46. }
  47. if ( targetNode.isPropertyNode !== true && targetNode.isVarNode !== true && targetNode.isArrayElementNode !== true && targetNode.isVaryingNode !== true ) {
  48. console.error( 'THREE.TSL: Invalid assign, target must be a property or variable.', targetNode.getSelf() );
  49. //return this;
  50. }
  51. return this.add( assign( targetNode, sourceValue ) );
  52. }
  53. loop( ...params ) {
  54. return this.add( loop( ...params ) );
  55. }
  56. build( builder, ...params ) {
  57. for ( const node of this.nodes ) {
  58. node.build( builder, 'void' );
  59. }
  60. return this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
  61. }
  62. }
  63. export default StackNode;
  64. export const stack = nodeProxy( StackNode );
  65. addNodeClass( 'StackNode', StackNode );