相机渲染

This commit is contained in:
YHH
2020-06-18 23:22:54 +08:00
parent feaac83ee7
commit e83bb087ea
13 changed files with 124 additions and 47 deletions

View File

@@ -1,16 +1,18 @@
///<reference path="../Component.ts"/>
class Camera extends Component {
private _zoom;
private _origin: Vector2;
private _origin: Vector2 = Vector2.zero;
private _transformMatrix: Matrix2D = Matrix2D.identity;
private _inverseTransformMatrix = Matrix2D.identity;
private _projectionMatrix = Matrix2D.identity;
private _minimumZoom = 0.3;
private _maximumZoom = 3;
private _areMatrixesDirty = true;
private _inset: CameraInset;
private _bounds: Rectangle;
private _inset: CameraInset = new CameraInset();
private _bounds: Rectangle = new Rectangle();
private _areBoundsDirty = true;
private _isProjectionMatrixDirty = true;
public get bounds(){
if (this._areMatrixesDirty)
@@ -104,6 +106,14 @@ class Camera extends Component {
this.setZoom(0);
}
public onSceneSizeChanged(newWidth: number, newHeight: number){
this._isProjectionMatrixDirty = true;
let oldOrigin = this._origin;
this.origin = new Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin));
}
public setMinimumZoom(minZoom: number): Camera{
if (this._zoom < minZoom)
this._zoom = this.minimumZoom;

View File

@@ -4,8 +4,8 @@
abstract class RenderableComponent extends Component implements IRenderable {
private _isVisible: boolean;
protected _areBoundsDirty = true;
protected _bounds: Rectangle;
protected _localOffset: Vector2;
protected _bounds: Rectangle = new Rectangle();
protected _localOffset: Vector2 = Vector2.zero;
public get width(){
return this.getWidth();
@@ -57,8 +57,10 @@ abstract class RenderableComponent extends Component implements IRenderable {
public abstract render(camera: Camera);
public isVisibleFromCamera(camera: Camera): boolean{
this.isVisible = camera.bounds.intersects(this.bounds);
return this.isVisible;
// this.isVisible = camera.bounds.intersects(this.bounds);
// return this.isVisible;
return true;
}
public onEntityTransformChanged(comp: ComponentTransform){

View File

@@ -2,7 +2,7 @@ class SpriteRenderer extends RenderableComponent {
private _sprite: egret.DisplayObject;
private _origin: Vector2;
public get bounds(){
protected getBounds(){
if (this._areBoundsDirty){
if (this._sprite){
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin,

View File

@@ -109,6 +109,8 @@ class Scene extends egret.DisplayObjectContainer {
if (this.entityProcessors)
this.entityProcessors.begin();
this.camera.onSceneSizeChanged(this.stage.width, this.stage.height);
}
/** 场景激活 */
@@ -140,6 +142,13 @@ class Scene extends egret.DisplayObjectContainer {
this.entityProcessors.lateUpdate();
this.renderableComponents.updateList();
this.render();
}
public render(){
for (let i = 0; i <this._renderers.length; i ++){
this._renderers[i].render(this);
}
}
public prepRenderState() {

View File

@@ -44,6 +44,9 @@ class ComponentList {
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
@@ -53,6 +56,9 @@ class ComponentList {
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
@@ -71,7 +77,8 @@ class ComponentList {
if (this._componentsToAdd.length > 0){
for (let i = 0, count = this._componentsToAdd.length; i < count; i ++){
let component = this._componentsToAdd[i];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);