add option to load normals with model

This commit is contained in:
Stewart Allen 2022-01-05 09:29:27 -05:00
commit 098fd9bb7e
4 changed files with 11 additions and 18 deletions

View file

@ -203,16 +203,10 @@ function space_init(data) {
moto.client.fn.model_heal(m.id)
.then(data => {
if (data) {
// api.group.new([new mesh.model({
// file: 'healed',
// mesh: data.vertices.toFloat32(),
// // mesh: Float32Array.from([...data.v]),
// // index: Uint32Array.from([...data.f])
// })]);
m.reload(
Float32Array.from(data.vertices),
// Float32Array.from([...data.v]),
// Uint32Array.from([...data.f])
data.vertices,
data.indices,
data.normals
);
}
});

View file

@ -329,6 +329,7 @@ let util = mesh.util = {
// bounding box workaround adapted from:
// https://discourse.threejs.org/t/bounding-box-bigger-than-concave-object-extrudegeometry/26073/2
// https://discourse.threejs.org/t/invalid-bounds-generated-for-some-orientations/33205
box3expand(box3, object) {
let geometry = object.geometry;
object.updateWorldMatrix(geometry ? true : false, false);

View file

@ -49,7 +49,7 @@ let materials = mesh.material = {
mesh.model = class MeshModel extends mesh.object {
constructor(data, id) {
super(id);
let { file, mesh, index, vertices, indices } = data;
let { file, mesh, vertices, indices, normals } = data;
if (!mesh) {
dbug.error(`'${file}' missing mesh data`);
@ -62,7 +62,7 @@ mesh.model = class MeshModel extends mesh.object {
if (dot > 0) file = text.substring(0, dot);
this.file = file || 'unnamed';
this.load(mesh || vertices, index || indices);
this.load(mesh || vertices, indices, normals);
// persist in db so it can be restored on page load
mapp.db.space.put(this.id, { file, mesh });
@ -89,11 +89,11 @@ mesh.model = class MeshModel extends mesh.object {
});
}
load(vertices, indices) {
load(vertices, indices, normals) {
let geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.computeVertexNormals();
if (!normals) geo.computeVertexNormals();
let meh = this.mesh = new THREE.Mesh(geo, materials.unselected);
meh.receiveShadow = true;
meh.castShadow = true;
@ -104,14 +104,14 @@ mesh.model = class MeshModel extends mesh.object {
worker.model_load({id: this.id, name: this.file, vertices, indices});
}
reload(vertices, indices) {
reload(vertices, indices, normals) {
this.wireframe(false);
let geo = this.mesh.geometry;
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geo.setAttribute('normal', undefined);
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.attributes.position.needsUpdate = true;
geo.computeVertexNormals();
if (!normals) geo.computeVertexNormals();
// sync data to worker
worker.model_load({id: this.id, name: this.name, vertices, indices});
}

View file

@ -59,9 +59,7 @@ let model = {
dbug.log('...healed');
dbug.log(tool);
return true || tool.newFaces ? {
vertices: tool.unrolled(),
// v: tool.vertices,
// f: tool.faces.map(f => f/3)
vertices: tool.unrolled().toFloat32(),
} : 0;
}
};