新增渲染接口

This commit is contained in:
yhh
2021-05-27 18:32:38 +08:00
parent 6c44d38c10
commit 26068aaf6f
29 changed files with 1301 additions and 96 deletions

View File

@@ -95,6 +95,9 @@ module es {
let component = this._components[i];
if (!component) continue;
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
// 处理IUpdatable
if (isIUpdatable(component))
new es.List(this._updatableComponents).remove(component);
@@ -109,6 +112,9 @@ module es {
if (this._components.length > 0) {
for (let i = 0, s = this._components.length; i < s; ++ i) {
let component = this._components[i];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
if (isIUpdatable(component))
this._updatableComponents.push(component);
@@ -151,6 +157,9 @@ module es {
if (this._componentsToAddList.length > 0) {
for (let i = 0, l = this._componentsToAddList.length; i < l; ++ i) {
let component = this._componentsToAddList[i];
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.add(component);
if (isIUpdatable(component))
this._updatableComponents.push(component);
@@ -186,6 +195,9 @@ module es {
}
public handleRemove(component: Component) {
if (component instanceof RenderableComponent)
this._entity.scene.renderableComponents.remove(component);
if (isIUpdatable(component) && this._updatableComponents.length > 0) {
let index = this._updatableComponents.findIndex((c) => (<any>c as Component).id == component.id);
if (index != -1)
@@ -313,5 +325,13 @@ module es {
this._components[i].onDisabled();
}
}
public debugRender(batcher: IBatcher) {
for (let i = 0; i < this._components.length; i ++) {
if (this._components[i].enabled) {
this._components[i].debugRender(batcher);
}
}
}
}
}

View File

@@ -0,0 +1,81 @@
module es {
export class RenderableComponentList {
private _components: IRenderable[] = [];
private _componentsByRenderLayer: Map<number, IRenderable[]> = new Map();
private _unsortedRenderLayers: number[] = [];
private _componentsNeedSort = true;
public get count() {
return this._components.length;
}
public get(index: number) {
return this._components[index];
}
public add(component: IRenderable) {
this._components.push(component);
this.addToRenderLayerList(component, component.renderLayer);
}
public remove(component: IRenderable) {
new List(this._components).remove(component);
new List(this._componentsByRenderLayer.get(component.renderLayer)).remove(component);
}
public updateRenderableRenderLayer(component: IRenderable, oldRenderLayer: number, newRenderLayer: number) {
if (this._componentsByRenderLayer.has(oldRenderLayer) && new List(this._componentsByRenderLayer.get(oldRenderLayer)).contains(component)) {
new List(this._componentsByRenderLayer.get(oldRenderLayer)).remove(component);
this.addToRenderLayerList(component, newRenderLayer);
}
}
public setRenderLayerNeedsComponentSort(renderLayer: number) {
const unsortedRenderLayersList = new List(this._unsortedRenderLayers);
if (!unsortedRenderLayersList.contains(renderLayer))
unsortedRenderLayersList.add(renderLayer);
this._componentsNeedSort = true;
}
public setNeedsComponentSort() {
this._componentsNeedSort = true;
}
private addToRenderLayerList(component: IRenderable, renderLayer: number) {
let list = this.componentsWithRenderLayer(renderLayer);
es.Insist.isFalse(!!list.find(c => c == component), "组件renderLayer列表已包含此组件");
list.push(component);
const unsortedRenderLayersList = new List(this._unsortedRenderLayers);
if (!unsortedRenderLayersList.contains(renderLayer))
unsortedRenderLayersList.add(renderLayer);
this._componentsNeedSort = true;
}
public componentsWithRenderLayer(renderLayer: number) {
if (!this._componentsByRenderLayer.get(renderLayer)) {
this._componentsByRenderLayer.set(renderLayer, []);
}
return this._componentsByRenderLayer.get(renderLayer);
}
public updateLists() {
if (this._componentsNeedSort) {
this._components.sort((self, other) => other.renderLayer - self.renderLayer);
this._componentsNeedSort = false;
}
if (this._unsortedRenderLayers.length > 0) {
for (let i = 0, count = this._unsortedRenderLayers.length; i < count; i ++) {
const renderLayerComponents = this._componentsByRenderLayer.get(this._unsortedRenderLayers[i]);
if (renderLayerComponents) {
renderLayerComponents.sort((self, other) => other.renderLayer - self.renderLayer);
}
this._unsortedRenderLayers.length = 0;
}
}
}
}
}