Skip to content

Commit

Permalink
fix(destroy): destroy has bug (#142)
Browse files Browse the repository at this point in the history
fix destroy buffer
fix destroy texture
fix destroy object3D
fix destroy geometry
fix destroy material
fix destroy component
  • Loading branch information
ZenderJK committed May 13, 2023
1 parent da29404 commit c9a0fc2
Show file tree
Hide file tree
Showing 18 changed files with 302 additions and 63 deletions.
80 changes: 80 additions & 0 deletions samples/base/Sample_AddRemove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Engine3D, Scene3D, CameraUtil, View3D, AtmosphericComponent, ComponentBase, Time, AxisObject, Object3DUtil, KelvinUtil, DirectLight, Object3D, HoverCameraController, MeshRenderer, LitMaterial, BoxGeometry, UnLit, UnLitMaterial, Interpolator } from "@orillusion/core";
import { GUIHelp } from "@orillusion/debug/GUIHelp";

// sample use component
class Sample_AddRemove {
view: View3D;
async run() {
// init engine
await Engine3D.init();
// create new Scene
let scene = new Scene3D();
// add atmospheric sky
scene.addComponent(AtmosphericComponent);

// init camera3D
let mainCamera = CameraUtil.createCamera3D(null, scene);
mainCamera.perspective(60, Engine3D.aspect, 1, 2000.0);
let hoverCameraController = mainCamera.object3D.addComponent(HoverCameraController);
hoverCameraController.setCamera(15, -30, 300);

// create a view with target scene and camera
this.view = new View3D();
this.view.scene = scene;
this.view.camera = mainCamera;

// start render
Engine3D.startRenderView(this.view);

// gui
GUIHelp.init();

await this.test();
}

private test() {
let list: Object3D[] = [];
let index = 0;
GUIHelp.addButton("add", async () => {
// let obj = new Object3D();
// obj.z += index++ * 1;
// obj.x = Math.random() * 100 - 50
// obj.y = Math.random() * 100 - 50
// obj.z = Math.random() * 100 - 50
// let mr = obj.addComponent(MeshRenderer);
// mr.material = new LitMaterial();
// mr.geometry = new BoxGeometry(10, 10, 10);
// this.view.scene.addChild(obj);

/******** player1 *******/
let player1 = (await Engine3D.res.loadGltf('gltfs/anim/Minion_Lane_Super_Dawn/Minion_Lane_Super_Dawn.glb', {})) as Object3D;
player1.transform.scaleX = 10;
player1.transform.scaleY = 10;
player1.transform.scaleZ = 10;

let cc = player1.clone();
this.view.scene.addChild(cc);
list.push(cc);

// Interpolator.to(cc, { z: 100, scaleX: 5, scaleY: 5, scaleZ: 5 }, 1000).onComplete = () => {
// this.view.scene.removeChild(cc);
// let index = list.indexOf(cc);
// list.splice(index, 1);
// };
});

GUIHelp.addButton("remove", () => {
let index = Math.floor(list.length * Math.random());
let obj = list[index];
if (obj) {
console.log(index, list);
list.splice(index, 1);
obj.destroy();
}
});

GUIHelp.endFolder();
}
}

new Sample_AddRemove().run();
1 change: 1 addition & 0 deletions samples/lights/Sample_PointLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Sample_PointLight {
view.camera = mainCamera;

Engine3D.startRenderViews([view]);

}

public debug(light: PointLight) {
Expand Down
43 changes: 31 additions & 12 deletions src/components/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class Transform extends ComponentBase {
}

this.object3D.entityChildren.forEach((v) => {
v.transform.parent = this;
v.transform.parent = value ? this : null;
});
}

Expand Down Expand Up @@ -414,17 +414,7 @@ export class Transform extends ComponentBase {
}
}

destroy(): void {
if (this.parent && this.parent.object3D) {
this.parent.object3D.removeChild(this.object3D);
this.scene3D = null;
this.localPosition = null;
this.localRotQuat = null;
this.localRotation = null;
this.localScale = null;
}
super.destroy();
}



