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
|
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
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let mesh = self.mesh = self.mesh || {};
|
|
|
|
|
if (mesh.group) return;
|
|
|
|
|
|
2021-12-31 13:26:07 -05:00
|
|
|
mesh.group = class MeshGroup extends mesh.object {
|
2021-12-30 22:59:46 -05:00
|
|
|
|
2021-12-31 13:26:07 -05:00
|
|
|
// @param group {mesh.model[]}
|
2021-12-31 15:30:21 -05:00
|
|
|
constructor(models = []) {
|
2021-12-31 13:26:07 -05:00
|
|
|
super();
|
2021-12-30 22:59:46 -05:00
|
|
|
this.group = new THREE.Group();
|
|
|
|
|
this.models = [];
|
2021-12-31 15:30:21 -05:00
|
|
|
for (let model of models) {
|
2021-12-30 22:59:46 -05:00
|
|
|
this.add(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 13:26:07 -05:00
|
|
|
object() {
|
|
|
|
|
return this.group;
|
2021-12-30 22:59:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// @param model {MeshModel}
|
|
|
|
|
add(model) {
|
|
|
|
|
model.group = this;
|
|
|
|
|
this.models.addOnce(model);
|
|
|
|
|
this.group.add(model.mesh);
|
|
|
|
|
moto.Space.update();
|
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-01 15:04:24 -05:00
|
|
|
// remove all models
|
|
|
|
|
if (arguments.length === 0) {
|
|
|
|
|
for (let m of this.models.slice()) {
|
|
|
|
|
this.remove(m);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-30 22:59:46 -05:00
|
|
|
model.group = undefined;
|
|
|
|
|
this.models.remove(model);
|
|
|
|
|
this.group.remove(model.mesh);
|
|
|
|
|
// auto-remove group when empty
|
|
|
|
|
if (this.group.children.length === 0) {
|
|
|
|
|
mesh.api.group.remove(this);
|
|
|
|
|
}
|
|
|
|
|
moto.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
|
|
|
|
|
|
|
|
// center objects to group bounds
|
|
|
|
|
// dependent on first being added to world/scene
|
|
|
|
|
centerModels() {
|
|
|
|
|
let bounds = this.bounds();
|
|
|
|
|
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
|
|
|
|
|
|
|
|
material() {
|
|
|
|
|
for (let model of this.models) {
|
|
|
|
|
model.material(...arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-30 22:59:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})();
|