From 9626eafb2c87012267f21e1d3c9a8c60c07fd80b Mon Sep 17 00:00:00 2001 From: blobt Date: Tue, 11 May 2021 19:39:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90camera?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/render/core/Camera.ts | 135 +++++++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/src/render/core/Camera.ts b/src/render/core/Camera.ts index 9418c643..4975b490 100755 --- a/src/render/core/Camera.ts +++ b/src/render/core/Camera.ts @@ -305,6 +305,13 @@ export class Camera { return this._top; } + /** + * 获取视图矩阵 + */ + public get viewMatrix(): mat4 { + return this._viewMatrix; + } + /** * 获取世界坐标逆矩阵 */ @@ -312,12 +319,50 @@ export class Camera { return this._invViewMatrix; } + /** + * 获取投影矩阵 + */ + public get projectionMatrix(): mat4 { + return this._projectionMatrix; + } + + /** + * 获得视图投影矩阵 + */ + public get viewProjecttionMatrix(): mat4 { + return this._viewProjMatrix; + } + + /** + * 获得视图投影矩阵逆矩阵 + */ + public get invViewProjecttionMatrix(): mat4 { + return this._invViewProjMatrix; + } + + /** + * 获取视锥体 + */ + public get frustum(): Frustum { + return this._frustum; + } + /** * 更新相机状态 * @param intervaleSec 更新间隔 */ public update(intervaleSec: number): void { + //生成投影矩阵 this._projectionMatrix = mat4.perspective(this.fovY, this.aspectRatio, this.near, this.far); + + //生成透视矩阵 + this._calcViewMatrix(); + + //???? + mat4.product(this._projectionMatrix, this._viewMatrix, this._viewProjMatrix); + + this._viewProjMatrix.copy(this._invViewMatrix); + this._viewProjMatrix.inverse(this._invViewMatrix); } /** @@ -362,6 +407,94 @@ export class Camera { //生成view的逆矩阵,其实就是世界矩阵 this._viewMatrix.inverse(this._invViewMatrix); - //TODO this._frustum.buildFromCamera(this); + this._frustum.buildFromCamera(this); } + + /** + * 局部坐标系下的前后移动 + * @param speed + */ + public moveForward(speed: number): void { + if (this._type === ECameraType.FPSCAMERA) { + this._position.x = this._zAxis.x * speed; + this._position.z = this._zAxis.z * speed; + } else { + this._position.x += this._zAxis.x * speed; + this._position.y += this._zAxis.y * speed; + this._position.z += this._zAxis.z * speed; + } + } + + /** + * 局部坐标系下的左右移动 + * @param speed + */ + public moveRightward(speed: number): void { + if (this._type === ECameraType.FPSCAMERA) { + this._position.x += this._xAxis.x * speed; + this._position.z += this._xAxis.z * speed; + } else { + this._position.x += this._xAxis.x * speed; + this._position.y += this._xAxis.y * speed; + this._position.z += this._xAxis.z * speed; + } + } + + /** + * 局部坐标系下上下移动 + * @param speed + */ + public moveUpward(speed: number): void { + if (this._type === ECameraType.FPSCAMERA) { + this._position.y += speed; + } else { + this._position.x += this._yAxis.x * speed; + this._position.y += this._yAxis.y * speed; + this._position.z += this._yAxis.z * speed; + } + } + + /** + * 局部坐标系下的左右旋转 + * @param angle + */ + public yaw(angle: number): void { + mat4.m0.setIdentity(); + angle = Util.toRadian(angle); + if (this._type === ECameraType.FPSCAMERA) { + mat4.m0.rotate(angle, vec3.up); + } else { + mat4.m0.rotate(angle, this._yAxis); + } + mat4.m0.multiplyVec3(this.zAxis, this._zAxis); + mat4.m0.multiplyVec3(this.xAxis, this._xAxis); + } + + + /** + * 局部坐标系下的上下旋转 + * @param angle + */ + public pitch(angle: number): void { + mat4.m0.setIdentity(); + angle = Util.toRadian(angle); + mat4.m0.rotate(angle, this._xAxis); + mat4.m0.multiplyVec3(this.yAxis, this._yAxis); + mat4.m0.multiplyVec3(this.zAxis, this._zAxis); + } + + /** + * 局部坐标系下的坐标轴旋转 + * @param angle + */ + public roll(angle: number): void { + if (this._type === ECameraType.FLYCAMERA) { + mat4.m0.setIdentity(); + angle = Util.toRadian(angle); + mat4.m0.rotate(angle, this._zAxis); + mat4.m0.multiplyVec3(this.xAxis, this._xAxis); + mat4.m0.multiplyVec3(this.yAxis, this._yAxis); + } + } + } \ No newline at end of file