Repository navigation
Expand file tree
/
Copy pathFunctionOverloadingNode.js
More file actions
176 lines (122 loc) · 3.45 KB
/
Copy pathFunctionOverloadingNode.js
File metadata and controls
176 lines (122 loc) · 3.45 KB
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
import Node from '../core/Node.js';
import { nodeProxy } from '../tsl/TSLCore.js';
/**
* This class allows to define multiple overloaded versions
* of the same function. Depending on the parameters of the function
* call, the node picks the best-fit overloaded version.
*
* @augments Node
*/
class FunctionOverloadingNode extends Node {
static get type() {
return 'FunctionOverloadingNode';
}
/**
* Constructs a new function overloading node.
*
* @param {Array<Function>} functionNodes - Array of `Fn` function definitions.
* @param {...Node} parametersNodes - A list of parameter nodes.
*/
constructor( functionNodes = [], ...parametersNodes ) {
super();
/**
* Array of `Fn` function definitions.
*
* @type {Array<Function>}
*/
this.functionNodes = functionNodes;
/**
* A list of parameter nodes.
*
* @type {Array<Node>}
*/
this.parametersNodes = parametersNodes;
/**
* The selected overloaded function call.
*
* @private
* @type {ShaderCallNodeInternal}
*/
this._candidateFn = null;
/**
* This node is marked as global.
*
* @type {boolean}
* @default true
*/
this.global = true;
}
isCacheable( /*builder*/ ) {
return false;
}
/**
* This method is overwritten since the node type is inferred from
* the function's return type.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The node type.
*/
generateNodeType( builder ) {
const candidateFn = this.getCandidateFn( builder );
return candidateFn.shaderNode.layout.type;
}
/**
* Returns the candidate function for the current parameters.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {FunctionNode} The candidate function.
*/
getCandidateFn( builder ) {
const params = this.parametersNodes;
let candidateFn = this._candidateFn;
if ( candidateFn === null ) {
let bestCandidateFn = null;
let bestScore = - 1;
for ( const functionNode of this.functionNodes ) {
const shaderNode = functionNode.shaderNode;
const layout = shaderNode.layout;
if ( layout === null ) {
throw new Error( 'THREE.FunctionOverloadingNode: FunctionNode must be a layout.' );
}
const inputs = layout.inputs;
if ( params.length === inputs.length ) {
let currentScore = 0;
for ( let i = 0; i < params.length; i ++ ) {
const param = params[ i ];
const input = inputs[ i ];
if ( param.getNodeType( builder ) === input.type ) {
currentScore ++;
}
}
if ( currentScore > bestScore ) {
bestCandidateFn = functionNode;
bestScore = currentScore;
}
}
}
this._candidateFn = candidateFn = bestCandidateFn;
}
return candidateFn;
}
/**
* Sets up the node for the current parameters.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {Node} The setup node.
*/
setup( builder ) {
const candidateFn = this.getCandidateFn( builder );
return candidateFn( ...this.parametersNodes );
}
}
export default FunctionOverloadingNode;
const overloadingBaseFn = /*@__PURE__*/ nodeProxy( FunctionOverloadingNode );
/**
* TSL function for creating a function overloading node.
*
* @tsl
* @function
* @param {Array<Function>} functionNodes - Array of `Fn` function definitions.
* @returns {FunctionOverloadingNode}
*/
export const overloadingFn = ( functionNodes ) => ( ...params ) => overloadingBaseFn( functionNodes, ...params );