新增scene.optimizeCost参数 用于优化cost过高情况

This commit is contained in:
yhh
2020-09-16 14:48:15 +08:00
parent 8e97dcda9c
commit 52980f0b55
9 changed files with 90 additions and 21 deletions

View File

@@ -90,6 +90,7 @@ module es {
this.displayObject.anchorOffsetY = this._origin.y;
}
this.displayObject = new Bitmap(sprite.texture2D);
this.displayObject.touchEnabled = false;
return this;
}
@@ -99,7 +100,7 @@ module es {
* @param origin
*/
public setOrigin(origin: Vector2): SpriteRenderer {
if (this._origin != origin) {
if (!this._origin.equals(origin)) {
this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
@@ -123,8 +124,8 @@ module es {
public render(camera: Camera) {
this.sync(camera);
if (this.displayObject.x != this.bounds.x - camera.bounds.x) this.displayObject.x = this.bounds.x - camera.bounds.x;
if (this.displayObject.y != this.bounds.y - camera.bounds.y) this.displayObject.y = this.bounds.y - camera.bounds.y;
if (this.displayObject.x != this.bounds.x - camera.bounds.x + this._origin.x) this.displayObject.x = this.bounds.x - camera.bounds.x + this._origin.x;
if (this.displayObject.y != this.bounds.y - camera.bounds.y + this._origin.y) this.displayObject.y = this.bounds.y - camera.bounds.y + this._origin.y;
}
}
}

View File

@@ -1,4 +1,6 @@
module es {
import Bitmap = egret.Bitmap;
/** 场景 */
export class Scene extends egret.DisplayObjectContainer {
/**
@@ -33,7 +35,14 @@ module es {
public _renderers: Renderer[] = [];
public readonly _postProcessors: PostProcessor[] = [];
public _didSceneBegin;
/**
* 动态合批
*/
public dynamicBatch: boolean = false;
/**
* 极致优化cost 会失去所有的事件(触摸、点击) 慎重开启
*/
public optimizeCost: boolean = false;
constructor() {
super();
@@ -203,6 +212,7 @@ module es {
this.addChild(component.displayObject);
this.addChild(component.debugDisplayObject);
batching = false;
if (this.optimizeCost && displayContainer) this.optimizeCombine(displayContainer);
displayContainer = null;
} else if (component instanceof RenderableComponent) {
// 静态
@@ -210,6 +220,8 @@ module es {
batching = true;
displayContainer = new egret.DisplayObjectContainer();
displayContainer.cacheAsBitmap = true;
displayContainer.touchEnabled = false;
displayContainer.touchChildren = false;
this.addChild(displayContainer);
}
@@ -217,6 +229,18 @@ module es {
displayContainer.addChild(component.debugDisplayObject);
}
}
if (this.optimizeCost && displayContainer) this.optimizeCombine(displayContainer);
}
private optimizeCombine(container: egret.DisplayObjectContainer){
let renderTexture = new egret.RenderTexture();
renderTexture.drawToTexture(container, new Rectangle(0, 0, container.width, container.height));
let parent = container.parent;
let index = this.getChildIndex(container);
parent.addChildAt(new Bitmap(renderTexture), index);
parent.removeChild(container);
container.removeChildren();
}
/**

View File

@@ -103,7 +103,7 @@ module es {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
this._entity.scene.dynamicInBatch();
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
}
/**
@@ -134,7 +134,7 @@ module es {
this._components.push(component);
this._tempBufferList.push(component);
}
this._entity.scene.dynamicInBatch();
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd.length = 0;