grid-apps-cmms/src/mesh/model.js

145 lines
3.5 KiB
JavaScript
Raw Normal View History

/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
"use strict";
(function() {
gapp.register("mesh.model", [
"add.three", // dep: add.three
"moto.license", // dep: moto.license
2021-12-30 22:59:46 -05:00
"mesh.group", // dep: mesh.group
]);
let mesh = self.mesh = self.mesh || {};
if (mesh.model) return;
/** default materials **/
2021-12-29 17:20:17 -05:00
mesh.material = {
solid: new THREE.MeshPhongMaterial({
transparent: true,
shininess: 100,
specular: 0x181818,
color: 0xffff00,
opacity: 1
})
};
/** 3D model rendered on plaform **/
mesh.model = class MeshModel {
constructor(data) {
let { file, mesh } = data;
if (!mesh) {
dbug.error(`'${file}' missing mesh data`);
return;
}
2021-12-29 17:20:17 -05:00
this.file = file;
this.mesh = this.load(mesh);
}
2021-12-30 22:59:46 -05:00
bounds() {
2021-12-31 00:35:45 -05:00
return mesh.util.bounds(this.mesh);
}
floor() {
let b = this.bounds();
this.move(0, 0, -b.min.z);
return this;
2021-12-31 00:35:45 -05:00
}
centerXY() {
let b = this.bounds();
this.move(-b.center.x, b.center.y, 0);
return this;
2021-12-31 00:35:45 -05:00
}
move(x = 0, y = 0, z = 0) {
let pos = this.position();
pos.set(pos.x + x, pos.y + y, pos.z + z);
return this;
2021-12-31 00:35:45 -05:00
}
position() {
let pos = this.mesh.position;
if (arguments.length === 0) {
return pos;
}
pos.set(...this.arguments);
2021-12-30 22:59:46 -05:00
}
2021-12-29 17:20:17 -05:00
load(vertices, indices) {
let geo = new THREE.BufferGeometry();
if (indices) geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
geo.setAttribute('normal', undefined);
let meh = new THREE.Mesh(geo, mesh.material.solid);
geo.computeFaceNormals();
geo.computeVertexNormals();
meh.material.side = THREE.DoubleSide;
meh.receiveShadow = true;
meh.castShadow = true;
meh.renderOrder = 1;
return meh;
}
get group() {
return this._group;
}
set group(gv) {
2021-12-30 22:59:46 -05:00
if (gv && this._group && this._group !== gv) {
throw "models can only belong to one group";
}
this._group = gv;
}
2021-12-30 22:59:46 -05:00
2021-12-30 23:23:45 -05:00
opacity(ov) {
let mat = this.mesh.material;
if (ov === undefined) {
return mat.opacity;
}
if (ov <= 0.0) {
mat.transparent = false;
mat.opacity = 1;
mat.visible = false;
} else {
mat.transparent = ov < 1.0;
mat.opacity = ov;
mat.visible = true;
}
moto.Space.update();
}
wireframe(bool, opt = {}) {
2021-12-30 23:28:36 -05:00
if (bool === undefined) {
return this._wire ? {
enabled: true,
opacity: this._wireo,
color: this._wire ? this._wire.material.color : undefined,
} : {
enabled: false
};
}
2021-12-30 23:23:45 -05:00
if (this._wire) {
this.mesh.remove(this._wire);
this._wire = undefined;
this.opacity(this._wireo);
}
if (bool) {
this._wireo = this.opacity();
this._wire = new THREE.LineSegments(
new THREE.WireframeGeometry(this.mesh.geometry),
new THREE.LineBasicMaterial({ color: opt.color || 0 }));
this.mesh.add(this._wire);
this.opacity(opt.opacity || 0);
}
moto.Space.update();
}
2021-12-30 22:59:46 -05:00
remove() {
mesh.api.model.remove(this);
}
};
})();