81 lines
2.2 KiB
JavaScript
81 lines
2.2 KiB
JavaScript
![]() |
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const THREE = require("three");
|
||
|
/**
|
||
|
* 轨道控制的数学类,观察向量和角度的互相转换
|
||
|
* 当x当抬头或者低头到90度时,触发万向锁.
|
||
|
*
|
||
|
* @class Orbit
|
||
|
*/
|
||
|
class Orbit {
|
||
|
constructor() {
|
||
|
//抬头低头 正数抬头 负数低头
|
||
|
this.m_RoX = 0;
|
||
|
//身体旋转 0为正右边 逆时针旋转
|
||
|
this.RoZ = 0;
|
||
|
}
|
||
|
get RoX() {
|
||
|
return this.m_RoX;
|
||
|
}
|
||
|
set RoX(v) {
|
||
|
this.m_RoX = THREE.Math.clamp(v, Math.PI * -0.5, Math.PI * 0.5);
|
||
|
}
|
||
|
/**
|
||
|
* 使用旋转角度 计算观察向量
|
||
|
*
|
||
|
* @param {THREE.Vector3} [dir] 引用传入,如果传入,那么就不构造新的向量
|
||
|
* @returns {THREE.Vector3} 返回观察向量
|
||
|
* @memberof Orbit
|
||
|
*/
|
||
|
UpdateDirection(dir) {
|
||
|
let rtDir = dir ? dir : new THREE.Vector3();
|
||
|
rtDir.z = Math.sin(this.m_RoX);
|
||
|
//归一化专用.
|
||
|
let d = Math.abs(Math.cos(this.m_RoX));
|
||
|
rtDir.x = Math.cos(this.RoZ) * d;
|
||
|
rtDir.y = Math.sin(this.RoZ) * d;
|
||
|
return rtDir;
|
||
|
}
|
||
|
/**
|
||
|
* 使用观察向量,计算旋转角度
|
||
|
*
|
||
|
* @param {THREE.Vector3} dir
|
||
|
* @memberof Orbit
|
||
|
*/
|
||
|
UpdateRoValue(dir) {
|
||
|
dir.normalize();
|
||
|
this.m_RoX = Math.asin(dir.z);
|
||
|
if (dir.x < 1e-4 && dir.y < 1e-4)
|
||
|
this.RoZ = Math.PI * 0.5;
|
||
|
else
|
||
|
this.RoZ = Math.atan2(dir.y, dir.x);
|
||
|
}
|
||
|
/**
|
||
|
*
|
||
|
* 根据观察向量 求头部的向量.
|
||
|
*
|
||
|
* @static
|
||
|
* @param {THREE.Vector3} dir
|
||
|
* @param {THREE.Vector3} [up]
|
||
|
* @returns {THREE.Vector3}
|
||
|
* @memberof Orbit
|
||
|
*/
|
||
|
static ComputUpDirection(dir, up) {
|
||
|
let upRes = up ? up : new THREE.Vector3();
|
||
|
if (dir.equals(new THREE.Vector3(0, 0, -1))) {
|
||
|
upRes.set(0, 1, 0);
|
||
|
}
|
||
|
else if (dir.equals(new THREE.Vector3(0, 0, 1))) {
|
||
|
upRes.set(0, -1, 0);
|
||
|
}
|
||
|
else {
|
||
|
let xv = new THREE.Vector3();
|
||
|
xv.crossVectors(new THREE.Vector3(0, 0, 1), dir);
|
||
|
upRes.crossVectors(dir, xv);
|
||
|
upRes.normalize();
|
||
|
}
|
||
|
return upRes;
|
||
|
}
|
||
|
}
|
||
|
exports.Orbit = Orbit;
|
||
|
//# sourceMappingURL=Orbit.js.map
|