Skip to content

Commit

Permalink
demo: Fix color scaling behavior for simplification #nojira
Browse files Browse the repository at this point in the history
When simplifying meshes, we no longer need to divide by 255 because
BufferAttribute.getX will do this for us as of three.js 144. This was
missed during a three.js version upgrade which broke attribute awareness
in the demo by using very small color values that ended up not affecting
the simplification result.

When simplifying points, we are converting typed arrays without any
normalization; as such, we need to normalize these by applying the
scaling factor to color weight.
  • Loading branch information
zeux committed Oct 15, 2023
1 parent 3df2895 commit f23b67d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions demo/simplify.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,14 @@

var attrib_weights = [
settings.normalWeight, settings.normalWeight, settings.normalWeight,
settings.colorWeight / 255, settings.colorWeight / 255, settings.colorWeight / 255,
settings.colorWeight, settings.colorWeight, settings.colorWeight,
];
}

var flags = [];
if (settings.lockBorder)
if (settings.lockBorder) {
flags.push("LockBorder");
}

var threshold = Math.pow(10, -settings.errorThresholdLog10);
var stride = (geo.attributes.position instanceof THREE.InterleavedBufferAttribute) ? geo.attributes.position.data.stride : 3;
Expand Down Expand Up @@ -176,9 +177,12 @@
var pos_stride = (geo.attributes.position instanceof THREE.InterleavedBufferAttribute) ? geo.attributes.position.data.stride : 3;
var col_stride = (geo.attributes.color instanceof THREE.InterleavedBufferAttribute) ? geo.attributes.color.data.stride : geo.attributes.color.itemSize;

// note: we are converting source color array without normalization; if the color data is quantized, we need to divide by 255 to normalize it
var colorScale = geo.attributes.color && geo.attributes.color.normalized ? 255 : 1;

console.time('simplify');

var res = MeshoptSimplifier.simplifyPoints(positions, pos_stride, target, colors, col_stride, settings.colorWeight);
var res = MeshoptSimplifier.simplifyPoints(positions, pos_stride, target, colors, col_stride, settings.colorWeight / colorScale);

console.timeEnd('simplify');

Expand All @@ -191,10 +195,12 @@
{
MeshoptSimplifier.ready.then(function() {
scene.traverse(function (object) {
if (object.isMesh)
if (object.isMesh) {
simplifyMesh(object.geometry);
if (object.isPoints)
}
if (object.isPoints) {
simplifyPoints(object.geometry);
}
});
});
}
Expand Down Expand Up @@ -225,8 +231,7 @@

function load(path)
{
if (model)
{
if (model) {
scene.remove(model);
model = undefined;
}
Expand Down

0 comments on commit f23b67d

Please sign in to comment.