public decomposeFromMatrix(matrix: Matrix4, orientationStyle: string = 'eulerAngles'): this {
Expand Down Expand Up @@ -715,6 +705,35 @@ export class Transform extends ComponentBase {
return this._localScale;
}

destroy(): void {
if (this.parent && this.parent.object3D) {
this.parent.object3D.removeChild(this.object3D);
this.scene3D = null;
}
super.destroy();

this.eventPositionChange = null;
this.eventRotationChange = null;
this.eventScaleChange = null;
this.onPositionChange = null;
this.onRotationChange = null;
this.onScaleChange = null;
this._scene3d = null;
this._parent = null;
this._localPos = null;
this._localRot = null;
this._localRotQuat = null;
this._localScale = null;
this._forward = null;
this._back = null;
this._right = null;
this._left = null;
this._up = null;
this._down = null;
this._localChange = null;
this._targetPos = null;
}

// private _rotateAroundAxisX:number = 0 ;
// public set rotateAroundAxisX(value:number){
// this._rotateAroundAxisX = value;
Expand Down
8 changes: 8 additions & 0 deletions src/components/renderer/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ export class MeshRenderer extends RenderNode {
super.nodeUpdate(view, passType, renderPassState, clusterLightingBuffer);
}

public destroy(): void {
this.geometry.destroy();
this.materials.forEach(mat => {
mat.destroy();
});
super.destroy();
}

cloneTo(obj: Object3D) {
let mr = obj.addComponent(MeshRenderer);
mr.geometry = this.geometry;
Expand Down
8 changes: 8 additions & 0 deletions src/components/renderer/RenderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,12 @@ export class RenderNode extends ComponentBase {
}
}

public destroy() {
super.destroy();

this._geometry = null;
this._materials = null;
this._combineShaderRefection = null;
}

}
11 changes: 11 additions & 0 deletions src/core/bound/BoundingBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Vector3 } from '../../math/Vector3';
* @group Core
*/
export class BoundingBox implements IBound {

/**
* The center of the bounding box.
*/
Expand Down Expand Up @@ -167,4 +168,14 @@ export class BoundingBox implements IBound {
public updateBound() {

}

public destroy() {
this.center = null;
this.extents = null;
this.min = null;
this.max = null;
this.size = null;
this.worldMax = null;
this.worldMin = null;
}
}
6 changes: 5 additions & 1 deletion src/core/entities/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,14 @@ export class Entity extends CEventDispatcher {
* release current object
*/
public destroy() {
this.transform.parent = null;
this.components.forEach((c) => {
c.destroy();
});
this.components.clear();
this.entityChildren.forEach((c) => {
c.destroy();
})
this.transform.parent = null;
// this.entityChildren = null;
}
}
20 changes: 20 additions & 0 deletions src/core/geometry/GeometryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class SubGeometry {
* @group Geometry
*/
export class GeometryBase {

public uuid: string;
public name: string;
public subGeometries: SubGeometry[] = [];
Expand Down Expand Up @@ -263,4 +264,23 @@ export class GeometryBase {
public isPrimitive(): boolean {
return false;// this.geometrySource != null && this.geometrySource.type != 'none';
}

destroy() {
this.uuid = null;
this.name = null;
this.subGeometries = null;
this.morphTargetDictionary = null;

this._bounds.destroy();
this._bounds = null;

this._attributeMap = null;
this._attributes = null;

this._indicesBuffer.destroy();
this._vertexBuffer.destroy();

this._indicesBuffer = null;
this._vertexBuffer = null;
}
}
10 changes: 10 additions & 0 deletions src/core/geometry/GeometryIndicesBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { VertexAttributeData } from "./VertexAttributeData";


export class GeometryIndicesBuffer {

public uuid: string = '';
public name: string;
public indicesGPUBuffer: IndicesGPUBuffer;
Expand Down Expand Up @@ -31,6 +32,15 @@ export class GeometryIndicesBuffer {

}

destroy() {
this.uuid = null;
this.name = null;
this.indicesFormat = null;
this.indicesCount = null;
this.indicesGPUBuffer.destroy();
this.indicesGPUBuffer = null;
}

/**
* Get indices from geometry data
* Get position attribute from geometry data
Expand Down
12 changes: 12 additions & 0 deletions src/core/geometry/GeometryVertexBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { VertexAttributeSize } from "./VertexAttributeSize";


export class GeometryVertexBuffer {

public vertexCount: number = 0;
public vertexGPUBuffer: VertexGPUBuffer;
public geometryType: GeometryVertexType = GeometryVertexType.compose;
Expand Down Expand Up @@ -190,4 +191,15 @@ export class GeometryVertexBuffer {
public compute() {

}

public destroy() {
this.vertexCount = null;
this.geometryType = null;
this._vertexBufferLayouts = null;
this._attributeSlotLayouts = null;
this._attributeLocation = null;

this.vertexGPUBuffer.destroy();
this.vertexGPUBuffer = null;
}
}
8 changes: 8 additions & 0 deletions src/core/pool/memory/MemoryInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Vector4 } from '../../../math/Vector4';
* @group Core
*/
export class MemoryInfo {

public byteOffset: number;
public byteSize: number;
public offset: number = 0;
Expand Down Expand Up @@ -367,4 +368,11 @@ export class MemoryInfo {
public reset() {
this.offset = 0;
}

destroy() {
this.byteOffset = null;
this.byteSize = null;
this.offset = null;
this.dataBytes = null;
}
}
27 changes: 25 additions & 2 deletions src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,32 @@ export class GPUBufferBase {
}

public destroy() {
if (this.memoryNodes) {
this.memoryNodes.forEach((v) => {
v.destroy();
})
}

this.bufferType = null;
this.seek = null;
this.byteSize = null;
this.usage = null;
this.visibility = null;

this.outFloat32Array = null;
this.buffer.destroy();
this.memory.destroy();
if (this.buffer) {
this.buffer.destroy();
}
this.buffer = null;

if (this.memory) {
this.memory.destroy();
}
this.memory = null;

if (this._readBuffer) {
this._readBuffer.destroy();
}
}

protected createBuffer(usage: GPUBufferUsageFlags, size: number, data?: ArrayBufferData) {
Expand Down

0 comments on commit c9a0fc2

Please sign in to comment.