Skip to content

Commit f5cfbd5

Browse files
committed
Add custom float uniform
1 parent 86e8133 commit f5cfbd5

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/sd-uniform.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import SDBaseElement from './sd-base.js';
2+
3+
class SDUniformElement extends SDBaseElement {
4+
disconnectedCallback() {}
5+
6+
get x() {
7+
return parseFloat(this.getAttribute('x'));
8+
}
9+
10+
set x(newX) {
11+
if (newX != null) this.setAttribute('x', newX);
12+
else this.removeAttribute('x');
13+
}
14+
15+
get type() {
16+
return this.getAttribute('type');
17+
}
18+
19+
set type(newType) {
20+
if (newType != null) this.setAttribute('type', newType);
21+
else this.removeAttribute('type');
22+
}
23+
24+
static get observedAttributes() {
25+
return ['x'];
26+
}
27+
28+
attributeChangedCallback(name, oldValue, newValue) {
29+
switch (name) {
30+
case 'x':
31+
if (newValue != null) this.renderer.setUniform(this.name, newValue);
32+
break;
33+
}
34+
}
35+
36+
init(program) {
37+
if (!this.name) {
38+
console.warn('sd-uniform created without a name.');
39+
return;
40+
}
41+
42+
this.program = program;
43+
this.renderer.addUniform(this.name, this.x, this.type);
44+
}
45+
}
46+
47+
if (!customElements.get('sd-uniform')) {
48+
customElements.define('sd-uniform', SDUniformElement);
49+
}

src/shader-doodle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Template from './template.js';
22
import SDNodeElement from './sd-node.js';
33
import './sd-audio.js';
44
import './sd-texture.js';
5+
import './sd-uniform.js';
56

67
import Surface from './webgl/Surface.js';
78
import Renderer from './webgl/Renderer.js';

src/webgl/Renderer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function Renderer() {
2828
const surfaces = new Set();
2929

3030
const ustate = cheapClone(GLOBAL_UNIFORMS);
31+
/* TODO UNIFORM*/
3132

3233
const extensions = Extensions(gl);
3334
extensions.get('OES_texture_float');
@@ -106,6 +107,24 @@ function Renderer() {
106107
surfaces.delete(surface);
107108
}
108109

110+
function addUniform(name, value, type) {
111+
ustate.push({
112+
name,
113+
value,
114+
type,
115+
toyname: name,
116+
});
117+
}
118+
119+
function setUniform(name, value) {
120+
for (let i = 0; i < ustate.length; i++) {
121+
if (ustate[i].name === name) {
122+
ustate[i].value = value;
123+
break;
124+
}
125+
}
126+
}
127+
109128
function dispose() {
110129
surfaces.forEach(s => s.dispose());
111130
surfaces.clear();
@@ -128,6 +147,8 @@ function Renderer() {
128147
},
129148
addSurface,
130149
removeSurface,
150+
addUniform,
151+
setUniform,
131152
dispose,
132153
});
133154
}

0 commit comments

Comments
 (0)