Forráskód Böngészése

BatchedMesh setGeometryIdAt getGeometryIdAt (#29343)

* BatchedMesh setGeometryIdAt getGeometryIdAt

* update example

* re-enforce test on parameters in BatchedMesh.setGeometryIdAt

* remove the warning to keep consistency
Makio64 1 hete
szülő
commit
b06a2e9190

+ 22 - 1
docs/api/en/objects/BatchedMesh.html

@@ -168,7 +168,15 @@
 			[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>
@@ -207,6 +215,19 @@
 			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>

+ 10 - 5
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;

+ 36 - 0
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;