Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchedMesh: add deleteInstance() #29449

Merged
merged 2 commits into from
Sep 21, 2024

Conversation

Jordan-Lane
Copy link
Contributor

Description

The BatchedMesh currently has no way to remove an instance.

Solution

Adding the deleteInstance() method to the BatchedMesh class will allow users to remove instances easily from the BatchedMesh.

The previous commented out code stated that we would need to implement an optimize function to be able to repack the data. Instead, I added a _fragInfo array to keep track of 'fragmented' or open blocks. The BatchedMesh will still fill the DataTexture the same way it did before, but once it is at capacity, then it will begin adding instances to open spots in the Texture.

Additional context

The BatchedMesh has been a pleasure to work with, I just need the ability to remove existing instances. This is my first time opening a PR to three.js, so please let me know if I am missing anything. I am open to any additional changes or suggestions!

Copy link

github-actions bot commented Sep 19, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 686.03
169.81
686.37
169.89
+345 B
+78 B
WebGPU 835
223.93
835.34
224.02
+345 B
+82 B
WebGPU Nodes 834.5
223.81
834.85
223.89
+345 B
+82 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 462.34
111.55
462.34
111.55
+0 B
+0 B
WebGPU 531.77
143.36
531.77
143.36
+0 B
+0 B
WebGPU Nodes 488.43
133.22
488.43
133.22
+0 B
+0 B

Copy link
Collaborator

@gkjohnson gkjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! #28638 also implemented deleteInstance but I quite like the approach of reusing instance Ids which will generally be more performant than having to re-pack the matrix & color buffers when trying to shrink and remap the list of instances.

The only potential point of confusion is that a user will have "deleted" an id only for it to be re-used again, which isn't necessarily severe. It just means that users will need to be diligent about tracking their ids.

cc @Mugen87 unless you have objections I'm happy with this approach for instance deletion and is a lot less complex than the other implementation. Supporting deletion / repacking of geometry is still an open question but we can address that in the future.

