Skip to content

Commit

Permalink
perf(BoundingBox): add isBoundChange tag to Entity (Orillusion#257)
Browse files Browse the repository at this point in the history
Add tags to entity to check for any changes in the bound, and optimize the repeated calculation of the bound
  • Loading branch information
hellmor authored Jul 24, 2023
1 parent 5b57016 commit 70ece43
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 97 deletions.
55 changes: 55 additions & 0 deletions samples/base/Sample_BoundingBox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { GUIHelp } from '@orillusion/debug/GUIHelp';
import { Color, Engine3D, Object3D, Object3DUtil, Transform, View3D, } from '@orillusion/core';
import { GUIUtil } from '@samples/utils/GUIUtil';
import { createExampleScene, createSceneParam } from '@samples/utils/ExampleScene';

// A sample to show boundingbox
class Sample_BoundingBox {
view: View3D;
box: Object3D;
container: Object3D;
async run() {
// init engine
await Engine3D.init({ renderLoop: () => { this.loop() } });
GUIHelp.init();
let param = createSceneParam();
param.camera.distance = 1;
param.camera.near = 0.01;
param.camera.far = 100;
param.camera.distance = 2;
let exampleScene = createExampleScene(param);
Engine3D.startRenderViews([exampleScene.view]);
Engine3D.getRenderJob(exampleScene.view);

let box = Object3DUtil.GetSingleCube(0.5, 0.3, 0.8, 1, 1, 1);
box.transform.eventDispatcher.addEventListener(Transform.LOCAL_ONCHANGE, this.logChange, this);

this.box = box;
this.view = exampleScene.view;

let parent = this.container = new Object3D();
parent.addChild(box);
exampleScene.scene.addChild(parent);

GUIHelp.open();
GUIHelp.addButton('Remove Box', () => { box.transform.parent && box.removeFromParent(); })
GUIHelp.addButton('Add Box', () => { !box.transform.parent && parent.addChild(box); })

GUIUtil.renderTransform(parent.transform, true, 'Container');
GUIUtil.renderTransform(box.transform, true, 'Box');

}

logChange() {
console.log('BoudingBox changed');
}

red = new Color(1, 0, 0, 1);
gree = new Color(0, 1, 0, 1);
loop() {
this.view.graphic3D.drawBoundingBox(this.box.uuid, this.box.bound as any, this.gree);
this.view.graphic3D.drawBoundingBox(this.container.uuid, this.container.bound as any, this.red);
}
}

new Sample_BoundingBox().run()
13 changes: 9 additions & 4 deletions src/components/ComponentBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ export class ComponentBase implements IComponent {
/**
* @internal
*/
public eventDispatcher: CEventDispatcher;
protected _eventDispatcher: CEventDispatcher;
public get eventDispatcher() {
this._eventDispatcher ||= new CEventDispatcher();
return this._eventDispatcher;
}

public set eventDispatcher(value) {
console.error('The eventDispatcher should not be set externally!');
}

/**
* @internal
Expand All @@ -28,9 +36,6 @@ export class ComponentBase implements IComponent {

private __isStart: boolean = false;

constructor() {
this.eventDispatcher = new CEventDispatcher();
}

/**
* Return the Transform component attached to the Object3D.
Expand Down
Loading

0 comments on commit 70ece43

Please sign in to comment.