Skip to content

Numeric instability of world_from_clip in orthographic projection #20943

@Lichtso

Description

@Lichtso

Bevy version and features

Version: v0.16.1
Only default features

Relevant system information

Rust 1.86.0
macOS 15.6.1
Apple silicon M4 Max

What you did

Using view.world_from_clip * vertex_output.clip_space_position to get a ray origin for some ray tracing in the fragment shader. The last row of view.world_from_clip however is highly unstable in orthographic projections and requires to be patched roughly like so: view.world_from_clip[3].xyz = view.world_position;

What went wrong

I assume the multiplication of inverted matrices in the uniforms leads to this instability. More specifically it appears that view_from_clip is at fault, not world_from_view. Which is to be expected because the near / (near - far) can produce a value far away from all others on the log scale, thus leading to floating point precision loss.

See the screenshots below for what they look like.

Additional information

Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-CameraUser-facing camera APIs and controllers.A-RenderingDrawing game state to the screenC-BugAn unexpected or incorrect behaviorS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions