Skip to content

Commit 40b5d79

Browse files
committed
demo: Add a total simplified ratio to the page
This makes the demo easier to use in auto-LOD mode. Also: - Fix the distance calculation; since we recenter the model, we effectively move the model's bounding sphere to origin - Fix the repeated simplification when auto-LOD is off and camera moves
1 parent afd2682 commit 40b5d79

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

demo/simplify.html

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<body>
4545
<div id="info">
4646
<a href="https://github.com/zeux/meshoptimizer" target="_blank" rel="noopener">meshoptimizer</a>
47+
<div id="ratio"></div>
4748
</div>
4849

4950
<script type="module">
@@ -298,16 +299,21 @@
298299
MeshoptSimplifier.ready.then(function () {
299300
MeshoptSimplifier.useExperimentalFeatures = true;
300301

302+
var rnum = 0;
303+
var rden = 0;
304+
301305
var triangles = 0;
302306
var vertices = 0;
303307
var error = 0;
304308

305309
var threshold = Math.pow(10, -settings.errorThresholdLog10);
306310

307311
if (settings.autoLod) {
308-
// subtract 1 because we assume model radius is 1.0, so this is a distance-to-sphere
309-
// ideally this should actually use the real radius :) but we rescale the model to fit a unit box
310-
var distance = Math.max(camera.position.distanceTo(model.position) - 1, 0);
312+
// compute distance to model sphere
313+
// ideally this should actually use the real radius and center :) but we rescale/center the model when loading
314+
var center = new THREE.Vector3();
315+
var radius = 1;
316+
var distance = Math.max(camera.position.distanceTo(center) - radius, 0);
311317
var loderrortarget = 1e-3; // ~1 pixel at 1k x 1k
312318

313319
// note: we are currently cutting corners wrt handling the actual mesh scale
@@ -343,17 +349,23 @@
343349
error = Math.max(error, err); // note: we are ignoring the possibility of different mesh scales atm
344350
triangles += tri;
345351
vertices += object.geometry.attributes.position.count;
352+
rnum += tri;
353+
rden += object.original.index.count / 3;
346354
}
347355
if (object.isPoints) {
348356
simplifyPoints(object.geometry);
349357

350-
vertices += object.geometry.attributes.position.count;
358+
vertices += object.geometry.index.count;
359+
rnum += object.geometry.index.count;
360+
rden += object.geometry.attributes.position.count;
351361
}
352362
});
353363

354364
settings.stats.triangles = triangles;
355365
settings.stats.vertices = vertices;
356366
settings.stats.error = error.toExponential(3);
367+
368+
document.getElementById('ratio').innerText = ((rnum / rden) * 100).toFixed(1) + '%';
357369
});
358370
}
359371

@@ -460,7 +472,12 @@
460472
camera.position.z = 3.0;
461473

462474
controls = new OrbitControls(camera, renderer.domElement);
463-
controls.addEventListener('change', simplify);
475+
476+
controls.addEventListener('change', function () {
477+
if (settings.autoLod) {
478+
simplify();
479+
}
480+
});
464481

465482
scene = new THREE.Scene();
466483
scene.background = new THREE.Color(0x300a24);

0 commit comments

Comments
 (0)