fix mesh gl errors with geometry shallow clone

This commit is contained in:
Stewart Allen 2022-01-04 17:42:19 -05:00
commit 19721b0dcc
3 changed files with 14 additions and 18 deletions

View file

@ -111,6 +111,14 @@
return this; return this;
}; };
BP.shallowClone = function() {
let geo = new THREE.BufferGeometry();
for (let [k, v] of Object.entries(this.attributes)) {
geo.setAttribute(k, new THREE.BufferAttribute(v.array, v.itemSize));
}
return geo;
};
THREE.Object3D.prototype.newGroup = function() { THREE.Object3D.prototype.newGroup = function() {
var group = new THREE.Group(); var group = new THREE.Group();
this.add(group); this.add(group);

View file

@ -89,11 +89,6 @@ mesh.group = class MeshGroup extends mesh.object {
return this; return this;
} }
// update(oldmesh, newmesh) {
// this.group.remove(oldmesh);
// this.group.add(newmesh);
// }
// center objects to group bounds // center objects to group bounds
// dependent on first being added to world/scene // dependent on first being added to world/scene
centerModels() { centerModels() {

View file

@ -104,19 +104,15 @@ mesh.model = class MeshModel extends mesh.object {
worker.model_load({id: this.id, name: this.file, vertices, indices}); worker.model_load({id: this.id, name: this.file, vertices, indices});
} }
optimize(geo) {
let ind = THREE.BufferGeometryUtils.mergeVertices(geo || this.mesh.geometry);
console.log({geo: geo || this.mesh.geometry.clone(), ind});
return ind;
}
reload(vertices, indices) { reload(vertices, indices) {
this.wireframe(false); this.wireframe(false);
let geo = this.mesh.geometry; let geo = this.mesh.geometry;
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3)); geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geo.setAttribute('normal', undefined); geo.setAttribute('normal', undefined);
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1)); if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.attributes.position.needsUpdate = true;
geo.computeVertexNormals(); geo.computeVertexNormals();
// sync data to worker
worker.model_load({id: this.id, name: this.name, vertices, indices}); worker.model_load({id: this.id, name: this.name, vertices, indices});
} }
@ -141,7 +137,7 @@ mesh.model = class MeshModel extends mesh.object {
mat.opacity = 1; mat.opacity = 1;
mat.visible = false; mat.visible = false;
} else { } else {
mat.transparent = ov < 1.0; mat.transparent = true;
mat.opacity = ov; mat.opacity = ov;
mat.visible = true; mat.visible = true;
} }
@ -167,9 +163,8 @@ mesh.model = class MeshModel extends mesh.object {
this.opacity(this._wireo); this.opacity(this._wireo);
} }
if (bool) { if (bool) {
this.material({clone:true});
this._wireo = this.opacity(); this._wireo = this.opacity();
this._wire = new THREE.Mesh(this.mesh.geometry, materials.wireframe); this._wire = new THREE.Mesh(this.mesh.geometry.shallowClone(), materials.wireframe);
this.mesh.add(this._wire); this.mesh.add(this._wire);
this.opacity(opt.opacity || 0); this.opacity(opt.opacity || 0);
} }
@ -177,10 +172,8 @@ mesh.model = class MeshModel extends mesh.object {
} }
material(mat) { material(mat) {
this.mesh.material = mat.clone === true ? this.mesh.material.clone() : mat;
if (this._wire) {
this.wireframe(false); this.wireframe(false);
} this.mesh.material = mat;
} }
remove() { remove() {