Skip to content

Commit 3cd87b6

Browse files
zjm-metameta-codesync[bot]
authored andcommitted
fix(core): Avoid allocating new objects every frame in DepthSensingSystem.
Summary: Small fixes on the depth occlusion sample to register the component together with the system. Also, improved the depth sensing system to reuse objects instead of creating new allocations per frame. Reviewed By: felixtrz Differential Revision: D94250212 fbshipit-source-id: 591b5ce55eb4ded0923da0484392777a8192738b
1 parent 372923f commit 3cd87b6

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

examples/depth-occlusion/src/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export class OcclusionDemoSystem extends createSystem({
9191
movementMode: MovementMode.MoveFromTarget,
9292
});
9393
entity.addComponent(XRAnchor);
94-
entity.addComponent(DepthOccludable, { mode: OcclusionShadersMode.HardOcclusion });
94+
entity.addComponent(DepthOccludable, {
95+
mode: OcclusionShadersMode.HardOcclusion,
96+
});
9597

9698
return entity;
9799
}
@@ -214,19 +216,18 @@ World.create(document.getElementById('scene-container'), {
214216
camera.position.set(0, 1.6, 0);
215217

216218
// Register the depth sensing system with occlusion enabled
217-
world.registerSystem(DepthSensingSystem, {
218-
enableDepthTexture: true,
219-
enableOcclusion: true,
220-
useFloat32: true,
221-
blurRadius: 20.0,
222-
});
219+
world
220+
.registerSystem(DepthSensingSystem, {
221+
enableDepthTexture: true,
222+
enableOcclusion: true,
223+
useFloat32: true,
224+
blurRadius: 20.0,
225+
})
226+
.registerComponent(DepthOccludable);
223227

224228
// Register the demo system
225229
world.registerSystem(OcclusionDemoSystem);
226230

227-
// Register the DepthOccludable component
228-
world.registerComponent(DepthOccludable);
229-
230231
console.log('Depth Occlusion Demo initialized');
231232
console.log('Virtual objects will be hidden when behind real-world surfaces');
232233
});

packages/core/src/depth/depth-sensing-system.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export class DepthSensingSystem extends createSystem(
6666

6767
// Occlusion
6868
private entityShaderMap = new Map<Entity, Set<ShaderUniforms>>();
69+
private readonly viewportSize = new Vector2();
6970

7071
/**
7172
* Get the raw value to meters conversion factor.
@@ -372,8 +373,7 @@ export class DepthSensingSystem extends createSystem(
372373
const depthTextureArray = isGPUDepth ? nativeTexture : dataArrayTexture;
373374
if (!depthTextureArray) return;
374375

375-
const viewportSize = new Vector2();
376-
this.renderer.getDrawingBufferSize(viewportSize);
376+
this.renderer.getDrawingBufferSize(this.viewportSize);
377377

378378
for (const [entity, entityUniforms] of this.entityShaderMap) {
379379
const isHardMode =
@@ -385,7 +385,7 @@ export class DepthSensingSystem extends createSystem(
385385
uniforms.uRawValueToMeters.value = this.rawValueToMeters;
386386
uniforms.uIsGPUDepth.value = isGPUDepth;
387387
uniforms.uDepthNear.value = depthNear;
388-
(uniforms.uViewportSize.value as Vector2).copy(viewportSize);
388+
(uniforms.uViewportSize.value as Vector2).copy(this.viewportSize);
389389
uniforms.uOcclusionBlurRadius.value = this.config.blurRadius.value;
390390
uniforms.uOcclusionHardMode.value = isHardMode;
391391
uniforms.occlusionEnabled.value = this.config.enableOcclusion.value;

0 commit comments

Comments
 (0)