@realsee/dnalogel
Version:
202 lines (201 loc) • 7.99 kB
JavaScript
var f = Object.defineProperty;
var j = (b, e, t) => e in b ? f(b, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : b[e] = t;
var s = (b, e, t) => (j(b, typeof e != "symbol" ? e + "" : e, t), t);
import * as p from "three";
import { Subscribe as u } from "../../Subscribe.js";
import { DomEvents as y } from "../../threex/domevents/index.js";
import { notNil as m } from "../../isNil.js";
import { boundingBox as g } from "../utils/boundingBox.js";
class k {
constructor(e, t) {
s(this, "originObject3D");
s(this, "hooks");
s(this, "disposers", []);
s(this, "helperObject3D");
s(this, "camera");
s(this, "model");
s(this, "scene");
s(this, "container");
s(this, "domEvents");
s(this, "isDragging", !1);
s(this, "enabled", !1);
s(this, "name", "BaseController");
s(this, "boundingBox");
s(this, "offset");
s(this, "onRender");
s(this, "onWantsTapGesture", (e) => {
if (this.getIntersectObject(e))
return !1;
});
var l, d;
this.hooks = (l = e.sharedHooks) != null ? l : new u(), this.camera = e.camera, this.model = e.model, this.originObject3D = e.originObject3D, this.helperObject3D = e.helperObject3D, this.container = e.container, this.scene = e.scene, this.domEvents = new y(e.camera, e.container), this.onRender = (d = e.onRender) != null ? d : () => {
}, this.offset = t == null ? void 0 : t.offset, this.initialHelperPosition(), this.initialHelperQuaternion(), this.enable();
const h = (...c) => this.onSetOriginObjectScale(...c), n = (...c) => this.onSetOriginObjectRotate(...c), r = (...c) => this.onSetOriginObjectPosition(...c), i = (...c) => this.onApplyOriginObjectScale(...c), a = (...c) => this.onApplyOriginObjectRotate(...c), O = (...c) => this.onApplyOriginObjectPosition(...c);
this.hooks.on("setObjectScale", h), this.hooks.on("setObjectRotate", n), this.hooks.on("setObjectPosition", r), this.hooks.on("applyObjectPosition", O), this.hooks.on("applyObjectRotate", a), this.hooks.on("applyObjectScale", i), this.disposers.push(() => {
this.hooks.off("setObjectScale", h), this.hooks.off("setObjectRotate", n), this.hooks.off("setObjectPosition", r), this.hooks.off("applyObjectPosition", O), this.hooks.off("applyObjectRotate", a), this.hooks.off("applyObjectScale", i);
});
}
initialHelperPosition() {
this.helperObject3D.position.add(this.calculateOffset(this.offset)), this.helperObject3D.initialPosition();
}
initialHelperQuaternion() {
this.helperObject3D.initQuaternion();
}
enable() {
this.enabled || (this.enabled = !0, this.scene.add(this.helperObject3D), this.helperObject3D.enable(), this.render());
}
disable() {
this.enabled && (this.enabled = !1, this.helperObject3D.disable(), this.render());
}
show() {
this.helperObject3D.show(), this.render();
}
hide() {
this.helperObject3D.hide(), this.render();
}
dispose() {
var e;
(e = this.disposers) == null || e.forEach((t) => t == null ? void 0 : t()), this.helperObject3D.dispose(), this.render();
}
/**
* @description: applyHelperMatrix4
* @param {THREE.Matrix4} matrix position 偏移量
*/
applyHelperMatrix4(e) {
this.helperObject3D.applyMatrix4(e);
}
/**
* @description: applyHelperQuaternion
* @param {THREE.Quaternion} quaternion 旋转四元数
* @param {THREE.Vector3} origin 旋转中心
*/
applyHelperQuaternion(e, t) {
this.helperObject3D.applyHelperQuaternion(e, t);
}
/**
* @description: applyHelperScaleMatrix4
* @param {THREE.Matrix4} matrix 缩放矩阵
* @param {THREE.Vector3} origin 缩放中心
*/
applyHelperScaleMatrix4(e, t) {
this.helperObject3D.applyHelperScaleMatrix4(e, t);
}
onWantsGesture(e, t, h) {
if (this.isDragging)
return !1;
}
onIntersectionOnModelUpdate(e) {
}
/**
* @description: onApplyOriginObjectScale
* @param {THREE.Matrix4} params.matrix 缩放矩阵
* @param {THREE.Vector3} params.origin 缩放中心
*/
onApplyOriginObjectScale(e) {
this.applyHelperScaleMatrix4(e.matrix, e.origin);
}
/**
* @description: onApplyOriginObjectRotate
* @param {THREE.Quaternion} params.quaternion 旋转四元数
* @param {THREE.Vector3} params.origin 旋转中心
*/
onApplyOriginObjectRotate(e) {
this.applyHelperQuaternion(e.quaternion, e.origin);
}
/**
* @description: onApplyOriginObjectPosition
* @param {THREE.Matrix4} params.matrix position 偏移量
*/
onApplyOriginObjectPosition(e) {
this.applyHelperMatrix4(e.matrix);
}
onSetOriginObjectScale(e) {
this.helperObject3D.scale.copy(e), this.updateOffsetByScale(e);
}
onSetOriginObjectRotate(e, t) {
this.helperObject3D.setHelperQuaternion(e, t);
}
onSetOriginObjectPosition(e) {
this.helperObject3D.position.copy(e.clone().add(this.calculateOffset(this.offset)));
}
render() {
this.onRender();
}
updateOffsetByScale(e) {
if (this.boundingBox && this.offset) {
const t = this.originObject3D.position, h = new p.Vector3().subVectors(this.boundingBox.max, t).multiply(e).add(t), n = new p.Vector3().subVectors(this.boundingBox.min, t).multiply(e).add(t), r = this.calculateOffset(this.offset, { min: n, max: h });
this.helperObject3D.position.copy(r), this.helperObject3D.initialPosition();
}
}
hoverListener(e, t = 16777215, h = 1) {
const n = (Array.isArray(e) ? e : [e]).filter(m), r = [];
for (const i of n)
if (i instanceof p.Mesh || i instanceof p.Group) {
const a = i instanceof p.Group ? i.children.filter((o) => o instanceof p.Mesh) : [i];
a.forEach((o) => {
o.__originalColor__ = o.material.color.clone(), o.__originalOpacity__ = o.material.opacity;
});
const O = () => {
a.forEach((o) => {
o.material.color.set(t), o.material.opacity = h;
}), this.render();
}, l = () => {
a.forEach((o) => {
o.material.color.copy(o.__originalColor__), o.material.opacity = o.__originalOpacity__;
}), this.render();
}, d = () => {
this.isDragging || O();
}, c = () => {
this.isDragging || l();
};
for (const o of a)
this.domEvents.addEventListener(o, "mouseover", d), this.domEvents.addEventListener(o, "mouseout", c), this.hooks.on("moveStart", O), this.hooks.on("moveEnd", l), this.hooks.on("rotateEnd", l), r.push(() => {
this.domEvents.removeEventListener(o, "mouseover", d), this.domEvents.removeEventListener(o, "mouseout", c), this.hooks.off("moveStart", O), this.hooks.off("moveEnd", l), this.hooks.off("rotateEnd", l);
});
}
return () => r.forEach((i) => i == null ? void 0 : i());
}
getIntersectObject(e) {
const t = this.camera.position, h = this.helperObject3D;
if (!h)
return;
const n = this.helperObject3D.raycasterIntersectObject(e)[0];
if (!n)
return;
const r = n;
if (r.object = h, this.model.intersectRaycaster) {
const i = this.model.intersectRaycaster(e)[0];
if (i && i.point.distanceTo(t) < r.point.distanceTo(t))
return;
}
return r.object;
}
calculateOffset(e, t) {
const h = this.originObject3D, n = new p.Vector3(0, 0, 0);
if (!e)
return n;
const r = () => t || (this.boundingBox ? this.boundingBox : (this.boundingBox = g(h), this.boundingBox));
if (typeof e.x == "number")
n.setX(e.x);
else if (typeof e.x == "object") {
const i = r();
n.setX((i.max.x - i.min.x) * e.x.percents);
}
if (typeof e.y == "number")
n.setY(e.y);
else if (typeof e.y == "object") {
const i = r();
n.setY((i.max.y - i.min.y) * e.y.percents);
}
if (typeof e.z == "number")
n.setZ(e.z);
else if (typeof e.z == "object") {
const i = r();
n.setZ((i.max.z - i.min.z) * e.z.percents);
}
return n;
}
}
export {
k as BaseController
};