forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVaryingNode.js
205 lines (141 loc) · 4.48 KB
/
VaryingNode.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 { NodeShaderStage } from './constants.js';
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
/**
* Class for representing shader varyings as nodes. Varyings are create from
* existing nodes like the following:
*
* ```js
* const positionLocal = positionGeometry.toVarying( 'vPositionLocal' );
* ```
*
* @augments Node
*/
class VaryingNode extends Node {
static get type() {
return 'VaryingNode';
}
/**
* Constructs a new varying node.
*
* @param {Node} node - The node for which a varying should be created.
* @param {?string} name - The name of the varying in the shader.
*/
constructor( node, name = null ) {
super();
/**
* The node for which a varying should be created.
*
* @type {Node}
*/
this.node = node;
/**
* The name of the varying in the shader. If no name is defined,
* the node system auto-generates one.
*
* @type {?string}
* @default null
*/
this.name = name;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isVaryingNode = true;
}
/**
* The method is overwritten so it always returns `true`.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {boolean} Whether this node is global or not.
*/
isGlobal( /*builder*/ ) {
return true;
}
getHash( builder ) {
return this.name || super.getHash( builder );
}
getNodeType( builder ) {
// VaryingNode is auto type
return this.node.getNodeType( builder );
}
/**
* This method performs the setup of a varying node with the current node builder.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {NodeVarying} The node varying from the node builder.
*/
setupVarying( builder ) {
const properties = builder.getNodeProperties( this );
let varying = properties.varying;
if ( varying === undefined ) {
const name = this.name;
const type = this.getNodeType( builder );
properties.varying = varying = builder.getVaryingFromNode( this, name, type );
properties.node = this.node;
}
// this property can be used to check if the varying can be optimized for a variable
varying.needsInterpolation || ( varying.needsInterpolation = ( builder.shaderStage === 'fragment' ) );
return varying;
}
setup( builder ) {
this.setupVarying( builder );
}
analyze( builder ) {
this.setupVarying( builder );
return this.node.analyze( builder );
}
generate( builder ) {
const properties = builder.getNodeProperties( this );
const varying = this.setupVarying( builder );
const needsReassign = builder.shaderStage === 'fragment' && properties.reassignPosition === true && builder.context.needsPositionReassign;
if ( properties.propertyName === undefined || needsReassign ) {
const type = this.getNodeType( builder );
const propertyName = builder.getPropertyName( varying, NodeShaderStage.VERTEX );
// force node run in vertex stage
builder.flowNodeFromShaderStage( NodeShaderStage.VERTEX, this.node, type, propertyName );
properties.propertyName = propertyName;
if ( needsReassign ) {
// once reassign varying in fragment stage
properties.reassignPosition = false;
} else if ( properties.reassignPosition === undefined && builder.context.isPositionNodeInput ) {
properties.reassignPosition = true;
}
}
return builder.getPropertyName( varying );
}
}
export default VaryingNode;
/**
* TSL function for creating a varying node.
*
* @tsl
* @function
* @param {Node} node - The node for which a varying should be created.
* @param {?string} name - The name of the varying in the shader.
* @returns {VaryingNode}
*/
export const varying = /*@__PURE__*/ nodeProxy( VaryingNode ).setParameterLength( 1, 2 );
/**
* Computes a node in the vertex stage.
*
* @tsl
* @function
* @param {Node} node - The node which should be executed in the vertex stage.
* @returns {VaryingNode}
*/
export const vertexStage = ( node ) => varying( node );
addMethodChaining( 'toVarying', varying );
addMethodChaining( 'toVertexStage', vertexStage );
// Deprecated
addMethodChaining( 'varying', ( ...params ) => { // @deprecated, r173
console.warn( 'TSL.VaryingNode: .varying() has been renamed to .toVarying().' );
return varying( ...params );
} );
addMethodChaining( 'vertexStage', ( ...params ) => { // @deprecated, r173
console.warn( 'TSL.VaryingNode: .vertexStage() has been renamed to .toVertexStage().' );
return varying( ...params );
} );