From b06a2e91906ac71a3d0a45e45c18fc5486f14343 Mon Sep 17 00:00:00 2001 From: Makio64 Date: Fri, 13 Sep 2024 15:59:07 +0200 Subject: [PATCH] BatchedMesh setGeometryIdAt getGeometryIdAt (#29343) * BatchedMesh setGeometryIdAt getGeometryIdAt * update example * re-enforce test on parameters in BatchedMesh.setGeometryIdAt * remove the warning to keep consistency --- docs/api/en/objects/BatchedMesh.html | 23 +++++++++++++++++- examples/webgpu_mesh_batch.html | 15 ++++++++---- src/objects/BatchedMesh.js | 36 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/docs/api/en/objects/BatchedMesh.html b/docs/api/en/objects/BatchedMesh.html index 417ef92361ade1..1a4fa625c6b9c9 100644 --- a/docs/api/en/objects/BatchedMesh.html +++ b/docs/api/en/objects/BatchedMesh.html @@ -168,7 +168,15 @@

[page:Integer instanceId]: The id of an instance to get the visibility state of.

Get whether the given instance is marked as "visible" or not.

- + +

+ [method:Integer getGeometryIdAt]( [param:Integer instanceId] ) +

+

+ [page:Integer instanceId]: The id of an instance to get the geometryIndex of. +

+

Get the geometryIndex of the defined instance.

+

[method:undefined setColorAt]( [param:Integer instanceId], [param:Color color] )

@@ -207,6 +215,19 @@

Sets the visibility of the instance at the given index.

+

+ [method:this setGeometryIdAt]( [param:Integer instanceId], [param:Integer geometryId] ) +

+

+ [page:Integer instanceId]: The id of the instance to set the geometryIndex of. +

+

+ [page:Integer geometryId]: The geometryIndex to be use by the instance. +

+

+ Sets the geometryIndex of the instance at the given index. +

+

[method:Integer addGeometry]( [param:BufferGeometry geometry], [param:Integer reservedVertexRange], [param:Integer reservedIndexRange] )

diff --git a/examples/webgpu_mesh_batch.html b/examples/webgpu_mesh_batch.html index 0de8a2741a5167..efc6fe5da87f59 100644 --- a/examples/webgpu_mesh_batch.html +++ b/examples/webgpu_mesh_batch.html @@ -68,6 +68,15 @@ perObjectFrustumCulled: true, opacity: 1, useCustomSort: true, + randomizeGeometry: ()=>{ + + for ( let i = 0; i < api.count; i ++ ) { + + mesh.setGeometryIdAt( i, Math.floor( Math.random() * geometries.length ) ); + + } + + } }; @@ -187,8 +196,6 @@ } - - function init( forceWebGL = false ) { if ( renderer ) { @@ -270,15 +277,13 @@ gui.add( api, 'sortObjects' ); gui.add( api, 'perObjectFrustumCulled' ); gui.add( api, 'useCustomSort' ); + gui.add( api, 'randomizeGeometry' ); // listeners window.addEventListener( 'resize', onWindowResize ); - - - function onWindowResize() { const width = window.innerWidth; diff --git a/src/objects/BatchedMesh.js b/src/objects/BatchedMesh.js index a7d17e78c99dbc..9890bf5feaea35 100644 --- a/src/objects/BatchedMesh.js +++ b/src/objects/BatchedMesh.js @@ -832,6 +832,42 @@ class BatchedMesh extends Mesh { } + setGeometryIdAt( instanceId, geometryId ) { + + // return early if the geometry is out of range or not active + const drawInfo = this._drawInfo; + if ( instanceId >= drawInfo.length || drawInfo[ instanceId ].active === false ) { + + return null; + + } + + // check if the provided geometryId is within the valid range + if ( geometryId < 0 || geometryId >= this._geometryCount ) { + + return null; + + } + + drawInfo[ instanceId ].geometryIndex = geometryId; + + return this; + + } + + getGeometryIdAt( instanceId ) { + + const drawInfo = this._drawInfo; + if ( instanceId >= drawInfo.length || drawInfo[ instanceId ].active === false ) { + + return - 1; + + } + + return drawInfo[ instanceId ].geometryIndex; + + } + raycast( raycaster, intersects ) { const drawInfo = this._drawInfo;