sigmoid in Splat.js is overly complex
Description
splat.js converts opacity to alpha via a sigmoid function. This is called for every splat so optimal code is important. The implementation used is branched to provide greater accuracy by reducing the effect of rounding errors for negative numbers outside of the range -36 .. 0 (they round to 1.0). However the sigmoid is only used to generate integer values from 0...255 for the texture alpha. Now 1.0 / 255.0 = 0.0039.. and the sigmoid for -37 is 8.533047625744066e-17, so the greater accuracy is wasted effort.
const sigmoid = (v) => {
if (v > 0) {
return 1 / (1 + Math.exp(-v));
}
const t = Math.exp(v);
return t / (1 + t);
};
I propose a simple optimisation to:
const sigmoid = (v) => {
return 1 / (1 + Math.exp(-v));
};
Which should give identical alpha results, but faster without the branch.
Would you happen to have benchmark from a sample splat? The only difference between the two is code branching.
@slimbuck - any thoughts on this?
You're right @pjbaron that this function is being slightly more careful than strictly necessary, but the performance difference you're proposing is minute and only effects loading time or conversion time so I don't see a compelling reason to make any changes here.
(If I've missed something and/or you've noticed a performance issue somewhere please reopen this here ticket!)