Skip to content

Commit

Permalink
fix(collider): Fix error of component deconstruction (#236)
Browse files Browse the repository at this point in the history
Fix error for data dependency during component deconstruction
Revert sample codes.
  • Loading branch information
hellmor committed Jun 28, 2023
1 parent 3664c8b commit 7b6d356
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/Engine3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export class Engine3D {
})
});

let command = webGPUContext.device.createCommandEncoder();;
let command = webGPUContext.device.createCommandEncoder();
ComponentCollect.componentsComputeList.forEach((v, k) => {
v.forEach((c, f) => {
if (f.enable) {
Expand Down
8 changes: 3 additions & 5 deletions src/components/ColliderComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ export class ColliderComponent extends ComponentBase {
}
return null;
}
/**
* @internal
*/
public destroy(force?: boolean) {

public beforeDestroy?(force?: boolean) {
if (Engine3D.setting.pick.mode == `pixel`) {
this.transform.scene3D.view.pickFire.mouseEnableMap.delete(this.transform.worldMatrix.index);
}
super.destroy(force);
super.beforeDestroy?.(force);
}

}
18 changes: 8 additions & 10 deletions src/components/ComponentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class ComponentBase implements IComponent {
if (this._enable != value) {
this._enable = value;
if (this._enable) {
this.onEnable && this.onEnable();
this.onEnable?.();
} else {
this.onDisable && this.onDisable();
this.onDisable?.();
}
}
}
Expand All @@ -61,17 +61,16 @@ export class ComponentBase implements IComponent {
}

private __init(param?: any) {

this.init(param);
}

private __start() {
if (this.start && this.transform && this.transform.scene3D && this.__isStart == false) {
this.start();
if (this.transform && this.transform.scene3D && this.__isStart == false) {
this.start?.();
this.__isStart = true;
}
if (this.onEnable && this.transform && this.transform.scene3D) {
this.onEnable();
if (this.transform && this.transform.scene3D) {
this.onEnable?.();
}
if (this.onUpdate) {
this._onUpdate(this.onUpdate.bind(this));
Expand All @@ -92,15 +91,13 @@ export class ComponentBase implements IComponent {

private __stop() {
if (this.transform && this.transform.scene3D) {
this.onDisable && this.onDisable();
this.onDisable?.();
}
this._onUpdate(null);
this._onLateUpdate(null);
this._onBeforeUpdate(null);
this._onCompute(null);
this._onGraphic(null);


}

public init(param?: any) { }
Expand All @@ -114,6 +111,7 @@ export class ComponentBase implements IComponent {
public onCompute?(view?: View3D, command?: GPUCommandEncoder);
public onGraphic?(view?: View3D);
public onParentChange?(lastParent?: Object3D, currentParent?: Object3D);
public beforeDestroy?(force?: boolean);

/**
*
Expand Down
1 change: 1 addition & 0 deletions src/components/IComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IComponent {
onGraphic?(view?: View3D);
cloneTo(obj: Object3D);
destroy(force?: boolean);
beforeDestroy?(force?: boolean);
onParentChange?(lastParent?: Object3D, currentParent?: Object3D);

}
8 changes: 6 additions & 2 deletions src/components/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,13 +697,17 @@ export class Transform extends ComponentBase {
return this._localScale;
}

destroy(): void {
public beforeDestroy(force?: boolean) {
if (this.parent && this.parent.object3D) {
this.parent.object3D.removeChild(this.object3D);
this.scene3D = null;
}
super.beforeDestroy?.(force);
}

destroy(): void {
super.destroy();

this.scene3D = null;
this.eventPositionChange = null;
this.eventRotationChange = null;
this.eventScaleChange = null;
Expand Down
2 changes: 1 addition & 1 deletion src/components/audio/PositionAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class PositionAudio extends StaticAudio {
for (let l of this._lines) {
l.removeAllChild();
l.removeFromParent();
l.dispose();
l.destroy();
}
this._lines.length = 0;
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/renderer/RenderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ export class RenderNode extends ComponentBase {
}
}

public destroy(force?: boolean) {
super.destroy(force);

public beforeDestroy(force?: boolean) {
Reference.getInstance().detached(this._geometry, this);
if (!Reference.getInstance().hasReference(this._geometry)) {
this._geometry.destroy(force);
Expand All @@ -489,7 +487,11 @@ export class RenderNode extends ComponentBase {
mat.destroy(force);
}
}
super.beforeDestroy?.(force);
}

public destroy(force?: boolean) {
super.destroy(force);
this._geometry = null;
this._materials = null;
this._combineShaderRefection = null;
Expand Down
36 changes: 17 additions & 19 deletions src/core/entities/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,6 @@ export class Entity extends CEventDispatcher {
public update() {
}

/**
* @internal
*/
public dispose() {
this._dispose = true;
}

/**
*
* @private
Expand All @@ -297,11 +290,10 @@ export class Entity extends CEventDispatcher {
*/
public waitUpdate(): void {
if (this._dispose) {
if (this.transform.parent) {
this.transform.parent.object3D.removeChild(this);
}
this.removeFromParent();
this.components.forEach((v, k) => {
v.enable = false;
v.beforeDestroy?.();
v.destroy();
});
this.components.clear();
Expand Down Expand Up @@ -336,18 +328,24 @@ export class Entity extends CEventDispatcher {
BoundUtil.transformBound(this.transform.worldMatrix, this._bound as BoundingBox, this._boundWorld as BoundingBox);
}


/**
* release current object
*/
public destroy(force?: boolean) {
this.components.forEach((c) => {
c.destroy(force);
});
this.components.clear();
this.entityChildren.forEach((c) => {
c.destroy(force);
})
this.transform.parent = null;
if (!this._dispose) {
this.components.forEach((c) => {
c.beforeDestroy?.(force);
});
this.components.forEach((c) => {
c.destroy(force);
});
this.components.clear();
this.entityChildren.forEach((c) => {
c.destroy(force);
})
this.transform.parent = null;
this._dispose = true;
super.destroy();
}
}
}
1 change: 1 addition & 0 deletions src/core/entities/Object3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class Object3D extends Entity {
let component = this.components.get(className);
this.components.delete(className);
component[`__stop`]();
component.beforeDestroy?.();
component.destroy();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/event/CEventDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class CEventDispatcher {
*
* release all registered event.
*/
public dispose() {
public destroy() {
for (var key in this.listeners) {
var list: any = this.listeners[key];
while (list.length > 0) {
Expand Down

0 comments on commit 7b6d356

Please sign in to comment.