Skip to content

Commit b45ae8e

Browse files
Xiaoji Chendy
authored andcommitted
Various bug fixes
1 parent c6b27e6 commit b45ae8e

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,8 @@ GLSL.prototype.transforms = {
10161016
//simple assign, =
10171017
return Descriptor(`${target} = ${right}`, {
10181018
type: right.type,
1019-
complexity: 1
1019+
complexity: 1,
1020+
include: right.include
10201021
})
10211022
},
10221023

lib/stdlib.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
var operators = require('./operators').operators;
99

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+
}
1022

1123
/**
1224
* Types stubs
@@ -105,36 +117,43 @@ function radians (degrees) {
105117
if (degrees.length) return degrees.map(radians);
106118
return degrees * 0.017453292519943295;
107119
}
120+
radians.type = genType;
108121

109122
function degrees (radians) {
110123
if (radians.length) return radians.map(degrees);
111124
return radians * 57.29577951308232;
112125
}
126+
degrees.type = genType;
113127

114128
function sin (angle) {
115129
if (angle.length) return angle.map(sin);
116130
return Math.sin(angle);
117131
}
132+
sin.type = genType;
118133

119134
function cos (angle) {
120135
if (angle.length) return angle.map(cos);
121136
return Math.cos(angle);
122137
}
138+
cos.type = genType;
123139

124140
function tan (angle) {
125141
if (angle.length) return angle.map(tan);
126142
return Math.tan(angle);
127143
}
144+
tan.type = genType;
128145

129146
function asin (x) {
130147
if (x.length) return x.map(asin);
131148
return Math.asin(x);
132149
}
150+
asin.type = genType;
133151

134152
function acos (x) {
135153
if (x.length) return x.map(acos);
136154
return Math.acos(x);
137155
}
156+
acos.type = genType;
138157

139158
function atan (y, x) {
140159
if (arguments.length > 1) {
@@ -151,23 +170,27 @@ function atan (y, x) {
151170

152171
return Math.atan(y);
153172
}
173+
atan.type = genType;
154174

155175
function pow (x, y) {
156176
if (x.length) return x.map(function (x, i) {
157177
return Math.pow(x, y[i]);
158178
});
159179
return Math.pow(x, y);
160180
}
181+
pow.type = genType;
161182

162183
function exp (x) {
163184
if (x.length) return x.map(exp);
164185
return Math.exp(x);
165186
}
187+
exp.type = genType;
166188

167189
function log (x) {
168190
if (x.length) return x.map(log);
169191
return Math.log(x);
170192
}
193+
log.type = genType;
171194

172195
var log2 = Math.log2 ? function log2 (x) {
173196
if (x.length) return x.map(log2);
@@ -176,36 +199,43 @@ var log2 = Math.log2 ? function log2 (x) {
176199
if (x.length) return x.map(log2);
177200
return Math.log(x) / Math.LN2;
178201
};
202+
log2.type = genType;
179203

180204
function exp2 (x) {
181205
if (x.length) return x.map(exp2);
182206
return Math.pow(2, x);
183207
}
208+
exp2.type = genType;
184209

185210
function sqrt (x) {
186211
if (x.length) return x.map(sqrt);
187212
return Math.sqrt(x);
188213
}
214+
sqrt.type = genType;
189215

190216
function inversesqrt (x) {
191217
if (x.length) return x.map(inversesqrt);
192218
return 1 / Math.sqrt(x);
193219
}
220+
inversesqrt.type = genType;
194221

195222
function abs (x) {
196223
if (x.length) return x.map(abs);
197224
return Math.abs(x);
198225
}
226+
abs.type = genType;
199227

200228
function floor (x) {
201229
if (x.length) return x.map(floor);
202230
return Math.floor(x);
203231
}
232+
floor.type = genType;
204233

205234
function ceil (x) {
206235
if (x.length) return x.map(ceil);
207236
return Math.ceil(x);
208237
}
238+
ceil.type = genType;
209239

210240
var sign = Math.sign ? function sign (x) {
211241
if (x.length) return x.map(sign);
@@ -221,11 +251,13 @@ var sign = Math.sign ? function sign (x) {
221251

222252
return x > 0 ? 1 : -1;
223253
};
254+
sign.type = genType;
224255

225256
function fract (x) {
226257
if (x.length) return x.map(fract);
227258
return x - Math.floor(x);
228259
}
260+
fract.type = genType;
229261

230262
function mod (x, y) {
231263
if (x.length) {
@@ -240,6 +272,7 @@ function mod (x, y) {
240272
}
241273
return x % y;
242274
}
275+
mod.type = genType;
243276

244277
function min (x, y) {
245278
if (x.length) {
@@ -252,6 +285,7 @@ function min (x, y) {
252285
}
253286
return Math.min(x, y);
254287
}
288+
min.type = genType;
255289

256290
function max (x, y) {
257291
if (x.length) {
@@ -264,6 +298,7 @@ function max (x, y) {
264298
}
265299
return Math.max(x, y);
266300
}
301+
max.type = genType;
267302

268303
function clamp (x, min, max) {
269304
if (x.length) {
@@ -277,6 +312,7 @@ function clamp (x, min, max) {
277312

278313
return Math.min(Math.max(x, min), max);
279314
}
315+
clamp.type = genType;
280316

281317
function mix (x, y, a) {
282318
if (x.length) {
@@ -290,6 +326,7 @@ function mix (x, y, a) {
290326

291327
return x * (1.0 - a) + y * a;
292328
}
329+
mix.type = genType;
293330

294331
function step (edge, x) {
295332
if (!x && !edge) return 0
@@ -304,9 +341,7 @@ function step (edge, x) {
304341

305342
return x < edge ? 0.0 : 1.0;
306343
}
307-
step.type = function (node) {
308-
return this.process(node.children[1]).type;
309-
}
344+
step.type = genType(1);
310345

311346
function smoothstep (edge0, edge1, x) {
312347
if (!x && !edge0 && !edge1) return 0
@@ -321,6 +356,7 @@ function smoothstep (edge0, edge1, x) {
321356
var t = Math.min(Math.max((x - edge0) / (edge1 - edge0), 0.0), 1.0);
322357
return t * t * (3.0 - 2.0 * t);
323358
}
359+
smoothstep.type = genType(2);
324360

325361
function length (x) {
326362
var sum = 0;
@@ -375,6 +411,7 @@ function normalize (x) {
375411
}
376412
return out;
377413
}
414+
normalize.type = genType;
378415

379416
function faceforward (N, I, Nref) {
380417
if (Nref == null) Nref = N;
@@ -386,6 +423,7 @@ function faceforward (N, I, Nref) {
386423

387424
return dot > 0 ? N.map(function (x) { return -x;}) : N;
388425
}
426+
faceforward.type = genType;
389427

390428
function reflect (I, N) {
391429
var dot = 0;
@@ -400,6 +438,7 @@ function reflect (I, N) {
400438

401439
return out;
402440
}
441+
reflect.type = genType;
403442

404443
function refract (I, N, eta) {
405444
var dot = 0;
@@ -419,7 +458,7 @@ function refract (I, N, eta) {
419458

420459
return out;
421460
}
422-
461+
refract.type = genType;
423462

424463
/**
425464
* Vector relational functions

test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ test('float x; vec2 uv, position = fn(x) * vec2(uv.yx.yx.x, -uv.y);', function (
6060
);
6161
t.end()
6262
})
63+
test('vec2 uv = vec2(1.); uv += mix(uv, uv, 0.);', function (t) {
64+
t.deepEqual(eval(t.name), [2, 2]);
65+
t.end()
66+
})
67+
test('vec2 uv = vec2(0., 0.5); uv *= smoothstep(0., 1., uv);', function (t) {
68+
t.deepEqual(eval(t.name), [0, 0.25]);
69+
t.end()
70+
})
71+
test('float x = 0.5; vec2 uv = vec2(0., 0.5); uv *= smoothstep(0., 1., x);', function (t) {
72+
t.deepEqual(eval(t.name), [0, 0.25]);
73+
t.end()
74+
})
6375
test('vec2 position; position *= 1.0 + vec2();', function (t) {
6476
t.equal(
6577
clean(compile(t.name)),

0 commit comments

Comments
 (0)