Usability methods for RenderTargets and image handles (#10736)

# 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 <torstein.grindvik@muybridge.com>
Co-authored-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
This commit is contained in:
Torstein Grindvik 2024-01-04 18:01:04 +01:00 committed by GitHub
parent 93c7e7cf4d
commit 99c43fabdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -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.
@ -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 {

View File

@ -5,7 +5,6 @@ use std::f32::consts::PI;
use bevy::{
prelude::*,
render::{
camera::RenderTarget,
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
@ -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()
},