|
|
|
@ -24,36 +24,36 @@ type Camera = OrthographicCamera | PerspectiveCamera;
|
|
|
|
|
*/
|
|
|
|
|
export class CameraUpdate
|
|
|
|
|
{
|
|
|
|
|
private m_CurCamera: Camera;
|
|
|
|
|
private m_CameraArray: Map<any, Camera> = new Map<any, Camera>();
|
|
|
|
|
private _CurCamera: Camera;
|
|
|
|
|
private _CameraArray: Map<any, Camera> = new Map<any, Camera>();
|
|
|
|
|
|
|
|
|
|
//视口的画布大小
|
|
|
|
|
private m_Width: number;
|
|
|
|
|
private m_Height: number;
|
|
|
|
|
private _Width: number;
|
|
|
|
|
private _Height: number;
|
|
|
|
|
//视口显示的高度
|
|
|
|
|
private m_ViewHeight: number = 1000;
|
|
|
|
|
private _ViewHeight: number = 1000;
|
|
|
|
|
|
|
|
|
|
//观察的位置
|
|
|
|
|
private m_Target: Vector3 = new Vector3();
|
|
|
|
|
private _Target: Vector3 = new Vector3();
|
|
|
|
|
//观察向量
|
|
|
|
|
private m_Direction: Vector3 = new Vector3(0, 0, -1);
|
|
|
|
|
private _Direction: Vector3 = new Vector3(0, 0, -1);
|
|
|
|
|
//观察的轨道.
|
|
|
|
|
private m_Orbit: Orbit = new Orbit();
|
|
|
|
|
private _Orbit: Orbit = new Orbit();
|
|
|
|
|
|
|
|
|
|
//最大最小视口高度
|
|
|
|
|
m_MinViewHeight = 1e-3;
|
|
|
|
|
m_MaxViewHeight = 3e6;
|
|
|
|
|
_MinViewHeight = 1e-3;
|
|
|
|
|
_MaxViewHeight = 3e6;
|
|
|
|
|
|
|
|
|
|
constructor()
|
|
|
|
|
{
|
|
|
|
|
this.m_CameraArray.set(OrthographicCamera, new OrthographicCamera(-2, 2, 2, -2,
|
|
|
|
|
this._CameraArray.set(OrthographicCamera, new OrthographicCamera(-2, 2, 2, -2,
|
|
|
|
|
-ViewScopeSize, ViewScopeSize));
|
|
|
|
|
|
|
|
|
|
this.m_CameraArray.set(PerspectiveCamera, new PerspectiveCamera(50, 1, 0.01, ViewScopeSize));
|
|
|
|
|
this._CameraArray.set(PerspectiveCamera, new PerspectiveCamera(50, 1, 0.01, ViewScopeSize));
|
|
|
|
|
|
|
|
|
|
this.m_CurCamera = this.m_CameraArray.get(OrthographicCamera);
|
|
|
|
|
this._CurCamera = this._CameraArray.get(OrthographicCamera);
|
|
|
|
|
|
|
|
|
|
this.m_Orbit.UpdateRoValue(this.m_Direction);
|
|
|
|
|
this._Orbit.SetFromDirection(this._Direction);
|
|
|
|
|
|
|
|
|
|
this.UpdateUp();
|
|
|
|
|
|
|
|
|
@ -61,31 +61,31 @@ export class CameraUpdate
|
|
|
|
|
}
|
|
|
|
|
get Aspect(): number
|
|
|
|
|
{
|
|
|
|
|
return this.m_Width / this.m_Height;
|
|
|
|
|
return this._Width / this._Height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get Camera(): Camera
|
|
|
|
|
{
|
|
|
|
|
return this.m_CurCamera;
|
|
|
|
|
return this._CurCamera;
|
|
|
|
|
}
|
|
|
|
|
get ViewHeight()
|
|
|
|
|
{
|
|
|
|
|
return this.m_ViewHeight;
|
|
|
|
|
return this._ViewHeight;
|
|
|
|
|
}
|
|
|
|
|
set ViewHeight(height)
|
|
|
|
|
{
|
|
|
|
|
this.m_ViewHeight = MathUtils.clamp(height, this.m_MinViewHeight, this.m_MaxViewHeight);
|
|
|
|
|
this._ViewHeight = MathUtils.clamp(height, this._MinViewHeight, this._MaxViewHeight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get Direction()
|
|
|
|
|
{
|
|
|
|
|
return this.m_Direction.clone();
|
|
|
|
|
return this._Direction.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetSize(width: number, height: number)
|
|
|
|
|
{
|
|
|
|
|
this.m_Width = width;
|
|
|
|
|
this.m_Height = height;
|
|
|
|
|
this._Width = width;
|
|
|
|
|
this._Height = height;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -95,21 +95,21 @@ export class CameraUpdate
|
|
|
|
|
Pan(mouseMove: Vector3)
|
|
|
|
|
{
|
|
|
|
|
mouseMove.y *= -1;
|
|
|
|
|
mouseMove.multiplyScalar(-this.m_ViewHeight / this.m_Height);
|
|
|
|
|
mouseMove.multiplyScalar(-this._ViewHeight / this._Height);
|
|
|
|
|
mouseMove.applyQuaternion(this.Camera.quaternion);
|
|
|
|
|
this.m_Target.add(mouseMove);
|
|
|
|
|
this.m_Target.clamp(ViewScopeMin, ViewScopeMax);
|
|
|
|
|
this._Target.add(mouseMove);
|
|
|
|
|
this._Target.clamp(ViewScopeMin, ViewScopeMax);
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
|
Rotate(mouseMove: Vector3, target: Vector3)
|
|
|
|
|
{
|
|
|
|
|
this.m_Orbit.RoX -= mouseMove.y * 0.003;
|
|
|
|
|
this.m_Orbit.RoZ -= mouseMove.x * 0.003;
|
|
|
|
|
this._Orbit.RoX -= mouseMove.y * 0.003;
|
|
|
|
|
this._Orbit.theta -= mouseMove.x * 0.003;
|
|
|
|
|
|
|
|
|
|
//缓存观察点
|
|
|
|
|
let oldTargetFormCameraSpace = target.clone().applyMatrix4(this.Camera.matrixWorldInverse);
|
|
|
|
|
|
|
|
|
|
this.m_Orbit.UpdateDirection(this.m_Direction);
|
|
|
|
|
this._Orbit.UpdateDirection(this._Direction);
|
|
|
|
|
this.UpdateUp();
|
|
|
|
|
this.Update();
|
|
|
|
|
|
|
|
|
@ -123,7 +123,7 @@ export class CameraUpdate
|
|
|
|
|
//因为使用的是点变换,所以减去基点,得到向量
|
|
|
|
|
newTargetFormCameraSpace.sub(this.Camera.position);
|
|
|
|
|
//加上移动的向量. 使得观察点时钟在相机的某个位置
|
|
|
|
|
this.m_Target.add(newTargetFormCameraSpace);
|
|
|
|
|
this._Target.add(newTargetFormCameraSpace);
|
|
|
|
|
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
@ -132,18 +132,18 @@ export class CameraUpdate
|
|
|
|
|
if (this.Camera instanceof OrthographicCamera)
|
|
|
|
|
{
|
|
|
|
|
this.ViewHeight *= scale;
|
|
|
|
|
if (scaleCenter && this.m_ViewHeight < this.m_MaxViewHeight)
|
|
|
|
|
if (scaleCenter && this._ViewHeight < this._MaxViewHeight)
|
|
|
|
|
{
|
|
|
|
|
this.m_Target.sub(scaleCenter);
|
|
|
|
|
this.m_Target.multiplyScalar(scale);
|
|
|
|
|
this.m_Target.add(scaleCenter);
|
|
|
|
|
this._Target.sub(scaleCenter);
|
|
|
|
|
this._Target.multiplyScalar(scale);
|
|
|
|
|
this._Target.add(scaleCenter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (this.Camera instanceof PerspectiveCamera)
|
|
|
|
|
{
|
|
|
|
|
let add = scale > 1 ? 1 : -1;
|
|
|
|
|
add *= this.Camera.position.distanceTo(this.m_Target) / 10;
|
|
|
|
|
this.m_Target.add(this.m_Direction.clone().multiplyScalar(-add));
|
|
|
|
|
add *= this.Camera.position.distanceTo(this._Target) / 10;
|
|
|
|
|
this._Target.add(this._Direction.clone().multiplyScalar(-add));
|
|
|
|
|
}
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
@ -154,9 +154,9 @@ export class CameraUpdate
|
|
|
|
|
//变换到相机坐标系
|
|
|
|
|
box3.applyMatrix4(this.Camera.matrixWorldInverse);
|
|
|
|
|
//
|
|
|
|
|
box3.getCenter(this.m_Target);
|
|
|
|
|
box3.getCenter(this._Target);
|
|
|
|
|
//世界坐标系
|
|
|
|
|
this.m_Target.applyMatrix4(this.Camera.matrix);
|
|
|
|
|
this._Target.applyMatrix4(this.Camera.matrix);
|
|
|
|
|
//size
|
|
|
|
|
let size = box3.getSize(new Vector3());
|
|
|
|
|
//宽高比
|
|
|
|
@ -182,8 +182,8 @@ export class CameraUpdate
|
|
|
|
|
LookAt(dir: Vector3)
|
|
|
|
|
{
|
|
|
|
|
this.LookAtEvent(dir);
|
|
|
|
|
this.m_Orbit.UpdateRoValue(dir);
|
|
|
|
|
this.m_Direction.copy(dir);
|
|
|
|
|
this._Orbit.SetFromDirection(dir);
|
|
|
|
|
this._Direction.copy(dir);
|
|
|
|
|
this.UpdateUp();
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
@ -194,30 +194,30 @@ export class CameraUpdate
|
|
|
|
|
|
|
|
|
|
UpdateUp()
|
|
|
|
|
{
|
|
|
|
|
Orbit.ComputUpDirection(this.m_Direction, this.Camera.up);
|
|
|
|
|
Orbit.ComputUpDirection(this._Direction, this.Camera.up);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 根据视口大小,设置相机视口范围.
|
|
|
|
|
*/
|
|
|
|
|
Update()
|
|
|
|
|
{
|
|
|
|
|
this.Camera.position.copy(this.m_Target);
|
|
|
|
|
this.Camera.position.copy(this._Target);
|
|
|
|
|
|
|
|
|
|
if (this.Camera instanceof OrthographicCamera)
|
|
|
|
|
{
|
|
|
|
|
this.Camera.left = this.Aspect * this.m_ViewHeight / -2;
|
|
|
|
|
this.Camera.right = this.Aspect * this.m_ViewHeight / 2;
|
|
|
|
|
this.Camera.bottom = this.m_ViewHeight / -2;
|
|
|
|
|
this.Camera.top = this.m_ViewHeight / 2;
|
|
|
|
|
this.Camera.left = this.Aspect * this._ViewHeight / -2;
|
|
|
|
|
this.Camera.right = this.Aspect * this._ViewHeight / 2;
|
|
|
|
|
this.Camera.bottom = this._ViewHeight / -2;
|
|
|
|
|
this.Camera.top = this._ViewHeight / 2;
|
|
|
|
|
|
|
|
|
|
this.Camera.position.sub(this.m_Direction);
|
|
|
|
|
this.Camera.position.sub(this._Direction);
|
|
|
|
|
}
|
|
|
|
|
else if (this.Camera instanceof PerspectiveCamera)
|
|
|
|
|
{
|
|
|
|
|
this.Camera.aspect = this.Aspect;
|
|
|
|
|
let distens = (this.m_ViewHeight / 2) / (Math.tan(MathUtils.degToRad(this.Camera.fov) / 2));
|
|
|
|
|
let distens = (this._ViewHeight / 2) / (Math.tan(MathUtils.degToRad(this.Camera.fov) / 2));
|
|
|
|
|
|
|
|
|
|
this.Camera.position.sub(this.m_Direction.clone().multiplyScalar(distens));
|
|
|
|
|
this.Camera.position.sub(this._Direction.clone().multiplyScalar(distens));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
@ -226,7 +226,7 @@ export class CameraUpdate
|
|
|
|
|
|
|
|
|
|
this.Camera.matrixAutoUpdate = true;
|
|
|
|
|
// this.Camera.updateMatrix(); //如果不使用autoUpdate,那么应该还原这句
|
|
|
|
|
this.Camera.lookAt(this.m_Target);
|
|
|
|
|
this.Camera.lookAt(this._Target);
|
|
|
|
|
this.Camera.updateProjectionMatrix();
|
|
|
|
|
// this.Camera.updateMatrix(); //如果不使用autoUpdate,那么应该还原这句
|
|
|
|
|
this.Camera.updateMatrixWorld(false);
|
|
|
|
@ -249,9 +249,9 @@ export class CameraUpdate
|
|
|
|
|
SwitchCamera()
|
|
|
|
|
{
|
|
|
|
|
if (this.Camera instanceof OrthographicCamera)
|
|
|
|
|
this.m_CurCamera = this.m_CameraArray.get(PerspectiveCamera);
|
|
|
|
|
this._CurCamera = this._CameraArray.get(PerspectiveCamera);
|
|
|
|
|
else
|
|
|
|
|
this.m_CurCamera = this.m_CameraArray.get(OrthographicCamera);
|
|
|
|
|
this._CurCamera = this._CameraArray.get(OrthographicCamera);
|
|
|
|
|
this.UpdateUp();
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
@ -262,11 +262,11 @@ export class CameraUpdate
|
|
|
|
|
ReadFile(file: CADFiler)
|
|
|
|
|
{
|
|
|
|
|
let ver = file.Read();
|
|
|
|
|
this.m_ViewHeight = file.Read();
|
|
|
|
|
this.m_Target.fromArray(file.Read());
|
|
|
|
|
this.m_Direction.fromArray(file.Read());
|
|
|
|
|
this._ViewHeight = file.Read();
|
|
|
|
|
this._Target.fromArray(file.Read());
|
|
|
|
|
this._Direction.fromArray(file.Read());
|
|
|
|
|
|
|
|
|
|
this.m_Orbit.UpdateRoValue(this.m_Direction);
|
|
|
|
|
this._Orbit.SetFromDirection(this._Direction);
|
|
|
|
|
this.UpdateUp();
|
|
|
|
|
this.Update();
|
|
|
|
|
}
|
|
|
|
@ -274,8 +274,8 @@ export class CameraUpdate
|
|
|
|
|
WriteFile(file: CADFiler)
|
|
|
|
|
{
|
|
|
|
|
file.Write(1);
|
|
|
|
|
file.Write(this.m_ViewHeight);
|
|
|
|
|
file.Write(this.m_Target.toArray());
|
|
|
|
|
file.Write(this.m_Direction.toArray());
|
|
|
|
|
file.Write(this._ViewHeight);
|
|
|
|
|
file.Write(this._Target.toArray());
|
|
|
|
|
file.Write(this._Direction.toArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|