Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_render/src/texture/manual_texture_view.rs
6849 views
1
use bevy_camera::ManualTextureViewHandle;
2
use bevy_ecs::{prelude::Component, resource::Resource};
3
use bevy_image::BevyDefault;
4
use bevy_math::UVec2;
5
use bevy_platform::collections::HashMap;
6
use bevy_render_macros::ExtractResource;
7
use wgpu::TextureFormat;
8
9
use crate::render_resource::TextureView;
10
11
/// A manually managed [`TextureView`] for use as a [`bevy_camera::RenderTarget`].
12
#[derive(Debug, Clone, Component)]
13
pub struct ManualTextureView {
14
pub texture_view: TextureView,
15
pub size: UVec2,
16
pub format: TextureFormat,
17
}
18
19
impl ManualTextureView {
20
pub fn with_default_format(texture_view: TextureView, size: UVec2) -> Self {
21
Self {
22
texture_view,
23
size,
24
format: TextureFormat::bevy_default(),
25
}
26
}
27
}
28
29
/// Resource that stores manually managed [`ManualTextureView`]s for use as a [`RenderTarget`](bevy_camera::RenderTarget).
30
/// This type dereferences to a `HashMap<ManualTextureViewHandle, ManualTextureView>`.
31
/// To add a new texture view, pick a new [`ManualTextureViewHandle`] and insert it into the map.
32
/// Then, to render to the view, set a [`Camera`](bevy_camera::Camera)s `target` to `RenderTarget::TextureView(handle)`.
33
/// ```ignore
34
/// # use bevy_ecs::prelude::*;
35
/// # let mut world = World::default();
36
/// # world.insert_resource(ManualTextureViews::default());
37
/// # let texture_view = todo!();
38
/// let manual_views = world.resource_mut::<ManualTextureViews>();
39
/// let manual_view = ManualTextureView::with_default_format(texture_view, UVec2::new(1024, 1024));
40
///
41
/// // Choose an unused handle value; it's likely only you are inserting manual views.
42
/// const MANUAL_VIEW_HANDLE: ManualTextureViewHandle = ManualTextureViewHandle::new(42);
43
/// manual_views.insert(MANUAL_VIEW_HANDLE, manual_view);
44
///
45
/// // Now you can spawn a Cemera that renders to the manual view:
46
/// # use bevy_camera::{Camera, RenderTarget};
47
/// world.spawn(Camera {
48
/// target: RenderTarget::TextureView(MANUAL_VIEW_HANDLE),
49
/// ..Default::default()
50
/// });
51
/// ```
52
/// Bevy will then use the `ManualTextureViews` resource to find your texture view and render to it.
53
#[derive(Default, Clone, Resource, ExtractResource)]
54
pub struct ManualTextureViews(HashMap<ManualTextureViewHandle, ManualTextureView>);
55
56
impl core::ops::Deref for ManualTextureViews {
57
type Target = HashMap<ManualTextureViewHandle, ManualTextureView>;
58
59
fn deref(&self) -> &Self::Target {
60
&self.0
61
}
62
}
63
64
impl core::ops::DerefMut for ManualTextureViews {
65
fn deref_mut(&mut self) -> &mut Self::Target {
66
&mut self.0
67
}
68
}
69
70