Browse Source

完成camera

master
blobt 3 years ago
parent
commit
9626eafb2c
  1. 135
      src/render/core/Camera.ts

135
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);
}
}
}
Loading…
Cancel
Save