fix position array size, re-set attributes each time with sub-arrays

This commit is contained in:
Stewart Allen 2022-10-27 22:14:40 -04:00
commit 0494f2ca57

View file

@ -100,11 +100,12 @@ kiri.load(() => {
animate_clear animate_clear
}); });
function meshAdd(id, ind, pos, len) { function meshAdd(id, ind, pos, ilen, plen) {
const geo = new THREE.BufferGeometry(); const geo = new THREE.BufferGeometry();
if (len) geo.setDrawRange(0, len); const pa = plen ? pos.subarray(0, plen * 3) : pos;
geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); const ia = ilen ? ind.subarray(0, ilen) : ind;
geo.setIndex(new THREE.BufferAttribute(ind, 1)); geo.setAttribute('position', new THREE.BufferAttribute(pa, 3));
geo.setIndex(new THREE.BufferAttribute(ia, 1));
const mat = new THREE.MeshMatcapMaterial({ const mat = new THREE.MeshMatcapMaterial({
flatShading: true, flatShading: true,
transparent: false, transparent: false,
@ -113,20 +114,22 @@ kiri.load(() => {
side: THREE.DoubleSide side: THREE.DoubleSide
}); });
const mesh = new THREE.Mesh(geo, mat); const mesh = new THREE.Mesh(geo, mat);
mesh.pos = pos;
mesh.ind = ind;
space.world.add(mesh); space.world.add(mesh);
meshes[id] = mesh; meshes[id] = mesh;
} }
function meshUpdate(id, ind, pos, len) { function meshUpdate(id, ind, pos, ilen, plen) {
const mesh = meshes[id]; const mesh = meshes[id];
if (!mesh) { if (!mesh) {
return; // animate cancelled return; // animate cancelled
} }
// console.log({update: id, ind, pos, len});
const geo = mesh.geometry; const geo = mesh.geometry;
if (len) geo.setDrawRange(0, len); mesh.pos = pos || mesh.pos;
if (ind) geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); mesh.ind = ind || mesh.ind;
if (pos) geo.setIndex(new THREE.BufferAttribute(ind, 1)); geo.setAttribute('position', new THREE.BufferAttribute(mesh.pos.subarray(0, plen * 3), 3));
geo.setIndex(new THREE.BufferAttribute(mesh.ind.subarray(0, ilen), 1));
geo.attributes.position.needsUpdate = true; geo.attributes.position.needsUpdate = true;
geo.index.needsUpdate = true; geo.index.needsUpdate = true;
space.update(); space.update();
@ -181,8 +184,8 @@ kiri.load(() => {
return; return;
} }
if (data.mesh_add) { if (data.mesh_add) {
const { id, ind, pos, len } = data.mesh_add; const { id, ind, pos, ilen, plen } = data.mesh_add;
meshAdd(id, ind, pos, len); meshAdd(id, ind, pos, ilen, plen);
space.refresh(); space.refresh();
} }
if (data.mesh_del) { if (data.mesh_del) {
@ -199,8 +202,8 @@ kiri.load(() => {
} }
} }
if (data.mesh_update) { if (data.mesh_update) {
const { id, ind, pos, len } = data.mesh_update; const { id, ind, pos, ilen, plen } = data.mesh_update;
meshUpdate(id, ind, pos, len); meshUpdate(id, ind, pos, ilen, plen);
} }
if (data && data.progress) { if (data && data.progress) {
progress.innerText = (data.progress * 100).toFixed(1) + '%' progress.innerText = (data.progress * 100).toFixed(1) + '%'
@ -249,7 +252,8 @@ class Stock {
id: this.id, id: this.id,
ind: newbuf ? this.ibuf : undefined, ind: newbuf ? this.ibuf : undefined,
pos: newbuf ? this.vbuf : undefined, pos: newbuf ? this.vbuf : undefined,
len: this.ilen ilen: this.ilen,
plen: this.plen
} }); } });
this.newbuf = false; this.newbuf = false;
this.sends++; this.sends++;
@ -295,6 +299,7 @@ class Stock {
sharedVertexBuffer(size) { sharedVertexBuffer(size) {
const old = this.vbuf; const old = this.vbuf;
this.plen = size;
if (old && old.length >= size) { if (old && old.length >= size) {
// console.log({svb: this.id, reuse: size, old: old.length}); // console.log({svb: this.id, reuse: size, old: old.length});
return old; return old;