bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Implement visibility ranges, also known as hierarchical levels of detail (HLODs).

Open pcwalton opened this issue 1 year ago • 6 comments

Implement visibility ranges, also known as hierarchical levels of detail (HLODs).

This commit introduces a new component, VisibilityRange, which allows developers to specify camera distances in which meshes are to be shown and hidden. Hiding meshes happens early in the rendering pipeline, so this feature can be used for level of detail optimization. Additionally, this feature is properly evaluated per-view, so different views can show different levels of detail.

This feature differs from proper mesh LODs, which can be implemented later. Engines generally implement true mesh LODs later in the pipeline; they're typically more efficient than HLODs with GPU-driven rendering. However, mesh LODs are more limited than HLODs, because they require the lower levels of detail to be meshes with the same vertex layout and shader (and perhaps the same material) as the original mesh. Games often want to use objects other than meshes to replace distant models, such as octahedral imposters or billboard imposters. Therefore, all game engines I'm aware of support HLODs:

Engine LOD HLOD
Unity Unsupported Supported ("LOD")
Unreal Supported Supported [^1]
Godot Supported Supported
three.js Unsupported Supported ("LOD")

Crossfading between different levels of detail is supported, using the standard 4x4 ordered dithering pattern from 1. The shader code to compute the dithering patterns should be well-optimized. The dithering code is only active when visibility ranges are in use for the mesh in question, so that we don't lose early Z.

Cascaded shadow maps show the HLOD level of the view they're associated with. Point light and spot light shadow maps, which have no CSMs, display all HLOD levels that are visible in any view. To support this efficiently and avoid doing visibility checks multiple times, we precalculate all visible HLOD levels for each entity with a VisibilityRange during the check_visibility_range system.

A new example, visibility_range, has been added to the tree, as well as a new low-poly version of the flight helmet model to go with it. It demonstrates use of the visibility range feature to provide levels of detail.

[^1]: Unreal doesn't have a feature that exactly corresponds to visibility ranges, but Unreal's HLOD system serves roughly the same purpose.

Changelog

Added

  • A new VisibilityRange component is available to conditionally enable entity visibility at camera distances, with optional crossfade support. This can be used to implement different levels of detail (LODs).

Screenshots

High-poly model: Screenshot 2024-04-09 185541

Low-poly model up close: Screenshot 2024-04-09 185546

Crossfading between the two: Screenshot 2024-04-09 185604

pcwalton avatar Apr 10 '24 01:04 pcwalton

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar Apr 10 '24 04:04 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar Apr 10 '24 22:04 github-actions[bot]

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

github-actions[bot] avatar Apr 11 '24 06:04 github-actions[bot]

This should be ready for review now.

pcwalton avatar Apr 11 '24 09:04 pcwalton

This fixes most of https://github.com/bevyengine/bevy/issues/6868. True mesh LOD would still be useful even after this lands, though.

pcwalton avatar Apr 11 '24 20:04 pcwalton

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

github-actions[bot] avatar Apr 23 '24 01:04 github-actions[bot]

@Elabajaba Yep, that's exactly right.

pcwalton avatar May 02 '24 01:05 pcwalton

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to https://github.com/bevyengine/bevy-website/issues/1361 if you'd like to help out.

alice-i-cecile avatar Jun 05 '24 12:06 alice-i-cecile