Skip to content

Commit

Permalink
Demo with dynamic attribute switching!
Browse files Browse the repository at this point in the history
  • Loading branch information
vaneenige committed Nov 13, 2018
1 parent bcdaf9e commit c6e18f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
36 changes: 15 additions & 21 deletions demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const colors = [[255, 108, 0], [83, 109, 254], [29, 233, 182], [253, 216, 53]].m
);

// Boolean to toggle dynamic attributes
const dynamicAttributes = false;
const dynamicAttributes = true;

// Update value for every frame
const step = 0.01;
Expand Down Expand Up @@ -95,8 +95,6 @@ function addInstance() {
// Vertex shader used to calculate the position
const vertex = `
attribute vec3 aPositionStart;
attribute vec3 aControlPointOne;
attribute vec3 aControlPointTwo;
attribute vec3 aPositionEnd;
attribute vec3 aPosition;
attribute vec3 aColor;
Expand Down Expand Up @@ -137,7 +135,7 @@ function addInstance() {
let forward = true;

// Add an instance to the renderer
const instance = phenomenon.add(count, {
phenomenon.add(count, {
attributes,
multiplier,
vertex,
Expand All @@ -149,24 +147,23 @@ function addInstance() {

if (uProgress.value >= 1) {
if (dynamicAttributes) {
const b = {
const newEnd = {
x: getRandom(1),
y: getRandom(1),
z: getRandom(1)
};
const e = {
x: getRandom(1),
y: getRandom(1),
z: getRandom(1)
};
instance.prepareAttribute({
name: 'aPositionEnd',
data: () => [b.x + getRandom(0.1), b.y + getRandom(0.1), b.z + getRandom(0.1)],
r.prepareBuffer({
name: 'aPositionStart',
data: r.attributes[1].data,
size: 3
});
instance.prepareAttribute({
name: 'aPositionStart',
data: () => [e.x + getRandom(0.1), e.y + getRandom(0.1), e.z + getRandom(0.1)],
r.prepareAttribute({
name: 'aPositionEnd',
data: () => [
newEnd.x + getRandom(0.1),
newEnd.y + getRandom(0.1),
newEnd.z + getRandom(0.1)
],
size: 3
});
uProgress.value = 0;
Expand All @@ -178,9 +175,6 @@ function addInstance() {
});
}

for (let i = 0; i < 20; i += 1) {
// Delay the creation of each instance
setTimeout(() => {
addInstance();
}, 100 * i);
for (let i = 0; i < 10; i += 1) {
addInstance();
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class Instance {
}
}
}
this.prepareBuffer(Object.assign(attribute, { data: attributeBufferData }));
this.attributes[this.attributeKeys.indexOf(attribute.name)].data = attributeBufferData;
this.prepareBuffer(this.attributes[this.attributeKeys.indexOf(attribute.name)]);
}

/**
Expand Down

0 comments on commit c6e18f5

Please sign in to comment.