Browse Source

ObjectLoader: Add support for BatchedMesh (#27179)

* Add ObjectLoader support

* Add sortObjects and perObjectFrustumCulled fields

* Fix ObjectLoader, BatchedMesh initialization
Garrett Johnson 1 year ago
parent
commit
99fd53fbad
3 changed files with 52 additions and 5 deletions
  1. 2 4
      examples/jsm/objects/BatchedMesh.js
  2. 1 1
      src/core/Object3D.js
  3. 49 0
      src/loaders/ObjectLoader.js

+ 2 - 4
examples/jsm/objects/BatchedMesh.js

@@ -88,8 +88,8 @@ class BatchedMesh extends Mesh {
 
 		this._geometryInitialized = false;
 		this._geometryCount = 0;
-		this._multiDrawCounts = null;
-		this._multiDrawStarts = null;
+		this._multiDrawCounts = new Int32Array( maxGeometryCount );
+		this._multiDrawStarts = new Int32Array( maxGeometryCount );
 		this._multiDrawCount = 0;
 
 		// Local matrix per geometry by using data texture
@@ -156,8 +156,6 @@ class BatchedMesh extends Mesh {
 			geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
 
 			this._geometryInitialized = true;
-			this._multiDrawCounts = new Int32Array( maxGeometryCount );
-			this._multiDrawStarts = new Int32Array( maxGeometryCount );
 
 		}
 

+ 1 - 1
src/core/Object3D.js

@@ -746,7 +746,7 @@ class Object3D extends EventDispatcher {
 			object.geometryInitialized = this._geometryInitialized;
 			object.geometryCount = this._geometryCount;
 
-			object.matricesTexture = this._matricesTexture.toJSON();
+			object.matricesTexture = this._matricesTexture.toJSON( meta );
 
 			if ( this.boundingSphere !== null ) {
 

+ 49 - 0
src/loaders/ObjectLoader.js

@@ -22,6 +22,7 @@ import { Color } from '../math/Color.js';
 import { Object3D } from '../core/Object3D.js';
 import { Group } from '../objects/Group.js';
 import { InstancedMesh } from '../objects/InstancedMesh.js';
+import { BatchedMesh } from '../../examples/jsm/objects/BatchedMesh.js';
 import { Sprite } from '../objects/Sprite.js';
 import { Points } from '../objects/Points.js';
 import { Line } from '../objects/Line.js';
@@ -59,6 +60,8 @@ import { Loader } from './Loader.js';
 import { FileLoader } from './FileLoader.js';
 import * as Geometries from '../geometries/Geometries.js';
 import { getTypedArray } from '../utils.js';
+import { Box3 } from '../math/Box3.js';
+import { Sphere } from '../math/Sphere.js';
 
 class ObjectLoader extends Loader {
 
@@ -897,6 +900,52 @@ class ObjectLoader extends Loader {
 
 				break;
 
+			case 'BatchedMesh':
+
+				geometry = getGeometry( data.geometry );
+				material = getMaterial( data.material );
+
+				object = new BatchedMesh( data.maxGeometryCount, data.maxVertexCount, data.maxIndexCount, material );
+				object.geometry = geometry;
+				object.perObjectFrustumCulled = data.perObjectFrustumCulled;
+				object.sortObjects = data.sortObjects;
+
+				object._drawRanges = data.drawRanges;
+				object._reservedRanges = data.reservedRanges;
+
+				object._visible = data.visible;
+				object._active = data.active;
+				object._bounds = data.bounds.map( bound => {
+
+					const box = new Box3();
+					box.min.fromArray( bound.boxMin );
+					box.max.fromArray( bound.boxMax );
+
+					const sphere = new Sphere();
+					sphere.radius = bound.sphereRadius;
+					sphere.center.fromArray( bound.sphereCenter );
+
+					return {
+						boxInitialized: bound.boxInitialized,
+						box: box,
+
+						sphereInitialized: bound.sphereInitialized,
+						sphere: sphere
+					};
+
+				} );
+
+				object._maxGeometryCount = data.maxGeometryCount;
+				object._maxVertexCount = data.maxVertexCount;
+				object._maxIndexCount = data.maxIndexCount;
+
+				object._geometryInitialized = data.geometryInitialized;
+				object._geometryCount = data.geometryCount;
+
+				object._matricesTexture = getTexture( data.matricesTexture.uuid );
+
+				break;
+
 			case 'LOD':
 
 				object = new LOD();