Skip to content

Commit 6be5c9b

Browse files
authored
Add BatchedTilesPlugin (#800)
* Add BatchedTilesPlugin * Add start to expanding batched mesh * Split out ExpandingBatchedMesh * Clean up * small fixes * Progress on getting things working * Check revision * Texture fixes * Small fixes * Some fixes to geometry expansion * remove extra count * Fix rendering * Fix instance deletion * Remove frustum culling * Get everything working * Fix score * Update raycasting logic * Add raycasting support * Update material support * Add docs * updates * Fix disposal * Handle all already loaded models * Add comments * more comments * fix disposal * some fixes * Remove unused fields * Add model view batched mesh * Adjust the model view batched mesh to use camera offset * Temporarily remove mipmaps * Handle cases where tiles have multiple geometries * Remove override * fix use of free ids * Limit the amount of instances to the size of a 3d texture * Add stand in options * Adjust instance count check * Support multiple geometries per tile * Handle incongruent attributes, texturs * Update plugin
1 parent 5f79a47 commit 6be5c9b

File tree

9 files changed

+821
-8
lines changed

9 files changed

+821
-8
lines changed

PLUGINS.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,45 @@ transformLatLonHeightToOrigin( lat, lon, height = 0 ) : void
538538

539539
Transforms the centers the tile set such that the given coordinates and height are positioned at the origin with "X" facing west and "Z" facing north.
540540

541+
## BatchedTilesPlugin
542+
543+
_available in the examples directory_
544+
545+
Plugin that uses three.js' BatchedMesh to limit the number of draw calls required and improve performance. The BatchedMesh geometry and instance size are automatically resized and optimized as new geometry is added and removed. The max number of instances to generate is limited by the max size of a 3d texture.
546+
547+
> [!WARNING]
548+
> All tile geometry rendered with BatchedMesh will use the same material and only a single material "map" is supported. Only tiles geometry containing a single mesh are supported. Not compatible with other plugins that modify mesh materials or rely on other bespoke mesh data (eg TilesFadePlugin, DebugTilesPlugin, GLTF Metadata extensions).
549+
550+
### .constructor
551+
552+
553+
```js
554+
constructor( options : Object )
555+
```
556+
557+
Available options are as follows:
558+
559+
```js
560+
{
561+
// WebGLRenderer used for generating a WebGLArrayRenderTarget
562+
renderer,
563+
564+
// The initial number of instances to use for rendering
565+
instanceCount: 500,
566+
567+
// The minimum amount of vertex and index space to save per tile geometry added. If adequate tile space is already allocated
568+
// when a new tile geometry is added then it can prevent more expensive geometry resizing and optimization.
569+
vertexCount: 1000,
570+
indexCount: 1000,
571+
572+
// The amount to increase the geometry and instance allocation when the operations must occur
573+
expandPercent: 0.25,
574+
575+
// The material to use for the BatchedMesh. The material of the first tile rendered with be used if not set.
576+
material: null,
577+
}
578+
```
579+
541580
# Controls
542581

543582
## EnvironmentControls

example/fadingTiles.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ function reinstantiateTiles() {
112112
if ( groundTiles ) {
113113

114114
groundTiles.dispose();
115+
groundTiles.group.removeFromParent();
116+
115117
skyTiles.dispose();
118+
skyTiles.group.removeFromParent();
116119

117120
}
118121

example/googleMapsExample.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ function reinstantiateTiles() {
6161
tiles.registerPlugin( new UpdateOnChangePlugin() );
6262
tiles.registerPlugin( new TilesFadePlugin() );
6363
tiles.group.rotation.x = - Math.PI / 2;
64-
tiles.errorTarget = 50;
6564

6665
// Note the DRACO compression files need to be supplied via an explicit source.
6766
// We use unpkg here but in practice should be provided by the application.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { MeshBasicMaterial } from 'three';
2+
3+
// A MeshBasicMaterial that supports taking a TextureArray and a layer to render
4+
export class ArrayTextureCopyMaterial extends MeshBasicMaterial {
5+
6+
set layer( v ) {
7+
8+
this._layerUniform.value = v;
9+
10+
}
11+
12+
get layer() {
13+
14+
return this._layerUniform.value;
15+
16+
}
17+
18+
constructor( ...args ) {
19+
20+
super( ...args );
21+
this._layerUniform = { value: 0 };
22+
23+
}
24+
25+
onBeforeCompile( shader ) {
26+
27+
shader.uniforms.layer = this._layerUniform;
28+
29+
shader.fragmentShader = shader.fragmentShader
30+
.replace(
31+
'#include <map_pars_fragment>',
32+
/* glsl */`
33+
#ifdef USE_MAP
34+
precision highp sampler2DArray;
35+
uniform sampler2DArray map;
36+
uniform int layer;
37+
#endif
38+
`,
39+
)
40+
.replace(
41+
'#include <map_fragment>',
42+
/* glsl */`
43+
#ifdef USE_MAP
44+
diffuseColor *= texture( map, vec3( vMapUv, layer ) );
45+
#endif
46+
`
47+
);
48+
49+
}
50+
51+
}

0 commit comments

Comments
 (0)