From a5ddd782990c247c4a8177eb7f59faecc92955fc Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Fri, 31 Dec 2021 13:26:07 -0500 Subject: [PATCH] make model/group subclass to inherit common ops --- src/mesh/group.js | 36 +++++---------------------- src/mesh/model.js | 35 +++++--------------------- src/mesh/object.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++ src/moto/webui.js | 3 +++ 4 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 src/mesh/object.js diff --git a/src/mesh/group.js b/src/mesh/group.js index 99abc9cb..cb794bfc 100644 --- a/src/mesh/group.js +++ b/src/mesh/group.js @@ -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} diff --git a/src/mesh/model.js b/src/mesh/model.js index 200b25f9..57df8af7 100644 --- a/src/mesh/model.js +++ b/src/mesh/model.js @@ -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) { diff --git a/src/mesh/object.js b/src/mesh/object.js new file mode 100644 index 00000000..9f284755 --- /dev/null +++ b/src/mesh/object.js @@ -0,0 +1,61 @@ +/** Copyright Stewart Allen -- 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); + } +}; + +})(); diff --git a/src/moto/webui.js b/src/moto/webui.js index 41e7790c..4c9671f7 100644 --- a/src/moto/webui.js +++ b/src/moto/webui.js @@ -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) => {