|
|
@ -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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |