Skip to content

Commit b1f3f1f

Browse files
committed
Add custom vecX uniforms
1 parent f5cfbd5 commit b1f3f1f

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

src/sd-uniform.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,51 @@ class SDUniformElement extends SDBaseElement {
77
return parseFloat(this.getAttribute('x'));
88
}
99

10-
set x(newX) {
11-
if (newX != null) this.setAttribute('x', newX);
10+
set x(newx) {
11+
if (newx != null) this.setAttribute('x', newx);
1212
else this.removeAttribute('x');
1313
}
1414

15+
get y() {
16+
return parseFloat(this.getAttribute('y'));
17+
}
18+
19+
set y(newy) {
20+
if (newy != null) this.setAttribute('y', newy);
21+
else this.removeAttribute('y');
22+
}
23+
24+
get z() {
25+
return parseFloat(this.getAttribute('z'));
26+
}
27+
28+
set z(newz) {
29+
if (newz != null) this.setAttribute('z', newz);
30+
else this.removeAttribute('z');
31+
}
32+
33+
get w() {
34+
return parseFloat(this.getAttribute('w'));
35+
}
36+
37+
set w(neww) {
38+
if (neww != null) this.setAttribute('w', neww);
39+
else this.removeAttribute('w');
40+
}
41+
42+
getValue() {
43+
switch (this.type) {
44+
case 'vec2':
45+
return [this.x, this.y];
46+
case 'vec3':
47+
return [this.x, this.y, this.z];
48+
case 'vec4':
49+
return [this.x, this.y, this.z, this.w];
50+
default:
51+
return this.x;
52+
}
53+
}
54+
1555
get type() {
1656
return this.getAttribute('type');
1757
}
@@ -22,13 +62,17 @@ class SDUniformElement extends SDBaseElement {
2262
}
2363

2464
static get observedAttributes() {
25-
return ['x'];
65+
return ['x', 'y', 'z', 'w'];
2666
}
2767

2868
attributeChangedCallback(name, oldValue, newValue) {
2969
switch (name) {
3070
case 'x':
31-
if (newValue != null) this.renderer.setUniform(this.name, newValue);
71+
case 'y':
72+
case 'z':
73+
case 'w':
74+
if (newValue != null)
75+
this.renderer.setUniform(this.name, this.getValue());
3276
break;
3377
}
3478
}
@@ -40,7 +84,7 @@ class SDUniformElement extends SDBaseElement {
4084
}
4185

4286
this.program = program;
43-
this.renderer.addUniform(this.name, this.x, this.type);
87+
this.renderer.addUniform(this.name, this.getValue(), this.type);
4488
}
4589
}
4690

0 commit comments

Comments
 (0)