forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackNode.js
205 lines (151 loc) · 3.95 KB
/
StackNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import Node from './Node.js';
import { select } from '../math/ConditionalNode.js';
import { ShaderNode, nodeProxy, getCurrentStack, setCurrentStack } from '../tsl/TSLBase.js';
/**
* Stack is a helper for Nodes that need to produce stack-based code instead of continuous flow.
* They are usually needed in cases like `If`, `Else`.
*
* @augments Node
*/
class StackNode extends Node {
static get type() {
return 'StackNode';
}
/**
* Constructs a new stack node.
*
* @param {?StackNode} [parent=null] - The parent stack node.
*/
constructor( parent = null ) {
super();
/**
* List of nodes.
*
* @type {Array<Node>}
*/
this.nodes = [];
/**
* The output node.
*
* @type {?Node}
* @default null
*/
this.outputNode = null;
/**
* The parent stack node.
*
* @type {?StackNode}
* @default null
*/
this.parent = parent;
/**
* The current conditional node.
*
* @private
* @type {ConditionalNode}
* @default null
*/
this._currentCond = null;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isStackNode = true;
}
getNodeType( builder ) {
return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
}
getMemberType( builder, name ) {
return this.outputNode ? this.outputNode.getMemberType( builder, name ) : 'void';
}
/**
* Adds a node to this stack.
*
* @param {Node} node - The node to add.
* @return {StackNode} A reference to this stack node.
*/
add( node ) {
this.nodes.push( node );
return this;
}
/**
* Represent an `if` statement in TSL.
*
* @param {Node} boolNode - Represents the condition.
* @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
* @return {StackNode} A reference to this stack node.
*/
If( boolNode, method ) {
const methodNode = new ShaderNode( method );
this._currentCond = select( boolNode, methodNode );
return this.add( this._currentCond );
}
/**
* Represent an `elseif` statement in TSL.
*
* @param {Node} boolNode - Represents the condition.
* @param {Function} method - TSL code which is executed if the condition evaluates to `true`.
* @return {StackNode} A reference to this stack node.
*/
ElseIf( boolNode, method ) {
const methodNode = new ShaderNode( method );
const ifNode = select( boolNode, methodNode );
this._currentCond.elseNode = ifNode;
this._currentCond = ifNode;
return this;
}
/**
* Represent an `else` statement in TSL.
*
* @param {Function} method - TSL code which is executed in the `else` case.
* @return {StackNode} A reference to this stack node.
*/
Else( method ) {
this._currentCond.elseNode = new ShaderNode( method );
return this;
}
build( builder, ...params ) {
const previousStack = getCurrentStack();
setCurrentStack( this );
for ( const node of this.nodes ) {
node.build( builder, 'void' );
}
setCurrentStack( previousStack );
return this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
}
// Deprecated
/**
* @function
* @deprecated since r168. Use {@link StackNode#Else} instead.
*
* @param {...any} params
* @returns {StackNode}
*/
else( ...params ) { // @deprecated, r168
console.warn( 'TSL.StackNode: .else() has been renamed to .Else().' );
return this.Else( ...params );
}
/**
* @deprecated since r168. Use {@link StackNode#ElseIf} instead.
*
* @param {...any} params
* @returns {StackNode}
*/
elseif( ...params ) { // @deprecated, r168
console.warn( 'TSL.StackNode: .elseif() has been renamed to .ElseIf().' );
return this.ElseIf( ...params );
}
}
export default StackNode;
/**
* TSL function for creating a stack node.
*
* @tsl
* @function
* @param {?StackNode} [parent=null] - The parent stack node.
* @returns {StackNode}
*/
export const stack = /*@__PURE__*/ nodeProxy( StackNode ).setParameterLength( 0, 1 );