@@ -144,6 +144,7 @@ class BatchedMesh extends Mesh {

// stores visible, active, and geometry id per object
this._drawInfo = [];
this._fragInfo = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the name "_fragInfo" refer to? It would be better to call this something like "availableInstanceIds"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I like the name "availableInstanceIds". Will make that change

let drawId = null;

// If at capacity, rewrite over an inactive block
if ( atCapacity ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should check if there are any available deleted Ids and reuse those ids first rather than waiting until the full capacity has been used.

Copy link
Contributor Author

@Jordan-Lane Jordan-Lane Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea. It would be best to keep both the drawInfo and the available deleted ids as small as possible. I will make that change

@gkjohnson gkjohnson added this to the r169 milestone Sep 20, 2024
@Mugen87
Copy link
Collaborator

Mugen87 commented Sep 20, 2024

cc @Mugen87 unless you have objections I'm happy with this approach for instance deletion and is a lot less complex than the other implementation. Supporting deletion / repacking of geometry is still an open question but we can address that in the future.

Sounds good!

@Jordan-Lane Jordan-Lane force-pushed the JL-batchmesh-delete-instance branch from 3f5f814 to 114489b Compare September 20, 2024 17:29
@Jordan-Lane
Copy link
Contributor Author

@gkjohnson Updated the PR with your suggestions

  1. _fragInfo has now been renamed to _ availableInstanceIds
  2. When adding a new instance we prioritize using any available Ids before expanding the drawInfo

@gkjohnson gkjohnson merged commit 44cb404 into mrdoob:dev Sep 21, 2024
12 checks passed
LD2Studio pushed a commit to LD2Studio/LD2Studio-Editor that referenced this pull request Sep 27, 2024
BatchedMesh: add `deleteInstance()` (mrdoob#29449)

* BatchedMesh: add `deleteInstance()`

* BatchedMesh: prioritize reusing freed instance ids when adding instance

Update Three.js r169

Lensflare: Add WebGPU version. (mrdoob#29451)

* lensflare example

* fix depth

* remove forceWebGL

* rename

* fix typo

* follow original code

* Update LensflareMesh.js

---------

Co-authored-by: aardgoose <[email protected]>
Co-authored-by: Michael Herzog <[email protected]>

Examples: Update lensflare colors. (mrdoob#29458)

WebGLRenderer: add reverse-z via EXT_clip_control (mrdoob#29445)

* WebGLRenderer: add reverse-z via EXT_clip_control

* WebGLRenderer: move conversion methods to utils

Updated builds.

Docs: Improve `WebGLRenderer` page. (mrdoob#29459)

WebGLProgram: add USE_REVERSEDEPTHBUF define (mrdoob#29461)

CylinderGeometry: Don't add degenerate triangles (mrdoob#29460)

* CylinderGeometry: Don't add degenerate triangles

* Change comparison

RenderObject: Introduce `getGeometryCacheKey()` (mrdoob#29465)

* RenderObject: Added `getGeometryCacheKey()`

* NodeMaterialObserver: Improve skinnedmesh and morph supports

* cleanup

Update actions/setup-node digest to 0a44ba7 (mrdoob#29466)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

Update github/codeql-action digest to 294a9d9 (mrdoob#29467)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

Update devDependencies (non-major) (mrdoob#29468)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

Updated builds.

CurveModifierGPU: WebGPURenderer port. (mrdoob#29453)

* curve mod

* lint

* update screenshot

* use correct constant

* use filtered texture

---------

Co-authored-by: aardgoose <[email protected]>

Examples: Clean up. (mrdoob#29473)

* Examples: Clean up.

* E2E: Update screenshot.

SpriteNodeMaterial: Add sizeAttenuation (mrdoob#29394)

* SpriteNodeMaterial: Add sizeAttenuation

* Regenerate screenshot

* revert mat4 mul with vec4(,1)?

* revert

* using let and toVar fixes the issue?

* merge upstream/dev

* revert screenshot

* does reverting SpriteNode even fix?

* revert fix

* revert everything fix tsl galaxy?

* checkout dev SpriteNodeMaterial.js...

* smaller change test

* convert scale to var

* convert scale to var

* simply adding toVar breaks tsl_galaxy?

* fix multiple spritematerial with different center position

* feedbacks

* cleanup

* feedbacks

* cleanup

* cleanup

---------

Co-authored-by: sunag <[email protected]>

WebGPURenderer: Prevent out of bounds `textureLoad` access in WGSL (mrdoob#29470)

Co-authored-by: aardgoose <[email protected]>

SkyMesh,WaterMesh: Fix NodeMaterial imports (mrdoob#29477)

WebGPURenderer: Reuse LightNode when available (mrdoob#29480)

* WebGPURenderer: Reuse LightNode when available

* cleanup

DecalGeometry: Transform normal with normal matrix (mrdoob#29476)

* Transform normal with normal matrix

* Transform normal with normal matrix

Examples: add reflection mask in `retargeting_readyplayer` (mrdoob#29485)

BatchedMesh: Add `getGeometryRangeAt` (mrdoob#29409)

* BatchedMesh: Add getGeometryRangeAt

Co-authored-by: Luigi Denora <[email protected]>

* Fix eslint error

Co-authored-by: Luigi Denora <[email protected]>

* Doc changed

* Add optional target

---------

Co-authored-by: Luigi Denora <[email protected]>

WebGPURenderer: Introduce hash-based cache key (mrdoob#29479)

* introduce numeric cache key

* cleanup

* cleanup

* simplification

* revision

* rev

* rev

* cleanup

Nodes: Access Remaining Compute Builtins (mrdoob#29469)

* init

* remove duplicated function

Add decal as child of mesh (mrdoob#29486)

Update webgl_postprocessing_ssaa.html

Update webgpu_postprocessing_ssaa.html

ToonOutlinePassNode: Add FX pass for toon outlines. (mrdoob#29483)

* ToonOutlineNode: Add FX pass for toon outlines.

* ToonOutlinePassNode: Refactor code.

* E2E: Update screenshot.

* ToonOutlinePassNode: Clean up.

* ToonOutlinePassNode: More clean up.

* ToonOutlinePassNode: More clean up.

Update Addons.js (mrdoob#29493)

leftovers after removing `PackedPhongMaterial ` mrdoob#29382

ReferenceNode: Fix null reference using `getNodeType()` (mrdoob#29498)

* fix null reference using `getNodeType()`

* add `sprite.center` check

NodeBuilder: Introduce `addFlowCodeHierarchy()` / `NodeBlock` (mrdoob#29495)

* VolumeNodeMaterial: simplify a little

* cleanup

* NodeBuilder: Introduce `addFlowCodeHierarchy()`

Examples: Improve shadow map size in `webgpu_tsl_angular_slicing` (mrdoob#29499)

WebGPURenderer: respect the `renderer.shadowMap.enabled` property (mrdoob#29492)

* enable/disable shadow

* enable shadowmaps

* enable more examples

* and another one

* Update Nodes.js

---------

Co-authored-by: aardgoose <[email protected]>

Updated builds.

GLTFLoader: Remove deprecated code. (mrdoob#29502)

TiltLoader: Remove loader. (mrdoob#29471)

Update Chinese translation of InstancedMesh. (mrdoob#29506)

r169
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants