2021-12-27 16:24:39 -05:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
2021-12-29 16:49:15 -05:00
|
|
|
// start worker pool (disabled for now with *0)
|
|
|
|
|
moto.client.start(`/code/mesh_pool?${gapp.version}`, moto.client.max() * 0);
|
2021-12-29 00:19:22 -05:00
|
|
|
|
2021-12-29 16:49:15 -05:00
|
|
|
// dep: ext.three
|
|
|
|
|
// dep: ext.three-bgu
|
|
|
|
|
gapp.finalize("mesh.work", [
|
|
|
|
|
"moto.license", // dep: moto.license
|
2021-12-29 17:20:17 -05:00
|
|
|
"moto.client", // dep: moto.client
|
|
|
|
|
"moto.worker", // dep: moto.worker
|
|
|
|
|
"mesh.tool", // dep: mesh.tool
|
2021-12-31 22:11:20 -05:00
|
|
|
"add.three", // dep: add.three
|
2021-12-29 16:49:15 -05:00
|
|
|
]);
|
2021-12-27 18:13:11 -05:00
|
|
|
|
2021-12-31 22:11:20 -05:00
|
|
|
// compensation for space/world/platform rotation
|
2022-01-02 16:30:05 -05:00
|
|
|
let core_matrix = new THREE.Matrix4().makeRotationX(Math.PI / 2);
|
2021-12-31 22:11:20 -05:00
|
|
|
|
2022-01-02 00:40:43 -05:00
|
|
|
let { client, worker } = moto;
|
2022-01-01 22:39:49 -05:00
|
|
|
let cache = {};
|
|
|
|
|
|
2022-01-07 18:53:49 -05:00
|
|
|
function log(msg) {
|
|
|
|
|
return worker.publish("mesh.log", msg);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 14:25:28 -05:00
|
|
|
function cacheUpdate(id, data) {
|
|
|
|
|
Object.assign(cache[id], data);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 21:00:19 -05:00
|
|
|
function translate_encode(id, matrix) {
|
|
|
|
|
let rec = cache[id];
|
|
|
|
|
let geo = rec.geo.clone();
|
|
|
|
|
geo.applyMatrix4(core_matrix.clone().multiply( new THREE.Matrix4().fromArray(matrix) ));
|
|
|
|
|
return geo.attributes.position.array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 14:25:28 -05:00
|
|
|
let model = {
|
|
|
|
|
load(data) {
|
2022-01-04 01:11:13 -05:00
|
|
|
let { vertices, indices, name, id } = data;
|
2022-01-02 14:25:28 -05:00
|
|
|
let geo = new THREE.BufferGeometry();
|
|
|
|
|
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
|
2022-01-04 01:11:13 -05:00
|
|
|
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
|
2022-01-02 16:30:05 -05:00
|
|
|
cacheUpdate(id, { name, geo, matrix: core_matrix.clone() });
|
2022-01-02 14:25:28 -05:00
|
|
|
},
|
2022-01-02 00:40:43 -05:00
|
|
|
|
2022-01-02 14:25:28 -05:00
|
|
|
debug(data) {
|
2022-01-08 21:00:19 -05:00
|
|
|
let { id, matrix } = data;
|
|
|
|
|
return translate_encode(id, matrix);
|
|
|
|
|
throw "should not here";
|
2022-01-02 14:25:28 -05:00
|
|
|
let rec = cache[id];
|
|
|
|
|
let geo = rec.geo.clone();
|
2022-01-02 16:30:05 -05:00
|
|
|
let m4 = core_matrix.clone().multiply( new THREE.Matrix4().fromArray(matrix) );
|
2022-01-02 14:25:28 -05:00
|
|
|
geo.applyMatrix4(m4);
|
|
|
|
|
// for debugging state / matrix ops
|
|
|
|
|
return geo.attributes.position.array;
|
2022-01-03 22:57:19 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
heal(id) {
|
2022-01-07 18:53:49 -05:00
|
|
|
log(`${id} | indexing...`);
|
2022-01-04 01:11:13 -05:00
|
|
|
let geo = cache[id].geo;
|
2022-01-03 22:57:19 -05:00
|
|
|
let tool = new mesh.tool({
|
2022-01-04 01:11:13 -05:00
|
|
|
vertices: geo.attributes.position.array,
|
|
|
|
|
faces: geo.index ? geo.index.array : undefined,
|
|
|
|
|
debug: false
|
2022-01-03 22:57:19 -05:00
|
|
|
});
|
2022-01-07 18:53:49 -05:00
|
|
|
log(`${id} | healing...`);
|
2022-01-04 09:48:36 -05:00
|
|
|
tool.heal();
|
2022-01-03 22:57:19 -05:00
|
|
|
dbug.log(tool);
|
2022-01-04 01:11:13 -05:00
|
|
|
return true || tool.newFaces ? {
|
2022-01-05 09:29:27 -05:00
|
|
|
vertices: tool.unrolled().toFloat32(),
|
2022-01-03 22:57:19 -05:00
|
|
|
} : 0;
|
2022-01-02 14:25:28 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let group = {
|
|
|
|
|
add(data) {
|
|
|
|
|
let { id, model } = data;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
remove(data) {
|
|
|
|
|
let { id, model } = data;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let object = {
|
|
|
|
|
create(data) {
|
|
|
|
|
let { id, type } = data;
|
|
|
|
|
cache[id] = { id, type };
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
destroy(data) {
|
|
|
|
|
delete cache[data.id];
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-01-02 00:40:43 -05:00
|
|
|
|
2022-01-08 21:00:19 -05:00
|
|
|
let file = {
|
|
|
|
|
export(data, send) {
|
|
|
|
|
let { format, recs } = data;
|
|
|
|
|
switch (format) {
|
|
|
|
|
case "obj":
|
|
|
|
|
let p = 1;
|
2022-01-10 17:30:39 -05:00
|
|
|
let obj = [`# Generated By Kiri:Moto (units = millimeters)`];
|
2022-01-08 21:00:19 -05:00
|
|
|
for (let rec of recs) {
|
|
|
|
|
let { id, matrix, file } = rec;
|
2022-01-10 17:30:39 -05:00
|
|
|
let verts = Array.from(translate_encode(id, matrix)).map(v => v.round(5));
|
2022-01-08 21:00:19 -05:00
|
|
|
obj.push(`g ${file}`);
|
|
|
|
|
for (let i=0; i<verts.length; p += 3) {
|
|
|
|
|
obj.push(`v ${verts[i++]} ${verts[i++]} ${verts[i++]}`);
|
|
|
|
|
obj.push(`v ${verts[i++]} ${verts[i++]} ${verts[i++]}`);
|
|
|
|
|
obj.push(`v ${verts[i++]} ${verts[i++]} ${verts[i++]}`);
|
|
|
|
|
obj.push(`f ${p} ${p+1} ${p+2}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj.join('\n');
|
|
|
|
|
default:
|
|
|
|
|
throw `invalid format "${format}"`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function debug() {
|
2022-01-03 17:13:50 -05:00
|
|
|
console.log({work_cache: cache});
|
2022-01-08 21:00:19 -05:00
|
|
|
}
|
2022-01-03 17:13:50 -05:00
|
|
|
|
2022-01-02 14:25:28 -05:00
|
|
|
worker.bindObject({
|
2022-01-08 21:00:19 -05:00
|
|
|
debug,
|
2022-01-02 14:25:28 -05:00
|
|
|
model,
|
|
|
|
|
group,
|
2022-01-08 21:00:19 -05:00
|
|
|
object,
|
|
|
|
|
file
|
2021-12-31 22:11:20 -05:00
|
|
|
});
|
|
|
|
|
|
2022-01-03 17:13:50 -05:00
|
|
|
worker.ready();
|
|
|
|
|
|
2021-12-27 18:13:11 -05:00
|
|
|
})();
|