make model/group subclass to inherit common ops

This commit is contained in:
Stewart Allen 2021-12-31 13:26:07 -05:00
commit a5ddd78299
4 changed files with 76 additions and 59 deletions

View file

@ -8,16 +8,18 @@ gapp.register("mesh.group", [
"add.array", // dep: add.array
"add.three", // dep: add.three
"moto.license", // dep: moto.license
"mesh.object", // dep: mesh.object
"mesh.model", // dep: mesh.model
]);
let mesh = self.mesh = self.mesh || {};
if (mesh.group) return;
mesh.group = class MeshGroup {
mesh.group = class MeshGroup extends mesh.object {
// @param group {THREE.Group}
// @param group {mesh.model[]}
constructor(models) {
super();
this.group = new THREE.Group();
this.models = [];
for (let model of (models || [])) {
@ -25,34 +27,8 @@ mesh.group = class MeshGroup {
}
}
bounds() {
return mesh.util.bounds(this.group);
}
floor() {
let b = this.bounds();
this.move(0, 0, -b.min.z);
return this;
}
centerXY() {
let b = this.bounds();
this.move(-b.center.x, b.center.y, 0);
return this;
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
}
position() {
let pos = this.group.position;
if (arguments.length === 0) {
return pos;
}
pos.set(...arguments);
object() {
return this.group;
}
// @param model {MeshModel}

View file

@ -5,8 +5,10 @@
(function() {
gapp.register("mesh.model", [
"add.array", // dep: add.array
"add.three", // dep: add.three
"moto.license", // dep: moto.license
"mesh.object", // dep: mesh.object
"mesh.group", // dep: mesh.group
]);
@ -25,8 +27,9 @@ mesh.material = {
};
/** 3D model rendered on plaform **/
mesh.model = class MeshModel {
mesh.model = class MeshModel extends mesh.object {
constructor(data) {
super();
let { file, mesh } = data;
if (!mesh) {
@ -38,34 +41,8 @@ mesh.model = class MeshModel {
this.mesh = this.load(mesh);
}
bounds() {
return mesh.util.bounds(this.mesh);
}
floor() {
let b = this.bounds();
this.move(0, 0, -b.min.z);
return this;
}
centerXY() {
let b = this.bounds();
this.move(-b.center.x, b.center.y, 0);
return this;
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
}
position() {
let pos = this.mesh.position;
if (arguments.length === 0) {
return pos;
}
pos.set(...this.arguments);
object() {
return this.mesh;
}
load(vertices, indices) {

61
src/mesh/object.js Normal file
View file

@ -0,0 +1,61 @@
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
(function() {
gapp.register("mesh.object", [
"add.three", // dep: add.three
"moto.license", // dep: moto.license
]);
let mesh = self.mesh = self.mesh || {};
if (mesh.object) return;
mesh.object = class MeshObject {
// @returns {THREE.Object3D}
object() {
throw "object() requires implementation";
}
bounds() {
return mesh.util.bounds(this.object());
}
floor() {
let b = this.bounds();
this.move(0, 0, -b.min.z);
return this;
}
centerXY() {
let b = this.bounds();
this.move(-b.center.x, b.center.y, 0);
return this;
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
}
scale() {
let scale = this.object().scale;
if (arguments.length === 0) {
return scale;
}
scale.set(...arguments);
}
position() {
let pos = this.object().position;
if (arguments.length === 0) {
return pos;
}
pos.set(...arguments);
}
};
})();

View file

@ -96,13 +96,16 @@ let h = self.h = moto.webui = {
} else {
el.innerHTML = html;
}
let map = {};
for (let bind of ctx) {
let { elid, func } = bind;
let et = $(elid);
map[elid] = et;
for (let [name, fn] of Object.entries(func)) {
et[name] = fn;
}
}
return map;
},
el: (type, attr, innr) => {