CADViewComponent/src/Orbit.ts

95 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-04-30 17:12:30 +08:00
import { MathUtils, Vector3 } from "three";
/**
* ,
* x当抬头或者低头到90度时,.
2020-04-30 17:12:30 +08:00
*
* @class Orbit
*/
export class Orbit
{
//抬头低头 正数抬头 负数低头
private m_RoX: number = 0;
//身体旋转 0为正右边 逆时针旋转
RoZ: number = 0;
get RoX()
{
return this.m_RoX;
}
set RoX(v)
{
2020-04-30 17:12:30 +08:00
this.m_RoX = MathUtils.clamp(v, Math.PI * -0.5, Math.PI * 0.5);
}
/**
* 使
2020-04-30 17:12:30 +08:00
*
* @param {Vector3} [dir] ,,
* @returns {Vector3}
* @memberof Orbit
*/
2020-04-30 17:12:30 +08:00
UpdateDirection(dir?: Vector3): Vector3
{
2020-04-30 17:12:30 +08:00
let rtDir = dir ? dir : new 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;
}
/**
* 使,
2020-04-30 17:12:30 +08:00
*
* @param {Vector3} dir
* @memberof Orbit
*/
2020-04-30 17:12:30 +08:00
UpdateRoValue(dir: 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);
}
/**
2020-04-30 17:12:30 +08:00
*
* .
2020-04-30 17:12:30 +08:00
*
* @static
2020-04-30 17:12:30 +08:00
* @param {Vector3} dir
* @param {Vector3} [up]
* @returns {Vector3}
* @memberof Orbit
*/
2020-04-30 17:12:30 +08:00
static ComputUpDirection(dir: Vector3, up?: Vector3): Vector3
{
2020-04-30 17:12:30 +08:00
let upRes = up ? up : new Vector3();
if (dir.equals(new Vector3(0, 0, -1)))
{
upRes.set(0, 1, 0);
}
2020-04-30 17:12:30 +08:00
else if (dir.equals(new Vector3(0, 0, 1)))
{
upRes.set(0, -1, 0);
}
else
{
2020-04-30 17:12:30 +08:00
let xv = new Vector3();
xv.crossVectors(new Vector3(0, 0, 1), dir);
upRes.crossVectors(dir, xv);
upRes.normalize();
}
return upRes;
}
}