add util.defer() and add slight opacity in wireframe

This commit is contained in:
Stewart Allen 2022-01-02 23:47:28 -05:00
commit 5541e680ce
3 changed files with 32 additions and 9 deletions

View file

@ -32,7 +32,7 @@ function init() {
opacity: 0.1, opacity: 0.1,
color: 0xdddddd, color: 0xdddddd,
zoom: { reverse: true, speed: 1 }, zoom: { reverse: true, speed: 1 },
size: { width: 300, depth: 300, height: 1, maxz: 300 }, size: { width: 2000, depth: 2000, height: 1, maxz: 300 },
grid: { major: 25, minor: 5, colorMajor: 0xcccccc, colorMinor: 0xeeeeee, colorX: 0xffaaaa, colorY: 0xaaaaff }, grid: { major: 25, minor: 5, colorMajor: 0xcccccc, colorMinor: 0xeeeeee, colorX: 0xffaaaa, colorY: 0xaaaaff },
}); });
space.view.setZoom(zoomrev, zoomspd); space.view.setZoom(zoomrev, zoomspd);
@ -99,6 +99,7 @@ function ui_build() {
} }
// listen for api calls // listen for api calls
// todo: switch to util.defer(fn)
broker.listeners({ broker.listeners({
model_add: maker.update, model_add: maker.update,
group_add: maker.update, group_add: maker.update,
@ -150,7 +151,7 @@ function space_init(data) {
selection.focus(); selection.focus();
break; break;
case 'KeyW': case 'KeyW':
selection.wireframe({toggle:true}); selection.wireframe({toggle:true}, {opacity:0.15});
break; break;
case 'KeyB': case 'KeyB':
selection.boundsBox({toggle:true}); selection.boundsBox({toggle:true});

View file

@ -57,7 +57,7 @@ let selection = {
// @param group {MeshObject[]} // @param group {MeshObject[]}
set(objects) { set(objects) {
selected = objects; selected = objects;
selection.update(); util.defer(selection.update);
}, },
// @param group {MeshObject} // @param group {MeshObject}
@ -77,13 +77,13 @@ let selection = {
} }
} }
selected.addOnce(object); selected.addOnce(object);
selection.update(); util.defer(selection.update);
}, },
// @param group {MeshObject} // @param group {MeshObject}
remove(object) { remove(object) {
selected.remove(object); selected.remove(object);
selection.update(); util.defer(selection.update);
}, },
// @param group {MeshObject} // @param group {MeshObject}
@ -96,8 +96,9 @@ let selection = {
}, },
clear() { clear() {
selected = []; for (let s of selected) {
selection.update(); selection.remove(s);
}
}, },
update() { update() {
@ -251,6 +252,8 @@ let api = mesh.api = {
} }
}; };
let deferFn = [];
let util = mesh.util = { let util = mesh.util = {
uuid(segs = 1) { uuid(segs = 1) {
@ -261,6 +264,24 @@ let util = mesh.util = {
return uid.join('-'); return uid.join('-');
}, },
// merge repeated function calls like updates
// that importantly take no arguments
defer(fn, time = 50) {
for (let rec of deferFn) {
if (rec.fn === fn) {
clearTimeout(rec.timer);
deferFn.remove(rec);
break;
}
}
let rec = { fn };
rec.timer = setTimeout(() => {
deferFn.remove(rec);
fn();
});
deferFn.push(rec);
},
// @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]} // @param object {THREE.Object3D | THREE.Object3D[] | MeshObject | MeshObject[]}
// @returns bounds modified for moto.Space // @returns bounds modified for moto.Space
bounds(object) { bounds(object) {

View file

@ -150,7 +150,7 @@ mesh.model = class MeshModel extends mesh.object {
}; };
} }
if (bool.toggle) { if (bool.toggle) {
return this.wireframe(this._wire ? false : true); return this.wireframe(this._wire ? false : true, opt);
} }
if (this._wire) { if (this._wire) {
this.mesh.remove(this._wire); this.mesh.remove(this._wire);
@ -158,6 +158,7 @@ mesh.model = class MeshModel extends mesh.object {
this.opacity(this._wireo); this.opacity(this._wireo);
} }
if (bool) { if (bool) {
this.material({clone:true});
this._wireo = this.opacity(); this._wireo = this.opacity();
this._wire = new THREE.LineSegments( this._wire = new THREE.LineSegments(
new THREE.WireframeGeometry(this.mesh.geometry), new THREE.WireframeGeometry(this.mesh.geometry),
@ -169,7 +170,7 @@ mesh.model = class MeshModel extends mesh.object {
} }
material(mat) { material(mat) {
this.mesh.material = mat; this.mesh.material = mat.clone === true ? this.mesh.material.clone() : mat;
} }
remove() { remove() {