Skip to content

Commit 57c607a

Browse files
committed
feat(glsl): ext swizzle with index getter setter
1 parent c3b4eac commit 57c607a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/glsl/swizzle.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function prep(prototype, Class, ...keys) {
1212
});
1313
}
1414

15-
function prepare(Class, { Vec2, Vec3, Vec4 }, keysVec, keysCol) {
15+
function prepare(Class, { Vec2, Vec3, Vec4 }, keysVec, keysCol, indexCol) {
1616
const { prototype } = Class;
1717
const keys = [...keysVec, ...keysCol];
1818

@@ -34,12 +34,24 @@ function prepare(Class, { Vec2, Vec3, Vec4 }, keysVec, keysCol) {
3434

3535
keys.forEach((a) => keys.forEach((b) => keys.forEach((c) => keys.forEach((d) => prep(prototype, Vec4, a, b, c, d)))));
3636

37+
indexCol.forEach((index) => {
38+
let key = keysVec[parseInt(index, 10)];
39+
Object.defineProperty(prototype, index, {
40+
get() {
41+
return this[key];
42+
},
43+
set(value) {
44+
this[key] = value;
45+
return true;
46+
}
47+
});
48+
});
3749
}
3850

3951
export function swizzle(options) {
4052

41-
prepare(options.Vec2, options, ['x', 'y'], ['r', 'g']);
42-
prepare(options.Vec3, options, ['x', 'y', 'z'], ['r', 'g', 'b']);
43-
prepare(options.Vec4, options, ['x', 'y', 'z', 'w'], ['r', 'g', 'b', 'a']);
53+
prepare(options.Vec2, options, ['x', 'y'], ['r', 'g'], ['0', '1']);
54+
prepare(options.Vec3, options, ['x', 'y', 'z'], ['r', 'g', 'b'], ['0', '1', '2']);
55+
prepare(options.Vec4, options, ['x', 'y', 'z', 'w'], ['r', 'g', 'b', 'a'], ['0', '1', '2', '3']);
4456

4557
}

test/glsl/sim.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ float action(vec2 one, vec2 two) {
189189
assert.equal(result.z, 0);
190190
});
191191

192+
it('works fine with glsl swizzles index getter.', () => {
193+
const shader = ({ float, vec2 }) => {
194+
let bar = float((x = vec2()) => {
195+
return x[1];
196+
});
197+
return { bar };
198+
};
199+
const { js } = buildGLSL(shader, { js: true, glsl: false });
200+
201+
const { bar } = js;
202+
203+
const result = bar(new Vec2(3, 6));
204+
assert.equal(result, 6);
205+
});
206+
192207
it('works fine with glsl flexible vector factories.', () => {
193208
const shader = ({ vec4, vec2 }) => {
194209
let bar = vec4((x = vec2()) => {

0 commit comments

Comments
 (0)