Skip to content

Commit dcaefcb

Browse files
committed
Freeze flag and small refactoring
1 parent 33d2cc2 commit dcaefcb

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

examples/webgl_batch_lod_bvh.html

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<body>
1616
<div id="info">
1717
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> batch lod bvh - <a href="https://github.com/agargaro/batched-mesh-extensions" target="_blank" rel="noopener">@three.ez/batched-mesh-extensions</a><br/>
18-
BatchedMesh with 10 geometries and 500k instances. Each geometry has 4 LODs generated with meshoptimizer. <br>
18+
BatchedMesh with 10 geometries and 500k instances. Each geometry has 5 LODs (4 generated with meshoptimizer). <br>
1919
Frustum culling and raycasting are accelerated by using BVHs (TLAS & BLAS). <br>
2020
See <a href="https://github.com/agargaro/batched-mesh-extensions" target="_blank" rel="noopener">main project repository</a> for more information and examples on BatchedMesh extensions.
2121
</div>
@@ -56,9 +56,10 @@
5656
THREE.Mesh.prototype.raycast = acceleratedRaycast;
5757
THREE.BatchedMesh.prototype.computeBoundsTree = computeBatchedBoundsTree;
5858

59-
const instancesCount = 500000;
6059
let stats;
6160
let camera, scene, renderer;
61+
62+
const instancesCount = 500000;
6263
let batchedMesh;
6364
let lastHoveredInstance = null;
6465
const lastHoveredColor = new THREE.Color();
@@ -116,7 +117,7 @@
116117
new THREE.TorusKnotGeometry( 1, 0.4, 256, 32, 5, 3 )
117118
];
118119

119-
// generate 4 LODs (Levels of Detail) for each geometry
120+
// generate 4 LODs (levels of detail) for each geometry
120121
const geometriesLODArray = await simplifyGeometriesByErrorLOD( geometries, 4, performanceRangeLOD );
121122

122123
// create BatchedMesh
@@ -165,15 +166,24 @@
165166

166167
// set up gui
167168
const config = {
169+
freeze: false,
168170
useBVH: true,
169171
useLOD: true
170172
};
171173

172174
const bvh = batchedMesh.bvh;
173175
const lods = batchedMesh._geometryInfo.map( x => x.LOD );
176+
const onBeforeRender = batchedMesh.onBeforeRender;
174177

175178
const gui = new GUI();
179+
176180
gui.add( batchedMesh, 'instanceCount' ).disable();
181+
182+
gui.add( config, 'freeze' ).onChange( v => {
183+
184+
batchedMesh.onBeforeRender = v ? () => {} : onBeforeRender;
185+
186+
} );
177187

178188
const frustumCullingFolder = gui.addFolder( 'Frustum culling & raycasting' );
179189
frustumCullingFolder.add( config, 'useBVH' ).onChange( v => {
@@ -182,8 +192,8 @@
182192

183193
} );
184194

185-
const GeometriesFolder = gui.addFolder( 'Geometries' );
186-
GeometriesFolder.add( config, 'useLOD' ).onChange( v => {
195+
const geometriesFolder = gui.addFolder( 'Geometries' );
196+
geometriesFolder.add( config, 'useLOD' ).onChange( v => {
187197

188198
const geometryInfo = batchedMesh._geometryInfo;
189199
for ( let i = 0; i < geometryInfo.length; i ++ ) {
@@ -194,7 +204,7 @@
194204

195205
} );
196206

197-
document.addEventListener( 'mousemove', onMouseMove );
207+
document.addEventListener( 'pointermove', onPointerMove );
198208
window.addEventListener( 'resize', onWindowResize );
199209
onWindowResize();
200210

@@ -203,7 +213,7 @@
203213
}
204214

205215

206-
function onMouseMove( event ) {
216+
function onPointerMove( event ) {
207217

208218
event.preventDefault();
209219

@@ -229,29 +239,25 @@
229239
raycaster.setFromCamera( mouse, camera );
230240
const intersection = raycaster.intersectObject( batchedMesh );
231241

232-
if ( intersection.length > 0 ) {
233-
234-
const batchId = intersection[ 0 ].batchId;
235-
236-
if ( lastHoveredInstance === batchId ) return;
242+
const batchId = intersection.length > 0 ? intersection[ 0 ].batchId : null;
243+
244+
if ( lastHoveredInstance === batchId ) return;
237245

238-
if ( lastHoveredInstance ) {
246+
if ( lastHoveredInstance ) {
239247

240-
batchedMesh.setColorAt( lastHoveredInstance, lastHoveredColor );
248+
batchedMesh.setColorAt( lastHoveredInstance, lastHoveredColor );
241249

242-
}
250+
}
243251

244-
lastHoveredInstance = batchId;
245-
batchedMesh.getColorAt( lastHoveredInstance, lastHoveredColor );
246-
batchedMesh.setColorAt( lastHoveredInstance, yellow );
252+
if ( batchId ) {
247253

248-
} else if ( lastHoveredInstance ) {
254+
batchedMesh.getColorAt( batchId, lastHoveredColor );
255+
batchedMesh.setColorAt( batchId, yellow );
249256

250-
batchedMesh.setColorAt( lastHoveredInstance, lastHoveredColor );
251-
lastHoveredInstance = null;
252-
253257
}
254258

259+
lastHoveredInstance = batchId;
260+
255261
}
256262

257263
function animate() {

0 commit comments

Comments
 (0)