开发:清理变量名称

webgl2
ChenX 4 years ago
parent 63599ab293
commit 4cda2543cf

@ -9,10 +9,10 @@ test("", () =>
let dir = new THREE.Vector3(0, 1, 0); let dir = new THREE.Vector3(0, 1, 0);
orb.UpdateRoValue(dir); orb.SetFromDirection(dir);
expect(equaln(orb.RoX, 0)).toBe(true); expect(equaln(orb.RoX, 0)).toBe(true);
expect(equaln(orb.RoZ, Math.PI * 0.5)).toBe(true); expect(equaln(orb.theta, Math.PI * 0.5)).toBe(true);
//试着还原 //试着还原
orb.UpdateDirection(dir); orb.UpdateDirection(dir);
@ -20,9 +20,9 @@ test("", () =>
//试试新的 //试试新的
dir.set(1, 0, 0); dir.set(1, 0, 0);
orb.UpdateRoValue(dir); orb.SetFromDirection(dir);
expect(equaln(orb.RoX, 0)).toBe(true); expect(equaln(orb.RoX, 0)).toBe(true);
expect(equaln(orb.RoZ, 0)).toBe(true); expect(equaln(orb.theta, 0)).toBe(true);
//试着还原 //试着还原
orb.UpdateDirection(dir); orb.UpdateDirection(dir);
@ -32,7 +32,7 @@ test("", () =>
dir.set(0.5, 0.5, 0).normalize(); dir.set(0.5, 0.5, 0).normalize();
let dirc = dir.clone(); let dirc = dir.clone();
orb.UpdateRoValue(dir); orb.SetFromDirection(dir);
//试着还原 //试着还原
orb.UpdateDirection(dir); orb.UpdateDirection(dir);
@ -41,7 +41,7 @@ test("", () =>
//试试新的 //试试新的
dir.set(0.5, 0.5, 1).normalize(); dir.set(0.5, 0.5, 1).normalize();
dirc = dir.clone(); dirc = dir.clone();
orb.UpdateRoValue(dir); orb.SetFromDirection(dir);
//试着还原 //试着还原
orb.UpdateDirection(dir); orb.UpdateDirection(dir);
@ -49,9 +49,9 @@ test("", () =>
dir.set(0, 0, -1); dir.set(0, 0, -1);
dirc = dir.clone(); dirc = dir.clone();
orb.UpdateRoValue(dir); orb.SetFromDirection(dir);
expect(equaln(orb.RoZ, Math.PI * 0.5)).toBe(true); expect(equaln(orb.theta, Math.PI * 0.5)).toBe(true);
expect(equaln(orb.RoX, Math.PI * -0.5)).toBe(true); expect(equaln(orb.RoX, Math.PI * -0.5)).toBe(true);
//试着还原 //试着还原

@ -8,52 +8,52 @@ import { YAxis, ZAxis, equaln } from "./GeUtils";
export class Orbit export class Orbit
{ {
//抬头低头 正数抬头 负数低头 //抬头低头 正数抬头 负数低头
private m_RoX: number = 0; private phi: number = 0;//Φ
//身体旋转 0为正右边 逆时针旋转 //身体旋转 0为正右边 逆时针旋转
RoZ: number = 0; theta: number = 0;//θ
get RoX() get RoX()
{ {
return this.m_RoX; return this.phi;
} }
set RoX(v) set RoX(v)
{ {
this.m_RoX = MathUtils.clamp(v, Math.PI * -0.49, Math.PI * 0.49); this.phi = MathUtils.clamp(v, Math.PI * -0.49, Math.PI * 0.49);
} }
/** /**
* 使 * 使
* @param [dir] ,, * @param [outDirection] ,,
* @returns * @returns
*/ */
UpdateDirection(dir = new Vector3()): Vector3 UpdateDirection(outDirection = new Vector3()): Vector3
{ {
dir.z = Math.sin(this.m_RoX); outDirection.z = Math.sin(this.phi);
//归一化专用. //归一化专用.
let d = Math.abs(Math.cos(this.m_RoX)); let d = Math.abs(Math.cos(this.phi));
dir.x = Math.cos(this.RoZ) * d; outDirection.x = Math.cos(this.theta) * d;
dir.y = Math.sin(this.RoZ) * d; outDirection.y = Math.sin(this.theta) * d;
return dir; return outDirection;
} }
/** /**
* 使, * 使,
* @param dir . * @param dir .
*/ */
UpdateRoValue(dir: Vector3): void SetFromDirection(dir: Vector3): void
{ {
dir.normalize(); dir.normalize();
this.m_RoX = Math.asin(dir.z); this.phi = Math.asin(dir.z);
if (equaln(dir.x, 0) && equaln(dir.y, 0)) if (equaln(dir.x, 0) && equaln(dir.y, 0))
if (dir.z > 0) if (dir.z > 0)
this.RoZ = Math.PI * -0.5; this.theta = Math.PI * -0.5;
else else
this.RoZ = Math.PI * 0.5; this.theta = Math.PI * 0.5;
else else
this.RoZ = Math.atan2(dir.y, dir.x); this.theta = Math.atan2(dir.y, dir.x);
} }
/** /**

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

Loading…
Cancel
Save