Skip to content

Commit

Permalink
Merge pull request #236 from asny/render-target
Browse files Browse the repository at this point in the history
Screen is a render target #200 + #233 + #235
  • Loading branch information
asny authored Apr 25, 2022
2 parents 17a5ce2 + 9fa8dfc commit ecf4ec3
Show file tree
Hide file tree
Showing 62 changed files with 1,913 additions and 965 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ tokio = { version = "1.16", features = ["macros", "rt-multi-thread"] }
name = "triangle"
path = "examples/triangle/src/main.rs"

[[example]]
name = "screen"
path = "examples/screen/src/main.rs"

[[example]]
name = "mandelbrot"
path = "examples/mandelbrot/src/main.rs"
Expand Down
84 changes: 44 additions & 40 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,87 +35,91 @@ All of the examples builds to both native (desktop, mobile or whatever target sp
Because they should run in a browser and to keep the same code for native and wasm, all loading happens async.
If your application is native only, you can avoid the async runtime (`tokio` or `async-std`) and use `Loader::load_blocking` instead of `Loader::load_async`.

## Triangle [[code](https://github.com/asny/three-d/tree/master/examples/triangle/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/triangle.html)]
## Triangle [[code](https://github.com/asny/three-d/tree/master/examples/triangle/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/triangle.html)]

This is the recomended starting point for a gentle introduction to `three-d`.

![Triangle example](https://asny.github.io/three-d/0.11/triangle.png)
![Triangle example](https://asny.github.io/three-d/0.12/triangle.png)

## Mandelbrot [[code](https://github.com/asny/three-d/tree/master/examples/mandelbrot/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/mandelbrot.html)]
## Mandelbrot [[code](https://github.com/asny/three-d/tree/master/examples/mandelbrot/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/mandelbrot.html)]

![Mandelbrot example](https://asny.github.io/three-d/0.11/mandelbrot.png)
![Mandelbrot example](https://asny.github.io/three-d/0.12/mandelbrot.png)

## Shapes2D [[code](https://github.com/asny/three-d/tree/master/examples/shapes2d/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/shapes2d.html)]
## Shapes2D [[code](https://github.com/asny/three-d/tree/master/examples/shapes2d/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/shapes2d.html)]

![Shapes2d example](https://asny.github.io/three-d/0.11/shapes2d.png)
![Shapes2d example](https://asny.github.io/three-d/0.12/shapes2d.png)

## Shapes [[code](https://github.com/asny/three-d/tree/master/examples/shapes/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/shapes.html)]
## Shapes [[code](https://github.com/asny/three-d/tree/master/examples/shapes/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/shapes.html)]

![Shapes example](https://asny.github.io/three-d/0.11/shapes.png)
![Shapes example](https://asny.github.io/three-d/0.12/shapes.png)

## Sprites [[code](https://github.com/asny/three-d/tree/master/examples/sprites/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/sprites.html)]
## Screen [[code](https://github.com/asny/three-d/tree/master/examples/screen/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/screen.html)]

![Sprites example](https://asny.github.io/three-d/0.11/sprites.png)
![Screen example](https://asny.github.io/three-d/0.12/screen.png)

## Texture [[code](https://github.com/asny/three-d/tree/master/examples/texture/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/texture.html)]
## Sprites [[code](https://github.com/asny/three-d/tree/master/examples/sprites/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/sprites.html)]

![Texture example](https://asny.github.io/three-d/0.11/texture.png)
![Sprites example](https://asny.github.io/three-d/0.12/sprites.png)

## Picking [[code](https://github.com/asny/three-d/tree/master/examples/picking/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/picking.html)]
## Texture [[code](https://github.com/asny/three-d/tree/master/examples/texture/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/texture.html)]

![Picking example](https://asny.github.io/three-d/0.11/picking.png)
![Texture example](https://asny.github.io/three-d/0.12/texture.png)

## Environment [[code](https://github.com/asny/three-d/tree/master/examples/environment/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/environment.html)]
## Picking [[code](https://github.com/asny/three-d/tree/master/examples/picking/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/picking.html)]

![Environment example](https://asny.github.io/three-d/0.11/environment.png)
![Picking example](https://asny.github.io/three-d/0.12/picking.png)

## PBR [[code](https://github.com/asny/three-d/tree/master/examples/pbr/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/pbr.html)]
## Environment [[code](https://github.com/asny/three-d/tree/master/examples/environment/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/environment.html)]

![PBR example](https://asny.github.io/three-d/0.11/pbr.png)
![Environment example](https://asny.github.io/three-d/0.12/environment.png)

## Lighting [[code](https://github.com/asny/three-d/tree/master/examples/lighting/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/lighting.html)]
## PBR [[code](https://github.com/asny/three-d/tree/master/examples/pbr/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/pbr.html)]

![Lighting example](https://asny.github.io/three-d/0.11/lighting.png)
![PBR example](https://asny.github.io/three-d/0.12/pbr.png)

## Lights [[code](https://github.com/asny/three-d/tree/master/examples/lights/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/lights.html)]
## Lighting [[code](https://github.com/asny/three-d/tree/master/examples/lighting/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/lighting.html)]

![Lights example](https://asny.github.io/three-d/0.11/lights.png)
![Lighting example](https://asny.github.io/three-d/0.12/lighting.png)

## Image [[code](https://github.com/asny/three-d/tree/master/examples/image/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/image.html)]
## Lights [[code](https://github.com/asny/three-d/tree/master/examples/lights/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/lights.html)]

![Image example](https://asny.github.io/three-d/0.11/image.png)
![Lights example](https://asny.github.io/three-d/0.12/lights.png)

## Fog [[code](https://github.com/asny/three-d/tree/master/examples/fog/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/fog.html)]
## Image [[code](https://github.com/asny/three-d/tree/master/examples/image/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/image.html)]

![Fog example](https://asny.github.io/three-d/0.11/fog.png)
![Image example](https://asny.github.io/three-d/0.12/image.png)

## Fireworks [[code](https://github.com/asny/three-d/tree/master/examples/fireworks/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/fireworks.html)]
## Fog [[code](https://github.com/asny/three-d/tree/master/examples/fog/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/fog.html)]

![Fireworks example](https://asny.github.io/three-d/0.11/fireworks.png)
![Fog example](https://asny.github.io/three-d/0.12/fog.png)

## Statues [[code](https://github.com/asny/three-d/tree/master/examples/statues/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/statues.html)]
## Fireworks [[code](https://github.com/asny/three-d/tree/master/examples/fireworks/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/fireworks.html)]

![Statues example](https://asny.github.io/three-d/0.11/statues.png)
![Fireworks example](https://asny.github.io/three-d/0.12/fireworks.png)

## Wireframe [[code](https://github.com/asny/three-d/tree/master/examples/wireframe/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/wireframe.html)]
## Statues [[code](https://github.com/asny/three-d/tree/master/examples/statues/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/statues.html)]

![Wireframe example](https://asny.github.io/three-d/0.11/wireframe.png)
![Statues example](https://asny.github.io/three-d/0.12/statues.png)

## Forest [[code](https://github.com/asny/three-d/tree/master/examples/forest/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/forest.html)]
## Wireframe [[code](https://github.com/asny/three-d/tree/master/examples/wireframe/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/wireframe.html)]

![Forest example](https://asny.github.io/three-d/0.11/forest.png)
![Wireframe example](https://asny.github.io/three-d/0.12/wireframe.png)

## Volume [[code](https://github.com/asny/three-d/tree/master/examples/volume/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/volume.html)]
## Forest [[code](https://github.com/asny/three-d/tree/master/examples/forest/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/forest.html)]

![Volume example](https://asny.github.io/three-d/0.11/volume.png)
![Forest example](https://asny.github.io/three-d/0.12/forest.png)

## Normals [[code](https://github.com/asny/three-d/tree/master/examples/normals/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/normals.html)]
## Volume [[code](https://github.com/asny/three-d/tree/master/examples/volume/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/volume.html)]

![Normals example](https://asny.github.io/three-d/0.11/normals.png)
![Volume example](https://asny.github.io/three-d/0.12/volume.png)

## Logo [[code](https://github.com/asny/three-d/tree/master/examples/logo/src/main.rs)] [[demo](https://asny.github.io/three-d/0.11/logo.html)]
## Normals [[code](https://github.com/asny/three-d/tree/master/examples/normals/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/normals.html)]

![Logo example](https://asny.github.io/three-d/0.11/logo.png)
![Normals example](https://asny.github.io/three-d/0.12/normals.png)

## Logo [[code](https://github.com/asny/three-d/tree/master/examples/logo/src/main.rs)] [[demo](https://asny.github.io/three-d/0.12/logo.html)]

![Logo example](https://asny.github.io/three-d/0.12/logo.png)

## Headless [[code](https://github.com/asny/three-d/tree/master/examples/headless/src/main.rs)]

Expand Down
28 changes: 13 additions & 15 deletions examples/environment/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn run() {
let mut color = [1.0; 4];
window
.render_loop(move |mut frame_input| {
let mut panel_width = 0;
let mut panel_width = 0.0;
gui.update(&mut frame_input, |gui_context| {
use three_d::egui::*;
SidePanel::left("side_panel").show(gui_context, |ui| {
Expand All @@ -66,33 +66,31 @@ pub async fn run() {
ui.add(Slider::new(&mut model.material.roughness, 0.0..=1.0).text("Roughness"));
ui.color_edit_button_rgba_unmultiplied(&mut color);
});
panel_width = gui_context.used_size().x as u32;
panel_width = gui_context.used_size().x as f64;
})
.unwrap();
model.material.albedo = Color::from_rgba_slice(&color);

let viewport = Viewport {
x: panel_width as i32,
x: (panel_width * frame_input.device_pixel_ratio) as i32,
y: 0,
width: frame_input.viewport.width - panel_width,
width: frame_input.viewport.width
- (panel_width * frame_input.device_pixel_ratio) as u32,
height: frame_input.viewport.height,
};
camera.set_viewport(viewport).unwrap();
control
.handle_events(&mut camera, &mut frame_input.events)
.unwrap();

Screen::write(
&context,
ClearState::color_and_depth(0.5, 0.5, 0.5, 1.0, 1.0),
|| {
skybox.render(&camera, &[&light])?;
model.render(&camera, &[&light])?;
gui.render()?;
Ok(())
},
)
.unwrap();
frame_input
.screen()
.clear(ClearState::color_and_depth(0.5, 0.5, 0.5, 1.0, 1.0))
.unwrap()
.render(&camera, &[&skybox as &dyn Object, &model], &[&light])
.unwrap()
.write(|| gui.render())
.unwrap();

FrameOutput::default()
})
Expand Down
21 changes: 12 additions & 9 deletions examples/fireworks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl Material for FireworksMaterial {
},
depth_test: DepthTest::Always,
write_mask: WriteMask::COLOR,
clip: Clip::Disabled,
}
}
fn is_transparent(&self) -> bool {
Expand Down Expand Up @@ -138,14 +137,18 @@ pub fn run() {
particles.update(&data).unwrap();
}

Screen::write(&context, ClearState::color(0.0, 0.0, 0.0, 1.0), || {
let f = particles.time / explosion_time.max(0.0);
fireworks_material.fade = 1.0 - f * f * f * f;
fireworks_material.color = colors[color_index];
particles.render_with_material(&fireworks_material, &camera, &[])?;
Ok(())
})
.unwrap();
frame_input
.screen()
.clear(ClearState::color(0.0, 0.0, 0.0, 1.0))
.unwrap()
.write(|| {
let f = particles.time / explosion_time.max(0.0);
fireworks_material.fade = 1.0 - f * f * f * f;
fireworks_material.color = colors[color_index];
particles.render_with_material(&fireworks_material, &camera, &[])?;
Ok(())
})
.unwrap();

FrameOutput::default()
})
Expand Down
36 changes: 20 additions & 16 deletions examples/fog/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,27 @@ pub async fn run() {
);
}

Screen::write(&context, ClearState::default(), || {
monkey
.as_ref()
.unwrap()
.render(&camera, &[&ambient, &directional])?;
if fog_enabled {
if let Some(ref depth_texture) = depth_texture {
fog_effect.apply(
&camera,
depth_texture,
frame_input.accumulated_time as f32,
)?;
frame_input
.screen()
.clear(ClearState::default())
.unwrap()
.write(|| {
monkey
.as_ref()
.unwrap()
.render(&camera, &[&ambient, &directional])?;
if fog_enabled {
if let Some(ref depth_texture) = depth_texture {
fog_effect.apply(
&camera,
depth_texture,
frame_input.accumulated_time as f32,
)?;
}
}
}
Ok(())
})
.unwrap();
Ok(())
})
.unwrap();

FrameOutput::default()
})
Expand Down
22 changes: 8 additions & 14 deletions examples/forest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,14 @@ pub async fn run() {
.unwrap();

if redraw {
Screen::write(
&context,
ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0),
|| {
render_pass(
&camera,
&models.iter().map(|m| m as &dyn Object).collect::<Vec<_>>(),
&[&ambient, &directional],
)?;
imposters.render(&camera, &[])?;
Ok(())
},
)
.unwrap();
let mut models = models.iter().map(|m| m as &dyn Object).collect::<Vec<_>>();
models.push(&imposters);
frame_input
.screen()
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.unwrap()
.render(&camera, &models, &[&ambient, &directional])
.unwrap();
}

FrameOutput {
Expand Down
36 changes: 21 additions & 15 deletions examples/headless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,33 @@ fn main() {

// Render three frames
for frame_index in 0..3 {
// Create a render target (a combination of a color and a depth texture) to write into and clear the color and depth
RenderTarget::new(&context, &mut texture, &mut depth_texture)
.unwrap()
.write(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0), || {
// Set the current transformation of the triangle
model.set_transformation(Mat4::from_angle_y(radians(
(frame_index as f32 * 0.6) as f32,
)));
// Set the current transformation of the triangle
model.set_transformation(Mat4::from_angle_y(radians(
(frame_index as f32 * 0.6) as f32,
)));

// Render the triangle with the per vertex colors defined at construction
model.render(&camera, &[])
})
.unwrap();
// Create a render target (a combination of a color and a depth texture) to write into
let pixels = RenderTarget::new(
texture.as_color_target(None),
depth_texture.as_depth_target(),
)
.unwrap()
// Clear color and depth of the render target
.clear(ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0))
.unwrap()
// Render the triangle with the per vertex colors defined at construction
.render(&camera, &[&model], &[])
.unwrap()
// Read out the colors from the render target
.read_color()
.unwrap();

// Save the rendered image
let pixels = texture.read(viewport).unwrap();
Saver::save_pixels(
format!("headless-{}.png", frame_index),
&pixels,
viewport.width,
viewport.height,
texture.width(),
texture.height(),
)
.unwrap();
}
Expand Down
Loading

0 comments on commit ecf4ec3

Please sign in to comment.