Skip to content

Commit 99fd53f

Browse files
authored
ObjectLoader: Add support for BatchedMesh (#27179)
* Add ObjectLoader support * Add sortObjects and perObjectFrustumCulled fields * Fix ObjectLoader, BatchedMesh initialization
1 parent 22d20b6 commit 99fd53f

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

examples/jsm/objects/BatchedMesh.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class BatchedMesh extends Mesh {
8888

8989
this._geometryInitialized = false;
9090
this._geometryCount = 0;
91-
this._multiDrawCounts = null;
92-
this._multiDrawStarts = null;
91+
this._multiDrawCounts = new Int32Array( maxGeometryCount );
92+
this._multiDrawStarts = new Int32Array( maxGeometryCount );
9393
this._multiDrawCount = 0;
9494

9595
// Local matrix per geometry by using data texture
@@ -156,8 +156,6 @@ class BatchedMesh extends Mesh {
156156
geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
157157

158158
this._geometryInitialized = true;
159-
this._multiDrawCounts = new Int32Array( maxGeometryCount );
160-
this._multiDrawStarts = new Int32Array( maxGeometryCount );
161159

162160
}
163161

src/core/Object3D.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ class Object3D extends EventDispatcher {
746746
object.geometryInitialized = this._geometryInitialized;
747747
object.geometryCount = this._geometryCount;
748748

749-
object.matricesTexture = this._matricesTexture.toJSON();
749+
object.matricesTexture = this._matricesTexture.toJSON( meta );
750750

751751
if ( this.boundingSphere !== null ) {
752752

src/loaders/ObjectLoader.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Color } from '../math/Color.js';
2222
import { Object3D } from '../core/Object3D.js';
2323
import { Group } from '../objects/Group.js';
2424
import { InstancedMesh } from '../objects/InstancedMesh.js';
25+
import { BatchedMesh } from '../../examples/jsm/objects/BatchedMesh.js';
2526
import { Sprite } from '../objects/Sprite.js';
2627
import { Points } from '../objects/Points.js';
2728
import { Line } from '../objects/Line.js';
@@ -59,6 +60,8 @@ import { Loader } from './Loader.js';
5960
import { FileLoader } from './FileLoader.js';
6061
import * as Geometries from '../geometries/Geometries.js';
6162
import { getTypedArray } from '../utils.js';
63+
import { Box3 } from '../math/Box3.js';
64+
import { Sphere } from '../math/Sphere.js';
6265

6366
class ObjectLoader extends Loader {
6467

@@ -897,6 +900,52 @@ class ObjectLoader extends Loader {
897900

898901
break;
899902

903+
case 'BatchedMesh':
904+
905+
geometry = getGeometry( data.geometry );
906+
material = getMaterial( data.material );
907+
908+
object = new BatchedMesh( data.maxGeometryCount, data.maxVertexCount, data.maxIndexCount, material );
909+
object.geometry = geometry;
910+
object.perObjectFrustumCulled = data.perObjectFrustumCulled;
911+
object.sortObjects = data.sortObjects;
912+
913+
object._drawRanges = data.drawRanges;
914+
object._reservedRanges = data.reservedRanges;
915+
916+
object._visible = data.visible;
917+
object._active = data.active;
918+
object._bounds = data.bounds.map( bound => {
919+
920+
const box = new Box3();
921+
box.min.fromArray( bound.boxMin );
922+
box.max.fromArray( bound.boxMax );
923+
924+
const sphere = new Sphere();
925+
sphere.radius = bound.sphereRadius;
926+
sphere.center.fromArray( bound.sphereCenter );
927+
928+
return {
929+
boxInitialized: bound.boxInitialized,
930+
box: box,
931+
932+
sphereInitialized: bound.sphereInitialized,
933+
sphere: sphere
934+
};
935+
936+
} );
937+
938+
object._maxGeometryCount = data.maxGeometryCount;
939+
object._maxVertexCount = data.maxVertexCount;
940+
object._maxIndexCount = data.maxIndexCount;
941+
942+
object._geometryInitialized = data.geometryInitialized;
943+
object._geometryCount = data.geometryCount;
944+
945+
object._matricesTexture = getTexture( data.matricesTexture.uuid );
946+
947+
break;
948+
900949
case 'LOD':
901950

902951
object = new LOD();

0 commit comments

Comments
 (0)