demo/3d_demo/resources/src/js/model_processor.js

var ModelProcessor = function() {
var context = {}
context.update = function(x, y, z) {
function f(a, b) {
var c = 3;
return b + (a - b) / c
}
o = new THREE.Vector3(Math.min(y, 1e3), Math.max(-x, -1e3), Math.min(-z, 1e3)), o = o.normalize();
var c = new THREE.Matrix4,
d = o.y = f(o.y, p.y),
e = o.x = f(o.x, p.x),
g = o.z;
g *= Math.PI / 2
d *= -Math.PI / 2
e *= -Math.PI / 2
d = Math.atan2(d, g)
e += Math.PI / 2
c.makeRotationFromEuler(new THREE.Euler(e, d, 0, "XYZ"));
var h = new THREE.Matrix4;
h.multiplyMatrices(c, a.matrix), a.setRoatationMatrix(c), p = o, a.needsredraw = !0
}
function a() {
this.needsredraw = !0, this.rotationCenter = new THREE.Vector4, this.matrix = new THREE.Matrix4, this.distance = 600, this.pan = new THREE.Vector4, this.rotateAnimation = !0, this.rotmat = new THREE.Matrix4, this.getAndResetUpdateFlag = function() {
if (this.rotateAnimation) return this.rotateAnimation;
var a = this.needsredraw;
return this.needsredraw = !1, a
}, this.updateCamera = function(a, b) {
this.rotateAnimation === !0 && this.matrix.multiplyMatrices(this.rotmat, this.matrix);
var c = new THREE.Matrix4,
d = new THREE.Matrix4,
e = new THREE.Matrix4;
e.makeTranslation(-this.rotationCenter.x, -this.rotationCenter.y, -this.rotationCenter.z), d.multiplyMatrices(this.matrix, e), d.elements[14] += -this.distance, d.elements[12] += this.pan.x, d.elements[13] += this.pan.y, c.getInverse(d), a.matrix.copy(new THREE.Matrix4), a.applyMatrix(c), a.updateMatrix();
var f = b / 50;
return a.near = Math.max(f, this.distance - b), a.far = this.distance + b, a.updateProjectionMatrix(), this.getAndResetUpdateFlag()
}, this.animationRotate3DPreview = function() {
this.rotateAnimation = !0, this.rotmat.makeRotationY(Math.PI / 180), this.needsredraw = !0
}, this.setRoatationMatrix = function(a) {
this.matrix = a, this.needsredraw = !0
}, this.getCameraParams = function() {
var a = this,
b = 0,
c = 0,
d = new THREE.Euler,
e = new THREE.Matrix4;
e.copy(this.matrix), d.setFromRotationMatrix(e, "ZYX");
var f = {
roll: -d.x / Math.PI * 180,
pitch: d.y / Math.PI * 180,
yaw: -d.z / Math.PI * 180
};
return {
camera: "perpective",
rotation: f,
rotation_center: {
x: a.rotationCenter.x,
y: a.rotationCenter.y,
z: a.rotationCenter.z
},
focal_length: -a.distance,
pan: {
x: a.pan.x,
y: a.pan.y
},
frustum: {
width: b,
height: c
}
}
}, this.setCameraParams = function(a) {
var b = new THREE.Euler(-a.rotation.roll * Math.PI / 180, a.rotation.pitch * Math.PI / 180, -a.rotation.yaw * Math.PI / 180, "ZYX");
this.matrix.makeRotationFromEuler(b), this.rotationCenter.x = a.rotation_center.x, this.rotationCenter.y = a.rotation_center.y, this.rotationCenter.z = a.rotation_center.z, this.distance = -a.focal_length, this.pan.x = a.pan.x, this.pan.y = a.pan.y, this.needsredraw = !0
}
}
function b(b, c) {
function j(a) {}
function k(a) {}
function l(b) {
if (u.enabled !== !1) {
b.preventDefault(), b.stopPropagation();
var c = new THREE.Vector2,
d = new THREE.Matrix4,
e = 0,
f = 0;
if (u.getMouseOnScreen(b.pageX, b.pageY, c), !(B === !1 && Math.abs(c.x - A.x) < .01 && Math.abs(c.y - A.y) < .01)) {
if (B = !0, w === v.ROTATE) {
f = c.y - z.y, e = c.x - z.x, e *= 2 * Math.PI, f *= 2 * Math.PI, d.makeRotationFromEuler(new THREE.Euler(f, e, 0, "XYZ"));
var g = new THREE.Matrix4;
g.multiplyMatrices(d, a.matrix), a.setRoatationMatrix(g), z = c, a.needsredraw = !0
}
if (w === v.PAN) {
f = c.y - z.y, e = c.x - z.x;
var h = a.distance,
j = Math.tan(i.fov / 180 * Math.PI / 2),
k = h / j;
e *= k, f *= k, a.pan.x += e, a.pan.y -= f, z = c, a.needsredraw = !0
}
w === v.ZOOM && (f = c.y - z.y, a.distance *= 1 + f, z = c, a.needsredraw = !0), w === v.NONE && (Math.abs(u.toolTipStartPos.x - c.x) > 5 || Math.abs(u.toolTipStartPos.y - c.y) > 5) && (u.toolTipStartPos = c, window.performance.now ? u.toolTipStartPosTime = window.performance.now() : u.toolTipStartPosTime = Date.now())
}
}
}
function m(b) {
if (u.enabled !== !1) {
if (b.preventDefault(), b.stopPropagation(), B === !1) return u.onClickEvent(b), a.needsredraw = !0, document.removeEventListener("mousemove", l), document.removeEventListener("mouseup", m), void(w = v.NONE);
w === v.PAN ? (u.getMouseOnScreen(b.pageX, b.pageY, z), w = 0 === b.button ? v.ZOOM : v.ROTATE) : (w = v.NONE, document.removeEventListener("mousemove", l), document.removeEventListener("mouseup", m), u.dispatchEvent(y)), a.needsredraw = !0
}
}
function n(a) {
u.enabled !== !1 && (a.preventDefault(), a.stopPropagation(), 0 !== a.button && 2 !== a.button || (C.x = a.pageX, C.y = a.pageY, u.getMouseOnScreen(a.pageX, a.pageY, z), A.x = z.x, A.y = z.y, B = !1, document.addEventListener("mousemove", l, !1), document.addEventListener("mouseup", m, !1), u.dispatchEvent(x), 0 === a.button ? w = w === v.ZOOM ? v.PAN : v.ROTATE : 2 === a.button && (w = w === v.ROTATE ? v.PAN : v.ZOOM)))
}
function q(b) {
if (u.enabled !== !1) {
b.preventDefault(), b.stopPropagation();
var c = 0;
b.wheelDelta ? c = b.wheelDelta / 40 : b.detail && (c = -b.detail / 3), a.distance *= 1 + c / 30, console.log(a.distance), u.dispatchEvent(x), u.dispatchEvent(y), a.needsredraw = !0
}
}
function r(b) {
a.needsredraw = !0
}
function s(b) {
a.needsredraw = !0
}
function t(b) {
a.needsredraw = !0
}
var u = this,
v = {
NONE: -1,
ROTATE: 0,
ZOOM: 2,
PAN: 1,
TOUCH_ROTATE: 3,
TOUCH_ZOOM: 4,
TOUCH_PAN: 5
};
this.__cameraMover = b.__cameraMover, this.camera = b.camera, this.viewer = b, this.domElement = void 0 !== c ? c : document, this.enabled = !0, this.screen = {
left: 0,
top: 0,
width: 0,
height: 0
}, this.toolTipStartPos = new THREE.Vector2, this.toolTipStartPosTime = -1;
var w = v.NONE,
x = (v.NONE, new THREE.Projector, {
type: "start"
}),
y = {
type: "end"
};
p = new THREE.Vector3;
var z = new THREE.Vector2,
A = new THREE.Vector2,
B = !1,
C = new THREE.Vector2;
this.handleResize = function() {
if (this.domElement === document) this.screen.left = 0, this.screen.top = 0, this.screen.width = window.innerWidth, this.screen.height = window.innerHeight;
else {
var b = this.domElement.getBoundingClientRect(),
c = this.domElement.ownerDocument.documentElement;
this.screen.left = b.left + window.pageXOffset - c.clientLeft, this.screen.top = b.top + window.pageYOffset - c.clientTop, this.screen.width = b.width, this.screen.height = b.height
}
a.needsredraw = !0
}, this.handleEvent = function(a) {
"function" == typeof this[a.type] && this[a.type](a)
}, this.getMouseOnScreen = function(a, b, c) {
var d = 1 / u.screen.width,
e = 1 / u.screen.height;
return c.set((a - u.screen.left) * d, (b - u.screen.top) * e)
}, this.onLClickObject = function(a, c) {
return void 0 !== b.signals ? b.signals.onLClickObject(a, c) : !1
}, this.onContextMenuObject = function(a, c) {
return void 0 !== b.signals ? b.signals.onContextMenuObject(a, c) : !1
}, this.onClickObject = function(a, b) {
var c = !0;
return void 0 !== b && (c = b.object.visible), c === !0 ? 2 === a.button ? this.onContextMenuObject(a, b) : this.onLClickObject(a, b) : !1
}, this.onClickEvent = function(a) {}, this.update = function() {}, this.domElement.addEventListener("contextmenu", function(a) {
a.preventDefault()
}, !1),
this.domElement.addEventListener("mousedown", n, !1), this.domElement.addEventListener("mousewheel", q, !1), this.domElement.addEventListener("DOMMouseScroll", q, !1), this.domElement.addEventListener("touchstart", r, !1), this.domElement.addEventListener("touchend", t, !1), this.domElement.addEventListener("touchmove", s, !1), window.addEventListener("keydown", j, !1), window.addEventListener("keyup", k, !1), this.handleResize(), this.update()
}
function c(a) {
var b = 0;
if (a instanceof THREE.Mesh) {
var d = a.geometry;
return d.computeBoundingBox(), d.boundingBox
}
if (a instanceof THREE.Object3D) {
var e = new THREE.Box3;
for (b = 0; b < a.children.length; b++) {
var f = a.children[b],
g = c(f);
g.applyMatrix4(f.matrix), e.union(g)
}
return e
}
}
function d(b) {
void 0 === b && (b = _this.meshes_root);
var d = c(b);
if (!d.empty()) {
var e = d.center();
a.rotationCenter = e;
var f = .5 * d.size().length();
n = f;
var g = f / Math.sin(Math.PI / 180 * i.fov * .5),
h = 2 * Math.atan(i.aspect * Math.tan(i.fov / 180 * Math.PI / 2));
h *= 180 / Math.PI;
var j = f / Math.sin(Math.PI / 180 * h * .5);
j > g && (g = j), a.distance = g, a.pan.x = 0, a.pan.y = 0, a.needsredraw = !0
}
}
function e() {
i = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, .01, 1e10), i.position.z = 6, a = new a, k = new THREE.Scene, k.add(i);
var c = new THREE.DirectionalLight(16777215);
c.position.set(200, 200, 1e3).normalize(), c.castShadow = !1, c.shadowDarkness = .5, c.shadowCameraNear = 3, c.shadowCameraFar = 500, c.shadowMapBias = .0039, c.shadowMapDarkness = .5, c.shadowMapWidth = 1024, c.shadowMapHeight = 1024, i.add(c), i.add(c.target);
var e = new THREE.ObjectLoader,
g = e.parse(data_model);
k.add(g), d(k), l = new THREE.WebGLRenderer({
antialias: !0,
logarithmicDepthBuffer: !1,
alpha: !0
}),
l.setSize(window.innerWidth, window.innerHeight),
l.shadowMapEnabled = !0, l.shadowMapSoft = !0,
h = document.createElement("div"),
document.body.appendChild(h),
h.appendChild(l.domElement),
camerMoverControll = new b(this, l.domElement),
window.addEventListener("resize", f, !1)
}
function f() {
i.aspect = window.innerWidth / window.innerHeight, i.updateProjectionMatrix(), l.setSize(window.innerWidth, window.innerHeight), a.needsredraw = !0
}
function g() {
requestAnimationFrame(g), a.updateCamera(i, 2 * n) && l.render(k, i)
}
b.prototype = Object.create(THREE.EventDispatcher.prototype);
var h, i, j, k, l, m, b, a, n, o, p;
context.e = e()
context.g = g()
context.config = {
container: h,
camera: i,
controls: j,
scene: k,
renderer: l,
stage: m,
_cameraMoverControl: b,
_cameraMover: a,
sphereRad: n
}
return context
}