Skip to content

Commit

Permalink
ObjectLoader: Add support for BatchedMesh (mrdoob#27179)
Browse files Browse the repository at this point in the history
* Add ObjectLoader support

* Add sortObjects and perObjectFrustumCulled fields

* Fix ObjectLoader, BatchedMesh initialization
  • Loading branch information
gkjohnson authored Nov 13, 2023
1 parent 22d20b6 commit 99fd53f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
6 changes: 2 additions & 4 deletions examples/jsm/objects/BatchedMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );

}

Expand Down
2 changes: 1 addition & 1 deletion src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down
49 changes: 49 additions & 0 deletions src/loaders/ObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 99fd53f

Please sign in to comment.