Skip to content

Commit

Permalink
BatchedMesh setGeometryIdAt getGeometryIdAt (mrdoob#29343)
Browse files Browse the repository at this point in the history
* BatchedMesh setGeometryIdAt getGeometryIdAt

* update example

* re-enforce test on parameters in BatchedMesh.setGeometryIdAt

* remove the warning to keep consistency
  • Loading branch information
Makio64 authored Sep 13, 2024
1 parent bf6d895 commit b06a2e9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
23 changes: 22 additions & 1 deletion docs/api/en/objects/BatchedMesh.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ <h3>
[page:Integer instanceId]: The id of an instance to get the visibility state of.
</p>
<p>Get whether the given instance is marked as "visible" or not.</p>


<h3>
[method:Integer getGeometryIdAt]( [param:Integer instanceId] )
</h3>
<p>
[page:Integer instanceId]: The id of an instance to get the geometryIndex of.
</p>
<p>Get the geometryIndex of the defined instance.</p>

<h3>
[method:undefined setColorAt]( [param:Integer instanceId], [param:Color color] )
</h3>
Expand Down Expand Up @@ -207,6 +215,19 @@ <h3>
Sets the visibility of the instance at the given index.
</p>

<h3>
[method:this setGeometryIdAt]( [param:Integer instanceId], [param:Integer geometryId] )
</h3>
<p>
[page:Integer instanceId]: The id of the instance to set the geometryIndex of.
</p>
<p>
[page:Integer geometryId]: The geometryIndex to be use by the instance.
</p>
<p>
Sets the geometryIndex of the instance at the given index.
</p>

<h3>
[method:Integer addGeometry]( [param:BufferGeometry geometry], [param:Integer reservedVertexRange], [param:Integer reservedIndexRange] )
</h3>
Expand Down
15 changes: 10 additions & 5 deletions examples/webgpu_mesh_batch.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );

}

}
};


Expand Down Expand Up @@ -187,8 +196,6 @@

}



function init( forceWebGL = false ) {

if ( renderer ) {
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b06a2e9

Please sign in to comment.