Unity3d floating point precision limitations

前端 未结 1 1152
失恋的感觉
失恋的感觉 2021-01-06 00:04

I got a warning from Unity3D Pro that I don\'t quite understand. I set my player\'s Transform.position.x to 1000000 in the GUI and it gave me this warning:

相关标签:
1条回答
  • 2021-01-06 00:38

    For numbers of magnitude 1,000,000, the step size of IEEE-754 32-bit binary floating-point is 1/16 (.0625). So the next number above 1,000,000 that is representable is 1,000,000.0625.

    That might be fine if you have a view where changes of .0625 are not visible to the eye. If the scale of the objects in your world is such that .0625 is much smaller than any object feature, and the view will never zoom in to where differences of .0625 are noticeable to the human eye, then there may be no problem. (However, you may want a little more leeway. Even if object features of that size are not visible, some of the math may be affected, since any calculations of velocity, position, et cetera, cannot use increments smaller than .0625 while you are doing 32-bit arithmetic on numbers of magnitude 1,000,000.)

    Compare that to numbers near the origin, say numbers of magnitude 100. At 100, the step size is 1/131072 (.00000762939453125). If you are drawing objects that take advantage of this fine resolution, they will look fine as long as they are near the origin. However, when they move through your scene to places where the coordinates have larger magnitude, they lose resolution.

    The basic problem here is the ratio between the size of the scene and the size of the details in the scene. That is what is limited. You can have large coordinates as long as the object features are also large. But you cannot maintain fine object features while the coordinates are large.

    0 讨论(0)
提交回复
热议问题