Skip to content

Commit

Permalink
Usability methods for RenderTargets and image handles (bevyengine#10736)
Browse files Browse the repository at this point in the history
# Objective

In my code I use a lot of images as render targets.
I'd like some convenience methods for working with this type.

## Solution

- Allow `.into()` to construct a `RenderTarget`
- Add `.as_image()` 

---

## Changelog

### Added

- `RenderTarget` can be constructed via `.into()` on a `Handle<Image>`
- `RenderTarget` new method: `as_image`

---------

Signed-off-by: Torstein Grindvik <[email protected]>
Co-authored-by: Torstein Grindvik <[email protected]>
  • Loading branch information
torsteingrindvik and Torstein Grindvik authored Jan 4, 2024
1 parent 93c7e7c commit 99c43fa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 16 additions & 0 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ pub enum RenderTarget {
TextureView(ManualTextureViewHandle),
}

impl From<Handle<Image>> for RenderTarget {
fn from(handle: Handle<Image>) -> Self {
Self::Image(handle)
}
}

/// Normalized version of the render target.
///
/// Once we have this we shouldn't need to resolve it down anymore.
Expand Down Expand Up @@ -459,6 +465,16 @@ impl RenderTarget {
RenderTarget::TextureView(id) => Some(NormalizedRenderTarget::TextureView(*id)),
}
}

/// Get a handle to the render target's image,
/// or `None` if the render target is another variant.
pub fn as_image(&self) -> Option<&Handle<Image>> {
if let Self::Image(handle) = self {
Some(handle)
} else {
None
}
}
}

impl NormalizedRenderTarget {
Expand Down
3 changes: 1 addition & 2 deletions examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::f32::consts::PI;
use bevy::{
prelude::*,
render::{
camera::RenderTarget,
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
Expand Down Expand Up @@ -103,7 +102,7 @@ fn setup(
camera: Camera {
// render before the "main pass" camera
order: -1,
target: RenderTarget::Image(image_handle.clone()),
target: image_handle.clone().into(),
clear_color: Color::WHITE.into(),
..default()
},
Expand Down

0 comments on commit 99c43fa

Please sign in to comment.