Defold: Floating point rounding errors producing texture-edge anomalies

Created on 14 Feb 2021  路  4Comments  路  Source: defold/defold

Fresh install, trying the Movement tutorial, I noticed boundary issues along the top- and left- edge of the ship texture that are typically indicative of floating-point/clamping issues. It's often the cause of inconsistent terrain seams where the same tile will produce a tiny see-thru gap at some positions and not others.

It's usually caused by someone being too trusting of floating point values, thinking that rounding and misrepresentation errors only happen at extreme values (did you realize lua cannot store 0.1 accurately?) or that the errors are so small as to be irrelevant.

When, in-fact, (int)((4096 * d - (float)f) * 10) != 4096 or 4095 but 4094.

Frequently what happens is that someone does some trivial math with perfectly reasonable numbers to try and determine the bounding box of a texture in an atlas or the scaled destination coordinates for copying it, but there is a large-enough rounding error to introduce an off-by-1 error and the wrong pixels get copied or the pixels get copied to an off-by-1 location,

https://www.youtube.com/watch?v=lul3SNJT0Fc

bug

All 4 comments

The sprites.atlas of this tutorial does not have any extrude borders / margin / inner padding set. So that is why you see texture bleed. I don't know the exact technical cause but adding those values, such as extruding borders (a default of 2 is in atlases now), would fix it. If you can find a problem in the implementation somewhere that may be helpful, but this kind of issue is not unique to Defold, I've seen it in every tool that uses packed textures while packing textures too closely. You could try setting the fragment shader to highp instead of lowp but I don't know if that will fix the bleeding issue in this case.

The problem is definitely texture bleeding. The tutorial was created a long time ago and for some reason we forgot to set the Extrude Borders option on the atlas. This has now been resolved:

https://github.com/defold/tutorial-movement/commit/fe33bf7b2de847f202beb6248f07303dadaf68c3

Any future users trying out the tutorial will not observe the issues you noticed @kfsone

Thanks for the quick reply; I've fixed this in a few engines over the last couple decades, but this was literally still my first hour of doing tutorials :) When I'm a little more comfortable using the engine I might try and dive for it.

FWIW: In 5/6 cases the code was ultra-careful about epsilon/delta apart from that one case where the values seemed absolutely concrete to everyone, they looked concrete in the ide/debugger, but (paraphrasing actual numbers) "1 - (0.1 * 10)" turns out to be just negative enough to be -1 and only "%.15f" or greater revealed the difference.

(In the other case, the 'C for Dummies' book on the guy's desk should have been a clue to everyone on that team)

I've fixed this in a few engines over the last couple decades
When I'm a little more comfortable using the engine I might try and dive for it.

If you have experience with game engines and would like to take a look at the code in general and perhaps make a contribution please reach out to me or @JCash. We have a separate forum category for source code discussions. And you can always reach us at [email protected] or [email protected]

Was this page helpful?
0 / 5 - 0 ratings