2021-12-30 22:59:46 -05:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
gapp.register("mesh.group", [
|
|
|
|
|
"add.array", // dep: add.array
|
|
|
|
|
"add.three", // dep: add.three
|
|
|
|
|
"moto.license", // dep: moto.license
|
2022-01-02 16:30:05 -05:00
|
|
|
"moto.broker", // dep: moto.broker
|
2021-12-31 13:26:07 -05:00
|
|
|
"mesh.object", // dep: mesh.object
|
2021-12-30 22:59:46 -05:00
|
|
|
"mesh.model", // dep: mesh.model
|
2022-01-01 22:39:49 -05:00
|
|
|
"mesh.api", // dep: mesh.api
|
2021-12-30 22:59:46 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let mesh = self.mesh = self.mesh || {};
|
|
|
|
|
if (mesh.group) return;
|
|
|
|
|
|
2022-01-12 23:02:08 -05:00
|
|
|
let { Quaternion, Group, Vector3 } = THREE;
|
2022-01-02 16:30:05 -05:00
|
|
|
let broker = gapp.broker;
|
|
|
|
|
let call = broker.send;
|
2022-01-02 14:25:28 -05:00
|
|
|
let space = moto.Space;
|
|
|
|
|
let worker = moto.client.fn;
|
2022-01-12 23:02:08 -05:00
|
|
|
let lookUp = new Vector3(0,0,-1);
|
2022-01-02 14:25:28 -05:00
|
|
|
|
2021-12-31 13:26:07 -05:00
|
|
|
mesh.group = class MeshGroup extends mesh.object {
|
2021-12-30 22:59:46 -05:00
|
|
|
|
2022-01-02 16:30:05 -05:00
|
|
|
// @param group {MeshModel[]}
|
2022-01-11 21:04:24 -05:00
|
|
|
constructor(models = [], id, name) {
|
2022-01-03 17:13:50 -05:00
|
|
|
super(id);
|
2022-01-12 23:02:08 -05:00
|
|
|
this.group3 = new Group();
|
2021-12-30 22:59:46 -05:00
|
|
|
this.models = [];
|
2022-01-11 21:04:24 -05:00
|
|
|
this.name = name;
|
2021-12-31 15:30:21 -05:00
|
|
|
for (let model of models) {
|
2021-12-30 22:59:46 -05:00
|
|
|
this.add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:58:37 -05:00
|
|
|
get type() {
|
2022-01-02 14:25:28 -05:00
|
|
|
return "group";
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:58:37 -05:00
|
|
|
get object() {
|
2022-01-10 16:56:09 -05:00
|
|
|
return this.group3;
|
2021-12-30 22:59:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @param model {MeshModel}
|
|
|
|
|
add(model) {
|
|
|
|
|
model.group = this;
|
|
|
|
|
this.models.addOnce(model);
|
2022-01-10 16:56:09 -05:00
|
|
|
this.group3.add(model.mesh);
|
2022-01-02 14:25:28 -05:00
|
|
|
space.update();
|
2022-01-02 16:30:05 -05:00
|
|
|
call.model_add({model, group:this});
|
2022-01-02 14:25:28 -05:00
|
|
|
worker.group_add({id: this.id, model:model.id});
|
2022-01-03 17:13:50 -05:00
|
|
|
// update data store
|
|
|
|
|
mesh.db.space.put(this.id, this.models.map(m => m.id));
|
2021-12-31 14:12:42 -05:00
|
|
|
return this;
|
2021-12-30 22:59:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @param model {MeshModel}
|
|
|
|
|
remove(model) {
|
2022-01-03 17:13:50 -05:00
|
|
|
// console.log('group',this,'remove',model);
|
2022-01-01 23:48:36 -05:00
|
|
|
// remove all models and group
|
2022-01-01 15:04:24 -05:00
|
|
|
if (arguments.length === 0) {
|
|
|
|
|
for (let m of this.models.slice()) {
|
|
|
|
|
this.remove(m);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-30 22:59:46 -05:00
|
|
|
this.models.remove(model);
|
2022-01-10 16:56:09 -05:00
|
|
|
this.group3.remove(model.mesh);
|
2022-01-02 16:30:05 -05:00
|
|
|
// create message for listeners
|
|
|
|
|
call.model_remove({model, group:this});
|
2022-01-02 00:40:43 -05:00
|
|
|
// trigger sync with worker
|
2022-01-03 17:13:50 -05:00
|
|
|
worker.group_remove({id: this.id, model: model.id});
|
|
|
|
|
// ensure worker cleanup of model
|
2022-01-02 00:40:43 -05:00
|
|
|
model.remove(true);
|
2021-12-30 22:59:46 -05:00
|
|
|
// auto-remove group when empty
|
2022-01-10 16:56:09 -05:00
|
|
|
if (this.group3.children.length === 0) {
|
2021-12-30 22:59:46 -05:00
|
|
|
mesh.api.group.remove(this);
|
2022-01-09 21:05:14 -05:00
|
|
|
// manage lifecycle with worker, mesh app caches, etc
|
|
|
|
|
this.destroy();
|
2022-01-03 17:13:50 -05:00
|
|
|
} else {
|
|
|
|
|
// update data store
|
|
|
|
|
mesh.db.space.put(this.id, this.models.map(m => m.id));
|
2021-12-30 22:59:46 -05:00
|
|
|
}
|
2022-01-02 14:25:28 -05:00
|
|
|
space.update();
|
2021-12-31 14:12:42 -05:00
|
|
|
return this;
|
2021-12-30 22:59:46 -05:00
|
|
|
}
|
2021-12-31 16:59:38 -05:00
|
|
|
|
2022-01-12 23:02:08 -05:00
|
|
|
// rotate model so face normal points toward floor
|
|
|
|
|
faceDown(normal) {
|
|
|
|
|
let q = new Quaternion().setFromUnitVectors(normal, lookUp);
|
|
|
|
|
this.qrotation(q);
|
|
|
|
|
this.floor();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 22:18:12 -05:00
|
|
|
promote() {
|
2022-01-16 22:58:37 -05:00
|
|
|
this.centerModels();
|
|
|
|
|
let { dim } = this.bounds;
|
|
|
|
|
let { x, y, z } = dim;
|
|
|
|
|
let max = Math.max(x, y, z);
|
|
|
|
|
if (max < 2) {
|
|
|
|
|
mesh.api.log.emit(`auto-scaling import from ${max.round(5)}`);
|
|
|
|
|
this.scale(1000, 1000, 1000);
|
|
|
|
|
}
|
|
|
|
|
return this.centerXY().floor();
|
2022-01-11 22:18:12 -05:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 16:59:38 -05:00
|
|
|
// center objects to group bounds
|
|
|
|
|
// dependent on first being added to world/scene
|
|
|
|
|
centerModels() {
|
2022-01-05 17:58:37 -05:00
|
|
|
let bounds = this.bounds;
|
2021-12-31 16:59:38 -05:00
|
|
|
for (let model of this.models) {
|
|
|
|
|
model.center(bounds);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opacity() {
|
|
|
|
|
for (let model of this.models) {
|
|
|
|
|
model.opacity(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wireframe() {
|
|
|
|
|
for (let model of this.models) {
|
|
|
|
|
model.wireframe(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 17:14:55 -05:00
|
|
|
|
2022-01-14 14:14:13 -05:00
|
|
|
normals() {
|
|
|
|
|
for (let model of this.models) {
|
|
|
|
|
model.normals(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 11:59:26 -05:00
|
|
|
select() {
|
2021-12-31 17:14:55 -05:00
|
|
|
for (let model of this.models) {
|
2022-01-13 11:59:26 -05:00
|
|
|
model.select(...arguments);
|
2021-12-31 17:14:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-30 22:59:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|