CADViewComponent/src/Orbit.ts

95 lines
2.2 KiB
TypeScript
Raw Normal View History

import * as THREE from "three";
/**
* ,
* x当抬头或者低头到90度时,.
*
* @class Orbit
*/
export class Orbit
{
//抬头低头 正数抬头 负数低头
private m_RoX: number = 0;
//身体旋转 0为正右边 逆时针旋转
RoZ: number = 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?: THREE.Vector3): THREE.Vector3
{
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: THREE.Vector3)
{
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: THREE.Vector3, up?: THREE.Vector3): THREE.Vector3
{
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;
}
}