start mesh api. add function wrapper for array arguments unroll

This commit is contained in:
Stewart Allen 2021-12-29 21:52:24 -05:00
commit a1644ed609
6 changed files with 83 additions and 7 deletions

View file

@ -173,6 +173,24 @@
return this.filter(v => isNaN(v)).length > 0; return this.filter(v => isNaN(v)).length > 0;
}; };
// wrap a function and unroll the first parameter into
// successive function calls if it is an array
Array.handle = function(fn) {
return function() {
let args = [...arguments];
let val = args.shift();
if (Array.isArray(val)) {
let rv = [];
for (let v of val.slice()) {
rv.push(fn(v, ...args));
}
return rv;
} else {
return fn(val, ...args);
}
}
};
for (let i in AP) { for (let i in AP) {
Object.defineProperty(Array.prototype, i, { Object.defineProperty(Array.prototype, i, {
value: AP[i], value: AP[i],

View file

@ -85,8 +85,7 @@ broker.subscribe('space_init', data => {
// add object loader // add object loader
broker.subscribe('space_load', data => { broker.subscribe('space_load', data => {
for (let od of data) { for (let od of data) {
let obj = new mesh.object(od); mesh.api.models.add(new mesh.model(od));
moto.Space.world.add(obj.mesh);
} }
}); });
@ -107,7 +106,8 @@ gapp.finalize("main.mesh", [
"moto.client", // dep: moto.client "moto.client", // dep: moto.client
"moto.broker", // dep: moto.broker "moto.broker", // dep: moto.broker
"moto.space", // dep: moto.space "moto.space", // dep: moto.space
"mesh.object", // dep: mesh.object "mesh.api", // dep: mesh.api
"mesh.model", // dep: mesh.model
"load.file", // dep: load.file "load.file", // dep: load.file
]); ]);

42
src/mesh/api.js Normal file
View file

@ -0,0 +1,42 @@
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
(function() {
// dep: ext.three
// dep: ext.three-bgu
gapp.register("mesh.api", [
"moto.license", // dep: moto.license
"moto.client", // dep: moto.client
"moto.space", // dep: moto.space
"mesh.tool", // dep: mesh.tool
"add.array", // dep: add.array
]);
let mesh = self.mesh = self.mesh || {};
if (mesh.api) return;
let space = moto.Space;
let models = [];
let api = mesh.api = {
models: {
all: models,
add: model => {
if (!models.contains(model)) {
space.model.add(model.mesh);
models.push(model);
}
},
remove: Array.handle(model => {
space.model.remove(model.mesh);
models.remove(model);
})
}
};
})();

View file

@ -4,14 +4,15 @@
(function() { (function() {
gapp.register("mesh.object", [ gapp.register("mesh.model", [
"add.three", // dep: add.three "add.three", // dep: add.three
"moto.license", // dep: moto.license "moto.license", // dep: moto.license
]); ]);
let mesh = self.mesh = self.mesh || {}; let mesh = self.mesh = self.mesh || {};
if (mesh.object) return; if (mesh.model) return;
/** default materials **/
mesh.material = { mesh.material = {
solid: new THREE.MeshPhongMaterial({ solid: new THREE.MeshPhongMaterial({
transparent: true, transparent: true,
@ -22,7 +23,8 @@ mesh.material = {
}) })
}; };
mesh.object = class MeshObject { /** 3D model rendered on plaform **/
mesh.model = class MeshModel {
constructor(data) { constructor(data) {
let { file, mesh } = data; let { file, mesh } = data;

View file

@ -8,8 +8,9 @@
gapp.register("mesh.tool"); gapp.register("mesh.tool");
let mesh = self.mesh = self.mesh || {}; let mesh = self.mesh = self.mesh || {};
if (mesh.object) return; if (mesh.tool) return;
/** tool for identiying defects and healing them **/
mesh.tool = class MeshTool { mesh.tool = class MeshTool {
constructor(params = {}) { constructor(params = {}) {
this.precision = Math.pow(10, params.precision || 6); this.precision = Math.pow(10, params.precision || 6);

View file

@ -954,6 +954,19 @@
active: updateLastAction active: updateLastAction
}, },
// wraps WORLD add/remove with immediate refresh
model: {
add: mesh => {
WORLD.add(mesh);
refresh();
},
remove: (mesh) => {
WORLD.remove(mesh);
refresh();
}
},
world: WORLD, world: WORLD,
platform: { platform: {