7
7
8
8
var operators = require ( './operators' ) . operators ;
9
9
10
+ // Returns the type of argument at index
11
+ // If argIndex is not specified, assume the first argument
12
+ function genType ( argIndex ) {
13
+ if ( Number . isInteger ( argIndex ) ) {
14
+ return function ( node ) {
15
+ return this . process ( node . children [ argIndex + 1 ] ) . type ;
16
+ }
17
+ }
18
+ var node = argIndex ;
19
+ // node.children[0] is the function itself
20
+ return this . process ( node . children [ 1 ] ) . type ;
21
+ }
10
22
11
23
/**
12
24
* Types stubs
@@ -105,36 +117,43 @@ function radians (degrees) {
105
117
if ( degrees . length ) return degrees . map ( radians ) ;
106
118
return degrees * 0.017453292519943295 ;
107
119
}
120
+ radians . type = genType ;
108
121
109
122
function degrees ( radians ) {
110
123
if ( radians . length ) return radians . map ( degrees ) ;
111
124
return radians * 57.29577951308232 ;
112
125
}
126
+ degrees . type = genType ;
113
127
114
128
function sin ( angle ) {
115
129
if ( angle . length ) return angle . map ( sin ) ;
116
130
return Math . sin ( angle ) ;
117
131
}
132
+ sin . type = genType ;
118
133
119
134
function cos ( angle ) {
120
135
if ( angle . length ) return angle . map ( cos ) ;
121
136
return Math . cos ( angle ) ;
122
137
}
138
+ cos . type = genType ;
123
139
124
140
function tan ( angle ) {
125
141
if ( angle . length ) return angle . map ( tan ) ;
126
142
return Math . tan ( angle ) ;
127
143
}
144
+ tan . type = genType ;
128
145
129
146
function asin ( x ) {
130
147
if ( x . length ) return x . map ( asin ) ;
131
148
return Math . asin ( x ) ;
132
149
}
150
+ asin . type = genType ;
133
151
134
152
function acos ( x ) {
135
153
if ( x . length ) return x . map ( acos ) ;
136
154
return Math . acos ( x ) ;
137
155
}
156
+ acos . type = genType ;
138
157
139
158
function atan ( y , x ) {
140
159
if ( arguments . length > 1 ) {
@@ -151,23 +170,27 @@ function atan (y, x) {
151
170
152
171
return Math . atan ( y ) ;
153
172
}
173
+ atan . type = genType ;
154
174
155
175
function pow ( x , y ) {
156
176
if ( x . length ) return x . map ( function ( x , i ) {
157
177
return Math . pow ( x , y [ i ] ) ;
158
178
} ) ;
159
179
return Math . pow ( x , y ) ;
160
180
}
181
+ pow . type = genType ;
161
182
162
183
function exp ( x ) {
163
184
if ( x . length ) return x . map ( exp ) ;
164
185
return Math . exp ( x ) ;
165
186
}
187
+ exp . type = genType ;
166
188
167
189
function log ( x ) {
168
190
if ( x . length ) return x . map ( log ) ;
169
191
return Math . log ( x ) ;
170
192
}
193
+ log . type = genType ;
171
194
172
195
var log2 = Math . log2 ? function log2 ( x ) {
173
196
if ( x . length ) return x . map ( log2 ) ;
@@ -176,36 +199,43 @@ var log2 = Math.log2 ? function log2 (x) {
176
199
if ( x . length ) return x . map ( log2 ) ;
177
200
return Math . log ( x ) / Math . LN2 ;
178
201
} ;
202
+ log2 . type = genType ;
179
203
180
204
function exp2 ( x ) {
181
205
if ( x . length ) return x . map ( exp2 ) ;
182
206
return Math . pow ( 2 , x ) ;
183
207
}
208
+ exp2 . type = genType ;
184
209
185
210
function sqrt ( x ) {
186
211
if ( x . length ) return x . map ( sqrt ) ;
187
212
return Math . sqrt ( x ) ;
188
213
}
214
+ sqrt . type = genType ;
189
215
190
216
function inversesqrt ( x ) {
191
217
if ( x . length ) return x . map ( inversesqrt ) ;
192
218
return 1 / Math . sqrt ( x ) ;
193
219
}
220
+ inversesqrt . type = genType ;
194
221
195
222
function abs ( x ) {
196
223
if ( x . length ) return x . map ( abs ) ;
197
224
return Math . abs ( x ) ;
198
225
}
226
+ abs . type = genType ;
199
227
200
228
function floor ( x ) {
201
229
if ( x . length ) return x . map ( floor ) ;
202
230
return Math . floor ( x ) ;
203
231
}
232
+ floor . type = genType ;
204
233
205
234
function ceil ( x ) {
206
235
if ( x . length ) return x . map ( ceil ) ;
207
236
return Math . ceil ( x ) ;
208
237
}
238
+ ceil . type = genType ;
209
239
210
240
var sign = Math . sign ? function sign ( x ) {
211
241
if ( x . length ) return x . map ( sign ) ;
@@ -221,11 +251,13 @@ var sign = Math.sign ? function sign (x) {
221
251
222
252
return x > 0 ? 1 : - 1 ;
223
253
} ;
254
+ sign . type = genType ;
224
255
225
256
function fract ( x ) {
226
257
if ( x . length ) return x . map ( fract ) ;
227
258
return x - Math . floor ( x ) ;
228
259
}
260
+ fract . type = genType ;
229
261
230
262
function mod ( x , y ) {
231
263
if ( x . length ) {
@@ -240,6 +272,7 @@ function mod (x, y) {
240
272
}
241
273
return x % y ;
242
274
}
275
+ mod . type = genType ;
243
276
244
277
function min ( x , y ) {
245
278
if ( x . length ) {
@@ -252,6 +285,7 @@ function min (x, y) {
252
285
}
253
286
return Math . min ( x , y ) ;
254
287
}
288
+ min . type = genType ;
255
289
256
290
function max ( x , y ) {
257
291
if ( x . length ) {
@@ -264,6 +298,7 @@ function max (x, y) {
264
298
}
265
299
return Math . max ( x , y ) ;
266
300
}
301
+ max . type = genType ;
267
302
268
303
function clamp ( x , min , max ) {
269
304
if ( x . length ) {
@@ -277,6 +312,7 @@ function clamp (x, min, max) {
277
312
278
313
return Math . min ( Math . max ( x , min ) , max ) ;
279
314
}
315
+ clamp . type = genType ;
280
316
281
317
function mix ( x , y , a ) {
282
318
if ( x . length ) {
@@ -290,6 +326,7 @@ function mix (x, y, a) {
290
326
291
327
return x * ( 1.0 - a ) + y * a ;
292
328
}
329
+ mix . type = genType ;
293
330
294
331
function step ( edge , x ) {
295
332
if ( ! x && ! edge ) return 0
@@ -304,9 +341,7 @@ function step (edge, x) {
304
341
305
342
return x < edge ? 0.0 : 1.0 ;
306
343
}
307
- step . type = function ( node ) {
308
- return this . process ( node . children [ 1 ] ) . type ;
309
- }
344
+ step . type = genType ( 1 ) ;
310
345
311
346
function smoothstep ( edge0 , edge1 , x ) {
312
347
if ( ! x && ! edge0 && ! edge1 ) return 0
@@ -321,6 +356,7 @@ function smoothstep (edge0, edge1, x) {
321
356
var t = Math . min ( Math . max ( ( x - edge0 ) / ( edge1 - edge0 ) , 0.0 ) , 1.0 ) ;
322
357
return t * t * ( 3.0 - 2.0 * t ) ;
323
358
}
359
+ smoothstep . type = genType ( 2 ) ;
324
360
325
361
function length ( x ) {
326
362
var sum = 0 ;
@@ -375,6 +411,7 @@ function normalize (x) {
375
411
}
376
412
return out ;
377
413
}
414
+ normalize . type = genType ;
378
415
379
416
function faceforward ( N , I , Nref ) {
380
417
if ( Nref == null ) Nref = N ;
@@ -386,6 +423,7 @@ function faceforward (N, I, Nref) {
386
423
387
424
return dot > 0 ? N . map ( function ( x ) { return - x ; } ) : N ;
388
425
}
426
+ faceforward . type = genType ;
389
427
390
428
function reflect ( I , N ) {
391
429
var dot = 0 ;
@@ -400,6 +438,7 @@ function reflect (I, N) {
400
438
401
439
return out ;
402
440
}
441
+ reflect . type = genType ;
403
442
404
443
function refract ( I , N , eta ) {
405
444
var dot = 0 ;
@@ -419,7 +458,7 @@ function refract (I, N, eta) {
419
458
420
459
return out ;
421
460
}
422
-
461
+ refract . type = genType ;
423
462
424
463
/**
425
464
* Vector relational functions
0 commit comments