|
|
@ -1,10 +1,19 @@
|
|
|
|
import { Box3, BufferGeometry, Geometry, Line, Matrix4, Mesh, Object3D, Vector, Vector2, Vector3, Scene } from 'three';
|
|
|
|
import { Box3, BufferGeometry, Geometry, Line, Matrix4, Mesh, Object3D, Vector, Vector2, Vector3 } from 'three';
|
|
|
|
import { ToFixed } from '../Common/Utils';
|
|
|
|
import { ToFixed } from '../Common/Utils';
|
|
|
|
import { Matrix2 } from './Matrix2';
|
|
|
|
|
|
|
|
export const cZeroVec = new Vector3();
|
|
|
|
export const ZeroVec = new Vector3();
|
|
|
|
export const cXAxis = new Vector3(1, 0, 0);
|
|
|
|
export const XAxis = new Vector3(1, 0, 0);
|
|
|
|
export const cYAxis = new Vector3(0, 1, 0);
|
|
|
|
export const YAxis = new Vector3(0, 1, 0);
|
|
|
|
export const cZAxis = new Vector3(0, 0, 1);
|
|
|
|
export const ZAxis = new Vector3(0, 0, 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function AsVector2(p: { x: number, y: number })
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new Vector2(p.x, p.y);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export function AsVector3(p: { x: number, y: number, z?: number })
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return new Vector3(p.x, p.y, p.z);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 判断一维线段a和b是否存在交集
|
|
|
|
* 判断一维线段a和b是否存在交集
|
|
|
@ -16,16 +25,21 @@ export function isIntersect(amin: number, amax: number, bmin: number, bmax: numb
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 旋转一个点,旋转中心在原点
|
|
|
|
* 旋转一个点,旋转中心在原点
|
|
|
|
*
|
|
|
|
* @param {Vector3} p 点
|
|
|
|
* @export
|
|
|
|
* @param {number} a 角度.
|
|
|
|
* @param {Vector3} pt 点
|
|
|
|
|
|
|
|
* @param {number} ang 角度.
|
|
|
|
|
|
|
|
* @returns {Vector3} 返回pt不拷贝.
|
|
|
|
* @returns {Vector3} 返回pt不拷贝.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function rotatePoint(pt: Vector3, ang: number): Vector3
|
|
|
|
export function rotatePoint(p: Vector3, a: number): Vector3
|
|
|
|
{
|
|
|
|
{
|
|
|
|
new Matrix2().setRotate(ang).applyVector(pt);
|
|
|
|
let s = Math.sin(a);
|
|
|
|
return pt;
|
|
|
|
let c = Math.cos(a);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let x = p.x * c - p.y * s;
|
|
|
|
|
|
|
|
let y = p.x * s + p.y * c;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.x = x;
|
|
|
|
|
|
|
|
p.y = y;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function equaln(v1: number, v2: number, fuzz = 1e-5)
|
|
|
|
export function equaln(v1: number, v2: number, fuzz = 1e-5)
|
|
|
@ -82,14 +96,14 @@ export function angleTo(v1: Vector3, v2: Vector3, ref: Vector3 = new Vector3(0,
|
|
|
|
ref.multiplyScalar(-1);
|
|
|
|
ref.multiplyScalar(-1);
|
|
|
|
let up = getLoocAtUpVec(ref);
|
|
|
|
let up = getLoocAtUpVec(ref);
|
|
|
|
let refOcs = new Matrix4();
|
|
|
|
let refOcs = new Matrix4();
|
|
|
|
refOcs.lookAt(cZeroVec, ref, up);
|
|
|
|
refOcs.lookAt(ZeroVec, ref, up);
|
|
|
|
let refOcsInv = new Matrix4().getInverse(refOcs);
|
|
|
|
let refOcsInv = new Matrix4().getInverse(refOcs);
|
|
|
|
v1.applyMatrix4(refOcsInv);
|
|
|
|
v1.applyMatrix4(refOcsInv);
|
|
|
|
v2.applyMatrix4(refOcsInv);
|
|
|
|
v2.applyMatrix4(refOcsInv);
|
|
|
|
v1.z = 0;
|
|
|
|
v1.z = 0;
|
|
|
|
v2.z = 0;
|
|
|
|
v2.z = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (v1.equals(cZeroVec) || v2.equals(cZeroVec))
|
|
|
|
if (v1.equals(ZeroVec) || v2.equals(ZeroVec))
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
let cv = new Vector3().crossVectors(v1, v2).normalize();
|
|
|
|
let cv = new Vector3().crossVectors(v1, v2).normalize();
|
|
|
|
return cv.z === 0 ? v1.angleTo(v2) : v1.angleTo(v2) * cv.z;
|
|
|
|
return cv.z === 0 ? v1.angleTo(v2) : v1.angleTo(v2) * cv.z;
|
|
|
@ -97,23 +111,23 @@ export function angleTo(v1: Vector3, v2: Vector3, ref: Vector3 = new Vector3(0,
|
|
|
|
|
|
|
|
|
|
|
|
export function getLoocAtUpVec(dir: Vector3): Vector3
|
|
|
|
export function getLoocAtUpVec(dir: Vector3): Vector3
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (dir.equals(cZeroVec))
|
|
|
|
if (dir.equals(ZeroVec))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw ("zero vector")
|
|
|
|
throw ("zero vector")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let norm = dir.clone().normalize();
|
|
|
|
let norm = dir.clone().normalize();
|
|
|
|
if (norm.equals(cZAxis))
|
|
|
|
if (norm.equals(ZAxis))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return new Vector3(0, 1, 0);
|
|
|
|
return new Vector3(0, 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (norm.equals(cZAxis.clone().negate()))
|
|
|
|
else if (norm.equals(ZAxis.clone().negate()))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return new Vector3(0, -1, 0);
|
|
|
|
return new Vector3(0, -1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let xv: Vector3 = new Vector3();
|
|
|
|
let xv: Vector3 = new Vector3();
|
|
|
|
xv.crossVectors(cZAxis, norm);
|
|
|
|
xv.crossVectors(ZAxis, norm);
|
|
|
|
|
|
|
|
|
|
|
|
let up = new Vector3();
|
|
|
|
let up = new Vector3();
|
|
|
|
up.crossVectors(norm, xv);
|
|
|
|
up.crossVectors(norm, xv);
|
|
|
@ -125,7 +139,7 @@ export function createLookAtMat4(dir: Vector3): Matrix4
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let up = getLoocAtUpVec(dir);
|
|
|
|
let up = getLoocAtUpVec(dir);
|
|
|
|
let mat = new Matrix4();
|
|
|
|
let mat = new Matrix4();
|
|
|
|
mat.lookAt(cZeroVec, dir, up);
|
|
|
|
mat.lookAt(ZeroVec, dir, up);
|
|
|
|
return mat;
|
|
|
|
return mat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -200,15 +214,6 @@ export function MoveMatrix(v: Vector3): Matrix4
|
|
|
|
return new Matrix4().setPosition(v);
|
|
|
|
return new Matrix4().setPosition(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function getProjectDist(v1: Vector3, v2: Vector3)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let ang = v1.angleTo(v2);
|
|
|
|
|
|
|
|
let dist = v1.length();
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
h: dist * Math.cos(ang),
|
|
|
|
|
|
|
|
v: dist * Math.sin(ang)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//获得输入点在2线组成的4个区间的位置
|
|
|
|
//获得输入点在2线组成的4个区间的位置
|
|
|
|
export function getPtPostion(sp: Vector3, ep: Vector3, c: Vector3, inPt: Vector3)
|
|
|
|
export function getPtPostion(sp: Vector3, ep: Vector3, c: Vector3, inPt: Vector3)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -241,16 +246,14 @@ export function angleAndX(v: Vector3 | Vector2)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 将角度调整为0-2pi之间
|
|
|
|
* 将角度调整为0-2pi之间
|
|
|
|
*
|
|
|
|
|
|
|
|
* @export
|
|
|
|
|
|
|
|
* @param {number} an
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export function angleTo2Pi(an: number)
|
|
|
|
export function clampRad(an: number)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
an = an % (Math.PI * 2);
|
|
|
|
an = an % (Math.PI * 2);
|
|
|
|
if (an < 0) an += Math.PI * 2
|
|
|
|
if (an < 0) an += Math.PI * 2
|
|
|
|
return an;
|
|
|
|
return an;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function updateGeometry(l: Line | Mesh, geometry: Geometry | BufferGeometry)
|
|
|
|
export function updateGeometry(l: Line | Mesh, geometry: Geometry | BufferGeometry)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let geo = l.geometry as Geometry;
|
|
|
|
let geo = l.geometry as Geometry;
|
|
|
@ -329,7 +332,7 @@ export function getEulerAngle(x: Vector3, y: Vector3, z: Vector3)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let roY = Math.atan2(x.z, Math.sqrt(x.x ** 2 + x.y ** 2)) * -180 / Math.PI;
|
|
|
|
let roY = Math.atan2(x.z, Math.sqrt(x.x ** 2 + x.y ** 2)) * -180 / Math.PI;
|
|
|
|
let roZ = Math.atan2(x.y, x.x);
|
|
|
|
let roZ = Math.atan2(x.y, x.x);
|
|
|
|
let vec = cYAxis.clone();
|
|
|
|
let vec = YAxis.clone();
|
|
|
|
let roMat = new Matrix4().makeRotationZ(roZ);
|
|
|
|
let roMat = new Matrix4().makeRotationZ(roZ);
|
|
|
|
roZ *= 180 / Math.PI;
|
|
|
|
roZ *= 180 / Math.PI;
|
|
|
|
vec.applyMatrix4(roMat);
|
|
|
|
vec.applyMatrix4(roMat);
|
|
|
|