取消entitylist双缓冲区 使用分帧优化大数量实体时卡顿问题

This commit is contained in:
yhh
2020-10-09 16:59:34 +08:00
parent 74fcfd7778
commit 0e7b0bc45c
9 changed files with 143 additions and 116 deletions
+4 -2
View File
@@ -397,7 +397,6 @@ declare module es {
getPostProcessor<T extends PostProcessor>(type: any): T; getPostProcessor<T extends PostProcessor>(type: any): T;
removePostProcessor(postProcessor: PostProcessor): void; removePostProcessor(postProcessor: PostProcessor): void;
createEntity(name: string): Entity; createEntity(name: string): Entity;
createEntityAsync(name: string): Promise<Entity>;
addEntity(entity: Entity): Entity; addEntity(entity: Entity): Entity;
destroyAllEntities(): void; destroyAllEntities(): void;
findEntity(name: string): Entity; findEntity(name: string): Entity;
@@ -1024,7 +1023,9 @@ declare module es {
_isEntityListUnsorted: boolean; _isEntityListUnsorted: boolean;
_entityDict: Map<number, Entity[]>; _entityDict: Map<number, Entity[]>;
_unsortedTags: number[]; _unsortedTags: number[];
_tempEntityList: Entity[]; _addToSceneEntityList: Entity[];
frameAllocate: boolean;
maxAllocate: number;
constructor(scene: Scene); constructor(scene: Scene);
readonly count: number; readonly count: number;
readonly buffer: Entity[]; readonly buffer: Entity[];
@@ -1039,6 +1040,7 @@ declare module es {
removeFromTagList(entity: Entity): void; removeFromTagList(entity: Entity): void;
update(): void; update(): void;
updateLists(): void; updateLists(): void;
private perEntityAddToScene;
findEntity(name: string): Entity; findEntity(name: string): Entity;
entitiesWithTag(tag: number): Entity[]; entitiesWithTag(tag: number): Entity[];
entitiesOfType<T extends Entity>(type: any): T[]; entitiesOfType<T extends Entity>(type: any): T[];
+38 -34
View File
@@ -1889,12 +1889,6 @@ var es;
var entity = new es.Entity(name); var entity = new es.Entity(name);
return this.addEntity(entity); return this.addEntity(entity);
}; };
Scene.prototype.createEntityAsync = function (name) {
var _this = this;
return new Promise(function (resolve) {
resolve(_this.createEntity(name));
});
};
Scene.prototype.addEntity = function (entity) { Scene.prototype.addEntity = function (entity) {
if (this.entities.buffer.contains(entity)) if (this.entities.buffer.contains(entity))
console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity); console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity);
@@ -4762,7 +4756,9 @@ var es;
this._entitiesToRemove = []; this._entitiesToRemove = [];
this._entityDict = new Map(); this._entityDict = new Map();
this._unsortedTags = []; this._unsortedTags = [];
this._tempEntityList = []; this._addToSceneEntityList = [];
this.frameAllocate = false;
this.maxAllocate = 10;
this.scene = scene; this.scene = scene;
} }
Object.defineProperty(EntityList.prototype, "count", { Object.defineProperty(EntityList.prototype, "count", {
@@ -4815,7 +4811,8 @@ var es;
this._entityDict.clear(); this._entityDict.clear();
}; };
EntityList.prototype.contains = function (entity) { EntityList.prototype.contains = function (entity) {
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity); return this._entities.findIndex(function (e) { return e.id == entity.id; }) != -1 ||
this._entitiesToAdded.findIndex(function (e) { return e.id == entity.id; }) != -1;
}; };
EntityList.prototype.getTagList = function (tag) { EntityList.prototype.getTagList = function (tag) {
var list = this._entityDict.get(tag); var list = this._entityDict.get(tag);
@@ -4823,13 +4820,14 @@ var es;
list = []; list = [];
this._entityDict.set(tag, list); this._entityDict.set(tag, list);
} }
return this._entityDict.get(tag); return list;
}; };
EntityList.prototype.addToTagList = function (entity) { EntityList.prototype.addToTagList = function (entity) {
var list = this.getTagList(entity.tag); var list = this.getTagList(entity.tag);
if (!list.contains(entity)) { if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity); list.push(entity);
this._unsortedTags.push(entity.tag); if (!this._unsortedTags.contains(entity.tag))
this._unsortedTags.push(entity.tag);
} }
}; };
EntityList.prototype.removeFromTagList = function (entity) { EntityList.prototype.removeFromTagList = function (entity) {
@@ -4847,10 +4845,7 @@ var es;
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
var temp = this._entitiesToRemove; for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) {
this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp;
for (var _i = 0, _a = this._tempEntityList; _i < _a.length; _i++) {
var entity = _a[_i]; var entity = _a[_i];
this.removeFromTagList(entity); this.removeFromTagList(entity);
this._entities.remove(entity); this._entities.remove(entity);
@@ -4858,27 +4853,26 @@ var es;
entity.scene = null; entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
} }
this._tempEntityList.length = 0; this._entitiesToRemove.length = 0;
}
while (this._addToSceneEntityList.length > 0) {
var entity = this._addToSceneEntityList.shift();
entity.onAddedToScene();
} }
if (this._entitiesToAdded.length > 0) { if (this._entitiesToAdded.length > 0) {
var temp = this._entitiesToAdded; if (this.frameAllocate && this._entitiesToAdded.length > this.maxAllocate) {
this._entitiesToAdded = this._tempEntityList; for (var i = 0; i < this.maxAllocate; i++) {
this._tempEntityList = temp; this.perEntityAddToScene();
for (var _b = 0, _c = this._tempEntityList; _b < _c.length; _b++) {
var entity = _c[_b];
if (!this._entities.contains(entity)) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
} }
if (this._entitiesToAdded.length == 0)
this._isEntityListUnsorted = true;
} }
for (var _d = 0, _e = this._tempEntityList; _d < _e.length; _d++) { else {
var entity = _e[_d]; while (this._entitiesToAdded.length > 0) {
entity.onAddedToScene(); this.perEntityAddToScene();
}
this._isEntityListUnsorted = true;
} }
this._tempEntityList.length = 0;
this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
this._entities.sort(function (a, b) { this._entities.sort(function (a, b) {
@@ -4886,9 +4880,9 @@ var es;
}); });
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) {
for (var _f = 0, _g = this._unsortedTags; _f < _g.length; _f++) { for (var _b = 0, _c = this._unsortedTags; _b < _c.length; _b++) {
var tag = _g[_f]; var tag = _c[_b];
this._entityDict.get(tag).sort(function (a, b) { this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b); return a.compareTo(b);
}); });
@@ -4896,6 +4890,16 @@ var es;
this._unsortedTags.length = 0; this._unsortedTags.length = 0;
} }
}; };
EntityList.prototype.perEntityAddToScene = function () {
var entity = this._entitiesToAdded.shift();
this._addToSceneEntityList.push(entity);
if (this._entities.findIndex(function (e) { return e.id == entity.id; }) == -1) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
};
EntityList.prototype.findEntity = function (name) { EntityList.prototype.findEntity = function (name) {
for (var i = 0; i < this._entities.length; i++) { for (var i = 0; i < this._entities.length; i++) {
if (this._entities[i].name == name) if (this._entities[i].name == name)
File diff suppressed because one or more lines are too long
+11 -1
View File
@@ -10,8 +10,18 @@ module samples {
moonEntity.addComponent(new es.SpriteRenderer(moonTexture)); moonEntity.addComponent(new es.SpriteRenderer(moonTexture));
this.camera.entity.addComponent(new es.FollowCamera(moonEntity)); this.camera.entity.addComponent(new es.FollowCamera(moonEntity));
});
this.entities.frameAllocate = true;
this.entities.maxAllocate = 10;
for (let i = 0; i < 10000; i ++){
this.createEntity("");
}
});
}
public update(){
super.update();
console.log(this.entities.buffer.length);
} }
} }
} }
+4 -2
View File
@@ -397,7 +397,6 @@ declare module es {
getPostProcessor<T extends PostProcessor>(type: any): T; getPostProcessor<T extends PostProcessor>(type: any): T;
removePostProcessor(postProcessor: PostProcessor): void; removePostProcessor(postProcessor: PostProcessor): void;
createEntity(name: string): Entity; createEntity(name: string): Entity;
createEntityAsync(name: string): Promise<Entity>;
addEntity(entity: Entity): Entity; addEntity(entity: Entity): Entity;
destroyAllEntities(): void; destroyAllEntities(): void;
findEntity(name: string): Entity; findEntity(name: string): Entity;
@@ -1024,7 +1023,9 @@ declare module es {
_isEntityListUnsorted: boolean; _isEntityListUnsorted: boolean;
_entityDict: Map<number, Entity[]>; _entityDict: Map<number, Entity[]>;
_unsortedTags: number[]; _unsortedTags: number[];
_tempEntityList: Entity[]; _addToSceneEntityList: Entity[];
frameAllocate: boolean;
maxAllocate: number;
constructor(scene: Scene); constructor(scene: Scene);
readonly count: number; readonly count: number;
readonly buffer: Entity[]; readonly buffer: Entity[];
@@ -1039,6 +1040,7 @@ declare module es {
removeFromTagList(entity: Entity): void; removeFromTagList(entity: Entity): void;
update(): void; update(): void;
updateLists(): void; updateLists(): void;
private perEntityAddToScene;
findEntity(name: string): Entity; findEntity(name: string): Entity;
entitiesWithTag(tag: number): Entity[]; entitiesWithTag(tag: number): Entity[];
entitiesOfType<T extends Entity>(type: any): T[]; entitiesOfType<T extends Entity>(type: any): T[];
+38 -34
View File
@@ -1889,12 +1889,6 @@ var es;
var entity = new es.Entity(name); var entity = new es.Entity(name);
return this.addEntity(entity); return this.addEntity(entity);
}; };
Scene.prototype.createEntityAsync = function (name) {
var _this = this;
return new Promise(function (resolve) {
resolve(_this.createEntity(name));
});
};
Scene.prototype.addEntity = function (entity) { Scene.prototype.addEntity = function (entity) {
if (this.entities.buffer.contains(entity)) if (this.entities.buffer.contains(entity))
console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity); console.warn("\u60A8\u8BD5\u56FE\u5C06\u540C\u4E00\u5B9E\u4F53\u6DFB\u52A0\u5230\u573A\u666F\u4E24\u6B21: " + entity);
@@ -4762,7 +4756,9 @@ var es;
this._entitiesToRemove = []; this._entitiesToRemove = [];
this._entityDict = new Map(); this._entityDict = new Map();
this._unsortedTags = []; this._unsortedTags = [];
this._tempEntityList = []; this._addToSceneEntityList = [];
this.frameAllocate = false;
this.maxAllocate = 10;
this.scene = scene; this.scene = scene;
} }
Object.defineProperty(EntityList.prototype, "count", { Object.defineProperty(EntityList.prototype, "count", {
@@ -4815,7 +4811,8 @@ var es;
this._entityDict.clear(); this._entityDict.clear();
}; };
EntityList.prototype.contains = function (entity) { EntityList.prototype.contains = function (entity) {
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity); return this._entities.findIndex(function (e) { return e.id == entity.id; }) != -1 ||
this._entitiesToAdded.findIndex(function (e) { return e.id == entity.id; }) != -1;
}; };
EntityList.prototype.getTagList = function (tag) { EntityList.prototype.getTagList = function (tag) {
var list = this._entityDict.get(tag); var list = this._entityDict.get(tag);
@@ -4823,13 +4820,14 @@ var es;
list = []; list = [];
this._entityDict.set(tag, list); this._entityDict.set(tag, list);
} }
return this._entityDict.get(tag); return list;
}; };
EntityList.prototype.addToTagList = function (entity) { EntityList.prototype.addToTagList = function (entity) {
var list = this.getTagList(entity.tag); var list = this.getTagList(entity.tag);
if (!list.contains(entity)) { if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity); list.push(entity);
this._unsortedTags.push(entity.tag); if (!this._unsortedTags.contains(entity.tag))
this._unsortedTags.push(entity.tag);
} }
}; };
EntityList.prototype.removeFromTagList = function (entity) { EntityList.prototype.removeFromTagList = function (entity) {
@@ -4847,10 +4845,7 @@ var es;
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
var temp = this._entitiesToRemove; for (var _i = 0, _a = this._entitiesToRemove; _i < _a.length; _i++) {
this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp;
for (var _i = 0, _a = this._tempEntityList; _i < _a.length; _i++) {
var entity = _a[_i]; var entity = _a[_i];
this.removeFromTagList(entity); this.removeFromTagList(entity);
this._entities.remove(entity); this._entities.remove(entity);
@@ -4858,27 +4853,26 @@ var es;
entity.scene = null; entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
} }
this._tempEntityList.length = 0; this._entitiesToRemove.length = 0;
}
while (this._addToSceneEntityList.length > 0) {
var entity = this._addToSceneEntityList.shift();
entity.onAddedToScene();
} }
if (this._entitiesToAdded.length > 0) { if (this._entitiesToAdded.length > 0) {
var temp = this._entitiesToAdded; if (this.frameAllocate && this._entitiesToAdded.length > this.maxAllocate) {
this._entitiesToAdded = this._tempEntityList; for (var i = 0; i < this.maxAllocate; i++) {
this._tempEntityList = temp; this.perEntityAddToScene();
for (var _b = 0, _c = this._tempEntityList; _b < _c.length; _b++) {
var entity = _c[_b];
if (!this._entities.contains(entity)) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
} }
if (this._entitiesToAdded.length == 0)
this._isEntityListUnsorted = true;
} }
for (var _d = 0, _e = this._tempEntityList; _d < _e.length; _d++) { else {
var entity = _e[_d]; while (this._entitiesToAdded.length > 0) {
entity.onAddedToScene(); this.perEntityAddToScene();
}
this._isEntityListUnsorted = true;
} }
this._tempEntityList.length = 0;
this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
this._entities.sort(function (a, b) { this._entities.sort(function (a, b) {
@@ -4886,9 +4880,9 @@ var es;
}); });
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) {
for (var _f = 0, _g = this._unsortedTags; _f < _g.length; _f++) { for (var _b = 0, _c = this._unsortedTags; _b < _c.length; _b++) {
var tag = _g[_f]; var tag = _c[_b];
this._entityDict.get(tag).sort(function (a, b) { this._entityDict.get(tag).sort(function (a, b) {
return a.compareTo(b); return a.compareTo(b);
}); });
@@ -4896,6 +4890,16 @@ var es;
this._unsortedTags.length = 0; this._unsortedTags.length = 0;
} }
}; };
EntityList.prototype.perEntityAddToScene = function () {
var entity = this._entitiesToAdded.shift();
this._addToSceneEntityList.push(entity);
if (this._entities.findIndex(function (e) { return e.id == entity.id; }) == -1) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
};
EntityList.prototype.findEntity = function (name) { EntityList.prototype.findEntity = function (name) {
for (var i = 0; i < this._entities.length; i++) { for (var i = 0; i < this._entities.length; i++) {
if (this._entities[i].name == name) if (this._entities[i].name == name)
+1 -1
View File
File diff suppressed because one or more lines are too long
-10
View File
@@ -412,16 +412,6 @@ module es {
return this.addEntity(entity); return this.addEntity(entity);
} }
/**
*
* @param name
*/
public createEntityAsync(name: string): Promise<Entity> {
return new Promise<Entity>(resolve => {
resolve(this.createEntity(name));
});
}
/** /**
* *
* @param entity * @param entity
+46 -31
View File
@@ -22,10 +22,11 @@ module es {
*/ */
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>(); public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
public _unsortedTags: number[] = []; public _unsortedTags: number[] = [];
/** public _addToSceneEntityList: Entity[] = [];
* updateLists中用于双缓冲区便 /** 是否使用分帧处理 */
*/ public frameAllocate: boolean = false;
public _tempEntityList: Entity[] = []; /** 每帧最大处理数量 */
public maxAllocate: number = 10;
constructor(scene: Scene) { constructor(scene: Scene) {
this.scene = scene; this.scene = scene;
@@ -103,7 +104,8 @@ module es {
* @param entity * @param entity
*/ */
public contains(entity: Entity): boolean { public contains(entity: Entity): boolean {
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity); return this._entities.findIndex(e => e.id == entity.id) != -1 ||
this._entitiesToAdded.findIndex(e => e.id == entity.id) != -1;
} }
public getTagList(tag: number) { public getTagList(tag: number) {
@@ -113,14 +115,15 @@ module es {
this._entityDict.set(tag, list); this._entityDict.set(tag, list);
} }
return this._entityDict.get(tag); return list;
} }
public addToTagList(entity: Entity) { public addToTagList(entity: Entity) {
let list = this.getTagList(entity.tag); let list = this.getTagList(entity.tag);
if (!list.contains(entity)) { if (list.findIndex(e => e.id == entity.id) == -1) {
list.push(entity); list.push(entity);
this._unsortedTags.push(entity.tag); if (!this._unsortedTags.contains(entity.tag))
this._unsortedTags.push(entity.tag);
} }
} }
@@ -141,10 +144,7 @@ module es {
public updateLists() { public updateLists() {
if (this._entitiesToRemove.length > 0) { if (this._entitiesToRemove.length > 0) {
let temp = this._entitiesToRemove; for (const entity of this._entitiesToRemove) {
this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp;
for (const entity of this._tempEntityList) {
this.removeFromTagList(entity); this.removeFromTagList(entity);
this._entities.remove(entity); this._entities.remove(entity);
@@ -154,30 +154,30 @@ module es {
this.scene.entityProcessors.onEntityRemoved(entity); this.scene.entityProcessors.onEntityRemoved(entity);
} }
this._tempEntityList.length = 0; this._entitiesToRemove.length = 0;
}
// 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene
while (this._addToSceneEntityList.length > 0){
let entity = this._addToSceneEntityList.shift();
entity.onAddedToScene();
} }
if (this._entitiesToAdded.length > 0) { if (this._entitiesToAdded.length > 0) {
let temp = this._entitiesToAdded; if (this.frameAllocate && this._entitiesToAdded.length > this.maxAllocate){
this._entitiesToAdded = this._tempEntityList; // 启用分帧处理
this._tempEntityList = temp; for (let i = 0; i < this.maxAllocate; i ++){
for (const entity of this._tempEntityList) { this.perEntityAddToScene();
if (!this._entities.contains(entity)) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity)
} }
}
// 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene if (this._entitiesToAdded.length == 0) this._isEntityListUnsorted = true;
for (const entity of this._tempEntityList) { }else{
entity.onAddedToScene(); while (this._entitiesToAdded.length > 0){
this.perEntityAddToScene();
}
this._isEntityListUnsorted = true;
} }
this._tempEntityList.length = 0;
this._isEntityListUnsorted = true;
} }
if (this._isEntityListUnsorted) { if (this._isEntityListUnsorted) {
@@ -187,7 +187,7 @@ module es {
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
if (this._unsortedTags.length > 0) { if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) {
for (const tag of this._unsortedTags) { for (const tag of this._unsortedTags) {
this._entityDict.get(tag).sort((a, b) => { this._entityDict.get(tag).sort((a, b) => {
return a.compareTo(b); return a.compareTo(b);
@@ -198,6 +198,21 @@ module es {
} }
} }
/** 每次添加一个实体到场景 */
private perEntityAddToScene(){
let entity = this._entitiesToAdded.shift();
this._addToSceneEntityList.push(entity);
if (this._entities.findIndex(e => e.id == entity.id) == -1) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
}
/** /**
* null * null
* @param name * @param name