更新相机强制刷新矩阵
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -238,7 +238,6 @@ class Entity {
|
||||
|
||||
public update(){
|
||||
this.components.update();
|
||||
this.transform.updateTransform();
|
||||
}
|
||||
|
||||
public onAddedToScene(){
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
class DefaultRenderer extends Renderer {
|
||||
public render(scene: Scene) {
|
||||
let cam = this.camera ? this.camera : scene.camera;
|
||||
this.beginRender(cam);
|
||||
|
||||
for (let i = 0; i < scene.renderableComponents.count; i++){
|
||||
let renderable = scene.renderableComponents.buffer[i];
|
||||
|
||||
@@ -15,6 +15,15 @@ abstract class Renderer {
|
||||
*/
|
||||
public onAddedToScene(scene: Scene){}
|
||||
|
||||
protected beginRender(cam: Camera){
|
||||
cam.transform.updateTransform();
|
||||
|
||||
let entities = SceneManager.getActiveScene().entities;
|
||||
for (let i = 0; i < entities.buffer.length; i ++){
|
||||
entities.buffer[i].transform.updateTransform();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param scene
|
||||
|
||||
@@ -20,15 +20,15 @@ class Matrix2D {
|
||||
return Matrix2D._identity;
|
||||
}
|
||||
|
||||
constructor(m11: number, m12: number, m21: number, m22: number, m31: number, m32: number){
|
||||
this.m11 = m11;
|
||||
this.m12 = m12;
|
||||
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number){
|
||||
this.m11 = m11 ? m11 : 1;
|
||||
this.m12 = m12 ? m12 : 0;
|
||||
|
||||
this.m21 = m21;
|
||||
this.m22 = m22;
|
||||
this.m21 = m21 ? m21 : 0;
|
||||
this.m22 = m22 ? m22 : 1;
|
||||
|
||||
this.m31 = m31;
|
||||
this.m32 = m32;
|
||||
this.m31 = m31 ? m31 : 0;
|
||||
this.m32 = m32 ? m32 : 0;
|
||||
}
|
||||
|
||||
/** 存储在这个矩阵中的位置 */
|
||||
@@ -108,6 +108,8 @@ class Matrix2D {
|
||||
}
|
||||
|
||||
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){
|
||||
let result = new Matrix2D();
|
||||
|
||||
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 );
|
||||
let m12 = ( matrix1.m11 * matrix2.m12 ) + ( matrix1.m12 * matrix2.m22 );
|
||||
|
||||
@@ -117,15 +119,16 @@ class Matrix2D {
|
||||
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31;
|
||||
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
|
||||
|
||||
matrix1.m11 = m11;
|
||||
matrix1.m12 = m12;
|
||||
result.m11 = m11;
|
||||
result.m12 = m12;
|
||||
|
||||
matrix1.m21 = m21;
|
||||
matrix1.m22 = m22;
|
||||
result.m21 = m21;
|
||||
result.m22 = m22;
|
||||
|
||||
matrix1.m31 = m31;
|
||||
matrix1.m32 = m32;
|
||||
return matrix1;
|
||||
result.m31 = m31;
|
||||
result.m32 = m32;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){
|
||||
@@ -137,7 +140,7 @@ class Matrix2D {
|
||||
return this.m11 * this.m22 - this.m12 * this.m21;
|
||||
}
|
||||
|
||||
public static invert(matrix: Matrix2D, result: Matrix2D = Matrix2D.identity){
|
||||
public static invert(matrix: Matrix2D, result: Matrix2D = new Matrix2D()){
|
||||
let det = 1 / matrix.determinant();
|
||||
|
||||
result.m11 = matrix.m22 * det;
|
||||
@@ -152,7 +155,9 @@ class Matrix2D {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createTranslation(xPosition: number, yPosition: number, result: Matrix2D = Matrix2D.identity){
|
||||
public static createTranslation(xPosition: number, yPosition: number, result?: Matrix2D){
|
||||
result = result ? result : new Matrix2D();
|
||||
|
||||
result.m11 = 1;
|
||||
result.m12 = 0;
|
||||
|
||||
@@ -166,7 +171,7 @@ class Matrix2D {
|
||||
}
|
||||
|
||||
public static createRotation(radians: number, result?: Matrix2D){
|
||||
result = Matrix2D.identity;
|
||||
result = new Matrix2D();
|
||||
|
||||
let val1 = Math.cos(radians);
|
||||
let val2 = Math.sin(radians);
|
||||
@@ -179,7 +184,7 @@ class Matrix2D {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static createScale(xScale: number, yScale: number, result: Matrix2D = Matrix2D.identity){
|
||||
public static createScale(xScale: number, yScale: number, result: Matrix2D = new Matrix2D()){
|
||||
result.m11 = xScale;
|
||||
result.m12 = 0;
|
||||
|
||||
|
||||
@@ -54,6 +54,12 @@ class Vector2Ext {
|
||||
}
|
||||
}
|
||||
|
||||
public static transformR(position: Vector2, matrix: Matrix2D){
|
||||
let x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
|
||||
let y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
|
||||
public static transform(sourceArray: Vector2[], matrix: Matrix2D, destinationArray: Vector2[]) {
|
||||
this.transformA(sourceArray, 0, matrix, destinationArray, 0, sourceArray.length);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user