Skip to content

Commit

Permalink
Render to texture example: No need to create an image handle manually. (
Browse files Browse the repository at this point in the history
bevyengine#4223)

# Objective

- Make the example a little easier to follow by removing unnecessary steps.

## Solution

- `Assets<Image>` will give us a handle for our render texture if we call `add()` instead of `set()`.  No need to set it manually; one less thing to think about while reading the example.
  • Loading branch information
cdbfoster authored and ItsDoot committed Feb 1, 2023
1 parent f7394f1 commit 3c66f50
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions examples/3d/render_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use bevy::{
draw_3d_graph, node, AlphaMask3d, Opaque3d, RenderTargetClearColors, Transparent3d,
},
prelude::*,
reflect::TypeUuid,
render::{
camera::{ActiveCamera, Camera, CameraTypePlugin, RenderTarget},
render_graph::{Node, NodeRunError, RenderGraph, RenderGraphContext, SlotValue},
Expand All @@ -20,10 +19,6 @@ use bevy::{
#[derive(Component, Default)]
pub struct FirstPassCamera;

// This handle will point at the texture to which we will render in the first pass.
pub const RENDER_IMAGE_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Image::TYPE_UUID, 13378939762009864029);

// The name of the final node of the first pass.
pub const FIRST_PASS_DRIVER: &str = "first_pass_driver";

Expand Down Expand Up @@ -145,7 +140,7 @@ fn setup(
// fill image.data with zeroes
image.resize(size);

let image_handle = images.set(RENDER_IMAGE_HANDLE, image);
let image_handle = images.add(image);

let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 4.0 }));
let cube_material_handle = materials.add(StandardMaterial {
Expand Down Expand Up @@ -177,7 +172,7 @@ fn setup(
});

// First pass camera
let render_target = RenderTarget::Image(image_handle);
let render_target = RenderTarget::Image(image_handle.clone());
clear_colors.insert(render_target.clone(), Color::WHITE);
commands
.spawn_bundle(PerspectiveCameraBundle::<FirstPassCamera> {
Expand Down Expand Up @@ -209,7 +204,7 @@ fn setup(

// This material has the texture that has been rendered.
let material_handle = materials.add(StandardMaterial {
base_color_texture: Some(RENDER_IMAGE_HANDLE.typed()),
base_color_texture: Some(image_handle),
reflectance: 0.02,
unlit: false,
..default()
Expand Down

0 comments on commit 3c66f50

Please sign in to comment.