186 lines
6.9 KiB
JavaScript
186 lines
6.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var three_1 = require("three");
|
|
var Orbit_1 = require("./Orbit");
|
|
/**
|
|
*
|
|
* 相机的控制.
|
|
* ->切换相机
|
|
* ->设置视口大小
|
|
* ->旋转和移动相机.
|
|
*
|
|
* @export
|
|
* @class ViewCameraManage
|
|
*/
|
|
var CameraUpdate = /** @class */ (function () {
|
|
function CameraUpdate() {
|
|
this.m_CameraArray = new Map();
|
|
//视口显示的高度
|
|
this.m_ViewHeight = 10;
|
|
//观察的位置
|
|
this.m_Target = new three_1.Vector3();
|
|
//观察向量
|
|
this.m_Direction = new three_1.Vector3(0, 0, -1);
|
|
//观察的轨道.
|
|
this.m_Orbit = new Orbit_1.Orbit();
|
|
this.m_MinViewHeight = 10;
|
|
this.m_MaxViewHeight = 3e4;
|
|
this.m_CameraArray.set(three_1.OrthographicCamera, new three_1.OrthographicCamera(-2, 2, 2, -2, -1e6, 1e6));
|
|
this.m_CameraArray.set(three_1.PerspectiveCamera, new three_1.PerspectiveCamera(50, 1, 0.01, 10000));
|
|
this.m_CurCamera = this.m_CameraArray.get(three_1.OrthographicCamera);
|
|
this.m_Orbit.UpdateRoValue(this.m_Direction);
|
|
this.UpdateUp();
|
|
this.Update();
|
|
}
|
|
Object.defineProperty(CameraUpdate.prototype, "Aspect", {
|
|
get: function () {
|
|
return this.m_Width / this.m_Height;
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(CameraUpdate.prototype, "Camera", {
|
|
get: function () {
|
|
return this.m_CurCamera;
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(CameraUpdate.prototype, "ViewHeight", {
|
|
get: function () {
|
|
return this.m_ViewHeight;
|
|
},
|
|
set: function (height) {
|
|
this.m_ViewHeight = three_1.MathUtils.clamp(height, this.m_MinViewHeight, this.m_MaxViewHeight);
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
CameraUpdate.prototype.SetSize = function (width, height) {
|
|
this.m_Width = width;
|
|
this.m_Height = height;
|
|
};
|
|
/**
|
|
* 平移相机.
|
|
*
|
|
* @param {Vector3} mouseMove
|
|
* @memberof CameraControl
|
|
*/
|
|
CameraUpdate.prototype.Pan = function (mouseMove) {
|
|
mouseMove.y *= -1;
|
|
mouseMove.multiplyScalar(-this.m_ViewHeight / this.m_Height);
|
|
mouseMove.applyQuaternion(this.Camera.quaternion);
|
|
this.m_Target.add(mouseMove);
|
|
this.Update();
|
|
};
|
|
CameraUpdate.prototype.Rotate = function (mouseMove, target) {
|
|
this.m_Orbit.RoX -= mouseMove.y * 0.003;
|
|
this.m_Orbit.RoZ -= mouseMove.x * 0.003;
|
|
//缓存观察点
|
|
var oldTargetFormCameraSpace = target.clone().applyMatrix4(this.Camera.matrixWorldInverse);
|
|
this.m_Orbit.UpdateDirection(this.m_Direction);
|
|
this.UpdateUp();
|
|
this.Update();
|
|
//-----还原观察点
|
|
//得到新的观察点相对于相机的位置
|
|
var newTargetFormCameraSpace = target.clone().applyMatrix4(this.Camera.matrixWorldInverse);
|
|
//减去原先的位置. 得到观测点在相机内移动的向量
|
|
newTargetFormCameraSpace.sub(oldTargetFormCameraSpace);
|
|
//乘以相机的矩阵. 得到向量在世界坐标系的位置
|
|
newTargetFormCameraSpace.applyMatrix4(this.Camera.matrix);
|
|
//因为使用的是点变换,所以减去基点,得到向量
|
|
newTargetFormCameraSpace.sub(this.Camera.position);
|
|
//加上移动的向量. 使得观察点时钟在相机的某个位置
|
|
this.m_Target.add(newTargetFormCameraSpace);
|
|
this.Update();
|
|
};
|
|
CameraUpdate.prototype.Zoom = function (scale, scaleCenter) {
|
|
if (this.Camera instanceof three_1.OrthographicCamera) {
|
|
this.ViewHeight *= scale;
|
|
if (scaleCenter && this.m_ViewHeight < this.m_MaxViewHeight) {
|
|
this.m_Target.sub(scaleCenter);
|
|
this.m_Target.multiplyScalar(scale);
|
|
this.m_Target.add(scaleCenter);
|
|
}
|
|
}
|
|
else if (this.Camera instanceof three_1.PerspectiveCamera) {
|
|
var add = scale > 1 ? 1 : -1;
|
|
add *= this.Camera.position.distanceTo(this.m_Target) / 10;
|
|
this.m_Target.add(this.m_Direction.clone().multiplyScalar(-add));
|
|
}
|
|
this.Update();
|
|
};
|
|
CameraUpdate.prototype.ZoomExtensBox3 = function (box3) {
|
|
if (!box3 || box3.isEmpty())
|
|
return;
|
|
this.Camera.updateMatrixWorld(false);
|
|
//变换到相机坐标系
|
|
box3.applyMatrix4(this.Camera.matrixWorldInverse);
|
|
//
|
|
box3.getCenter(this.m_Target);
|
|
//世界坐标系
|
|
this.m_Target.applyMatrix4(this.Camera.matrix);
|
|
//size
|
|
var size = box3.getSize(new three_1.Vector3());
|
|
//宽高比
|
|
var aspectRatio = size.x / size.y;
|
|
var viewAspectRatio = this.Aspect;
|
|
//
|
|
if (aspectRatio > viewAspectRatio) {
|
|
this.m_ViewHeight = size.x / viewAspectRatio;
|
|
}
|
|
else {
|
|
this.m_ViewHeight = size.y;
|
|
}
|
|
this.Update();
|
|
};
|
|
CameraUpdate.prototype.LookAt = function (dir) {
|
|
this.m_Orbit.UpdateRoValue(dir);
|
|
this.m_Direction.copy(dir);
|
|
this.UpdateUp();
|
|
this.Update();
|
|
};
|
|
CameraUpdate.prototype.UpdateUp = function () {
|
|
Orbit_1.Orbit.ComputUpDirection(this.m_Direction, this.Camera.up);
|
|
};
|
|
/**
|
|
* 根据视口大小,设置相机视口范围.
|
|
*
|
|
* @returns
|
|
* @memberof CameraControl
|
|
*/
|
|
CameraUpdate.prototype.Update = function () {
|
|
this.Camera.position.copy(this.m_Target);
|
|
if (this.Camera instanceof three_1.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.position.sub(this.m_Direction);
|
|
}
|
|
else if (this.Camera instanceof three_1.PerspectiveCamera) {
|
|
this.Camera.aspect = this.Aspect;
|
|
var distens = (this.m_ViewHeight / 2) / (Math.tan(three_1.MathUtils.degToRad(this.Camera.fov) / 2));
|
|
this.Camera.position.sub(this.m_Direction.clone().multiplyScalar(distens));
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
this.Camera.lookAt(this.m_Target);
|
|
this.Camera.updateProjectionMatrix();
|
|
this.Camera.updateMatrixWorld(false);
|
|
};
|
|
CameraUpdate.prototype.SwitchCamera = function () {
|
|
if (this.Camera instanceof three_1.OrthographicCamera) {
|
|
this.m_CurCamera = this.m_CameraArray.get(three_1.PerspectiveCamera);
|
|
}
|
|
else {
|
|
this.m_CurCamera = this.m_CameraArray.get(three_1.OrthographicCamera);
|
|
}
|
|
this.UpdateUp();
|
|
this.Update();
|
|
};
|
|
return CameraUpdate;
|
|
}());
|
|
exports.CameraUpdate = CameraUpdate;
|
|
//# sourceMappingURL=CameraUpdate.js.map
|