-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathFunctionNode.js
More file actions
182 lines (122 loc) · 3.85 KB
/
FunctionNode.js
File metadata and controls
182 lines (122 loc) · 3.85 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
177
178
179
180
181
182
import CodeNode from './CodeNode.js';
/**
* This class represents a native shader function. It can be used to implement
* certain aspects of a node material with native shader code. There are two predefined
* TSL functions for easier usage.
*
* - `wgslFn`: Creates a WGSL function node.
* - `glslFn`: Creates a GLSL function node.
*
* A basic example with one include looks like so:
*
* ```js
* const desaturateWGSLFn = wgslFn( `
* fn desaturate( color:vec3<f32> ) -> vec3<f32> {
* let lum = vec3<f32>( 0.299, 0.587, 0.114 );
* return vec3<f32>( dot( lum, color ) );
* }`
*);
* const someWGSLFn = wgslFn( `
* fn someFn( color:vec3<f32> ) -> vec3<f32> {
* return desaturate( color );
* }
* `, [ desaturateWGSLFn ] );
* material.colorNode = someWGSLFn( { color: texture( map ) } );
*```
* @augments CodeNode
*/
class FunctionNode extends CodeNode {
static get type() {
return 'FunctionNode';
}
/**
* Constructs a new function node.
*
* @param {string} [code=''] - The native code.
* @param {Array<Node>} [includes=[]] - An array of includes.
* @param {('js'|'wgsl'|'glsl')} [language=''] - The used language.
*/
constructor( code = '', includes = [], language = '' ) {
super( code, includes, language );
}
/**
* Returns the type of this function node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The type.
*/
generateNodeType( builder ) {
return this.getNodeFunction( builder ).type;
}
/**
* Returns the type of a member of this function node.
*
* @param {NodeBuilder} builder - The current node builder.
* @param {string} name - The name of the member.
* @return {string} The type of the member.
*/
getMemberType( builder, name ) {
const type = this.getNodeType( builder );
const structType = builder.getStructTypeNode( type );
return structType.getMemberType( builder, name );
}
/**
* Returns the inputs of this function node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {Array<NodeFunctionInput>} The inputs.
*/
getInputs( builder ) {
return this.getNodeFunction( builder ).inputs;
}
/**
* Returns the node function for this function node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {NodeFunction} The node function.
*/
getNodeFunction( builder ) {
const nodeData = builder.getDataFromNode( this );
let nodeFunction = nodeData.nodeFunction;
if ( nodeFunction === undefined ) {
nodeFunction = builder.parser.parseFunction( this.code );
nodeData.nodeFunction = nodeFunction;
}
return nodeFunction;
}
generate( builder, output ) {
super.generate( builder );
const nodeFunction = this.getNodeFunction( builder );
const name = nodeFunction.name;
const type = nodeFunction.type;
const nodeCode = builder.getCodeFromNode( this, type );
if ( name !== '' ) {
// use a custom property name
nodeCode.name = name;
}
const propertyName = builder.getPropertyName( nodeCode );
const code = this.getNodeFunction( builder ).getCode( propertyName );
nodeCode.code = code + '\n';
if ( output === 'property' ) {
return propertyName;
} else {
return builder.format( `${ propertyName }()`, type, output );
}
}
}
export default FunctionNode;
const nativeFn = ( code, includes = [], language = '' ) => {
for ( let i = 0; i < includes.length; i ++ ) {
const include = includes[ i ];
// TSL Function: glslFn, wgslFn
if ( typeof include === 'function' ) {
includes[ i ] = include.functionNode;
}
}
const functionNode = new FunctionNode( code, includes, language );
const fn = ( ...params ) => functionNode.call( ...params );
fn.functionNode = functionNode;
return fn;
};
export const glslFn = ( code, includes ) => nativeFn( code, includes, 'glsl' );
export const wgslFn = ( code, includes ) => nativeFn( code, includes, 'wgsl' );