更新相机强制刷新矩阵

This commit is contained in:
YHH
2020-06-19 00:38:37 +08:00
parent e83bb087ea
commit 09e6ace142
19 changed files with 226 additions and 128 deletions

View File

@@ -2,9 +2,9 @@
class Camera extends Component {
private _zoom;
private _origin: Vector2 = Vector2.zero;
private _transformMatrix: Matrix2D = Matrix2D.identity;
private _inverseTransformMatrix = Matrix2D.identity;
private _projectionMatrix = Matrix2D.identity;
private _transformMatrix: Matrix2D = new Matrix2D();
private _inverseTransformMatrix = new Matrix2D();
private _projectionMatrix = new Matrix2D();
private _minimumZoom = 0.3;
private _maximumZoom = 3;
@@ -145,20 +145,16 @@ class Camera extends Component {
return this;
}
public initialize() {
}
public update(){
}
public setPosition(position: Vector2){
this.entity.transform.setPosition(position);
return this;
}
public forceMatrixUpdate(){
this._areMatrixesDirty = true;
}
public updateMatrixes(){
if (!this._areMatrixesDirty)
return;
@@ -170,6 +166,11 @@ class Camera extends Component {
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}
if (this.entity.transform.rotation != 0){
tempMat = Matrix2D.createRotation(this.entity.rotation);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
}
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
@@ -181,12 +182,16 @@ class Camera extends Component {
public screenToWorldPoint(screenPosition: Vector2){
this.updateMatrixes();
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
return Vector2Ext.transformR(screenPosition, this._inverseTransformMatrix);
}
public worldToScreenPoint(worldPosition: Vector2){
this.updateMatrixes();
return Vector2.transform(worldPosition, this._transformMatrix);
return Vector2Ext.transformR(worldPosition, this._transformMatrix);
}
public onEntityTransformChanged(comp: ComponentTransform){
this._areMatrixesDirty = true;
}
public destory() {

View File

@@ -49,8 +49,8 @@ class SpriteRenderer extends RenderableComponent {
if (!this.sprite)
return;
this.sprite.x = this.entity.transform.position.x;
this.sprite.y = this.entity.transform.position.y;
this.sprite.x = this.entity.transform.position.x - camera.transform.position.x;
this.sprite.y = this.entity.transform.position.y - camera.transform.position.y;
this.sprite.rotation = this.entity.transform.rotation;
this.sprite.anchorOffsetX = this._origin.x;
this.sprite.anchorOffsetY = this._origin.y;

View File

@@ -238,7 +238,6 @@ class Entity {
public update(){
this.components.update();
this.transform.updateTransform();
}
public onAddedToScene(){

View File

@@ -147,6 +147,9 @@ class Scene extends egret.DisplayObjectContainer {
public render(){
for (let i = 0; i <this._renderers.length; i ++){
if (this._renderers[i].camera)
this._renderers[i].camera.forceMatrixUpdate();
this.camera.forceMatrixUpdate();
this._renderers[i].render(this);
}
}

View File

@@ -25,9 +25,9 @@ class Transform {
private _rotationMatrix: Matrix2D;
private _scaleMatrix: Matrix2D;
private _worldTransform = Matrix2D.identity;
private _worldToLocalTransform = Matrix2D.identity;
private _worldInverseTransform = Matrix2D.identity;
private _worldTransform = new Matrix2D();
private _worldToLocalTransform = new Matrix2D();
private _worldInverseTransform = new Matrix2D();
private _rotation: number = 0;
private _position: Vector2;
@@ -75,7 +75,7 @@ class Transform {
public get worldToLocalTransform(){
if (this._worldToLocalDirty){
if (!this.parent){
this._worldInverseTransform = Matrix2D.identity;
this._worldInverseTransform = new Matrix2D();
} else{
this.parent.updateTransform();
this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform);
@@ -134,7 +134,7 @@ class Transform {
this._position = this._localPosition;
}else{
this.parent.updateTransform();
this._position = Vector2.transform(this._localPosition, this.parent._worldTransform);
this._position = Vector2Ext.transformR(this._localPosition, this.parent._worldTransform);
}
return this._position;