pub struct ShapeBatch {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub index_id_counter: u32,
}
Fields§
§vertices: Vec<Vertex>
§indices: Vec<u32>
§index_id_counter: u32
Implementations§
Source§impl ShapeBatch
impl ShapeBatch
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/hello_triangle/main.rs (line 21)
5pub fn main() -> Result<(), Box<dyn Error>> {
6 //Create app and main window.
7 let mut app = Tridify::new();
8 let window = app.create_window()?;
9
10 //Stores WGPU context and devices. Must be used in most GPU functions.
11 let gpu_ctx = window.ctx();
12
13 //Create brush to draw the shapes.
14 let mut brush = Brush::from_source(
15 BrushDesc::default(),
16 gpu_ctx,
17 include_str!("shader.wgsl").to_string(),
18 )?;
19
20 //Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
21 let buffer = ShapeBatch::new()
22 .add_triangle([
23 vertex!(-0.5, -0.5, 0.0, Color::SILVER),
24 vertex!(0.5, -0.5, 0.0, Color::SILVER),
25 vertex!(0.0, 0.5, 0.0, Color::SILVER),
26 ])
27 .bake_buffers(gpu_ctx);
28
29 window.set_render_loop(move |gpu, _| {
30 //Create a render pass builder which we will use to define multiple render passes (In this case, only one).
31 let mut pass_builder = gpu.create_render_builder();
32
33 //Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
34 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
35 render_pass.render_shapes(gpu, &mut brush, &buffer);
36 render_pass.finish();
37
38 //Execute all drawing commands from all render passes and render into screen.
39 pass_builder.finish_render(gpu);
40 });
41
42 //Start program logic cycle.
43 app.start(());
44}
More examples
examples/texture_cube/main.rs (line 37)
6fn main() -> Result<(), Box<dyn Error>> {
7 //Create app and main window.
8 let mut app = Tridify::new();
9 let window = app.create_window()?;
10 let gpu_ctx = window.ctx();
11
12 //Load texture from path.
13 let texture = Texture::from_path(gpu_ctx, Path::new(r#"examples/texture_cube/texture.png"#));
14
15 //Sampler defines how the texture will be rendered in shapes.
16 let sampler = Sampler::new_default(gpu_ctx);
17
18 let camera = Camera::new(
19 Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
20 Projection::default(),
21 );
22 let mut camera_buf = camera.build_buffer(gpu_ctx);
23
24 //Create brush to draw the shapes.
25 let mut brush = Brush::from_source(
26 BrushDesc::default(),
27 gpu_ctx,
28 include_str!("shader.wgsl").to_string(),
29 )?;
30 //Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
31 //in the shader.
32 brush.bind(0, 0, camera_buf.clone());
33 brush.bind(1, 0, texture);
34 brush.bind(1, 1, sampler);
35
36 //Create and bake a shape batch with a cube in it.
37 let shape_buffer = ShapeBatch::new()
38 .add_cube(
39 Vec3::ZERO,
40 Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
41 Vec3::ONE * 5.,
42 Color::WHITE,
43 )
44 .bake_buffers(gpu_ctx);
45
46 //Setup the window render loop.
47 window.set_render_loop(move |gpu, frame_ctx| {
48 let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
49 let mvp = camera.build_camera_matrix() * model;
50
51 //Updating the gpu buffer will update all brushes binded as well.
52 camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
53
54 //Render frame as usual.
55 let mut pass_builder = gpu.create_render_builder();
56 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
57 render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
58 render_pass.finish();
59 pass_builder.finish_render(gpu);
60 });
61
62 // Start program.
63 app.start(());
64}
Sourcepub fn bake_buffers(&self, ctx: &GpuCtx) -> ShapeBuffer
pub fn bake_buffers(&self, ctx: &GpuCtx) -> ShapeBuffer
Create buffers based on current batch data.
Examples found in repository?
examples/hello_triangle/main.rs (line 27)
5pub fn main() -> Result<(), Box<dyn Error>> {
6 //Create app and main window.
7 let mut app = Tridify::new();
8 let window = app.create_window()?;
9
10 //Stores WGPU context and devices. Must be used in most GPU functions.
11 let gpu_ctx = window.ctx();
12
13 //Create brush to draw the shapes.
14 let mut brush = Brush::from_source(
15 BrushDesc::default(),
16 gpu_ctx,
17 include_str!("shader.wgsl").to_string(),
18 )?;
19
20 //Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
21 let buffer = ShapeBatch::new()
22 .add_triangle([
23 vertex!(-0.5, -0.5, 0.0, Color::SILVER),
24 vertex!(0.5, -0.5, 0.0, Color::SILVER),
25 vertex!(0.0, 0.5, 0.0, Color::SILVER),
26 ])
27 .bake_buffers(gpu_ctx);
28
29 window.set_render_loop(move |gpu, _| {
30 //Create a render pass builder which we will use to define multiple render passes (In this case, only one).
31 let mut pass_builder = gpu.create_render_builder();
32
33 //Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
34 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
35 render_pass.render_shapes(gpu, &mut brush, &buffer);
36 render_pass.finish();
37
38 //Execute all drawing commands from all render passes and render into screen.
39 pass_builder.finish_render(gpu);
40 });
41
42 //Start program logic cycle.
43 app.start(());
44}
More examples
examples/texture_cube/main.rs (line 44)
6fn main() -> Result<(), Box<dyn Error>> {
7 //Create app and main window.
8 let mut app = Tridify::new();
9 let window = app.create_window()?;
10 let gpu_ctx = window.ctx();
11
12 //Load texture from path.
13 let texture = Texture::from_path(gpu_ctx, Path::new(r#"examples/texture_cube/texture.png"#));
14
15 //Sampler defines how the texture will be rendered in shapes.
16 let sampler = Sampler::new_default(gpu_ctx);
17
18 let camera = Camera::new(
19 Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
20 Projection::default(),
21 );
22 let mut camera_buf = camera.build_buffer(gpu_ctx);
23
24 //Create brush to draw the shapes.
25 let mut brush = Brush::from_source(
26 BrushDesc::default(),
27 gpu_ctx,
28 include_str!("shader.wgsl").to_string(),
29 )?;
30 //Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
31 //in the shader.
32 brush.bind(0, 0, camera_buf.clone());
33 brush.bind(1, 0, texture);
34 brush.bind(1, 1, sampler);
35
36 //Create and bake a shape batch with a cube in it.
37 let shape_buffer = ShapeBatch::new()
38 .add_cube(
39 Vec3::ZERO,
40 Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
41 Vec3::ONE * 5.,
42 Color::WHITE,
43 )
44 .bake_buffers(gpu_ctx);
45
46 //Setup the window render loop.
47 window.set_render_loop(move |gpu, frame_ctx| {
48 let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
49 let mvp = camera.build_camera_matrix() * model;
50
51 //Updating the gpu buffer will update all brushes binded as well.
52 camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
53
54 //Render frame as usual.
55 let mut pass_builder = gpu.create_render_builder();
56 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
57 render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
58 render_pass.finish();
59 pass_builder.finish_render(gpu);
60 });
61
62 // Start program.
63 app.start(());
64}
pub fn add_mesh(&mut self, mesh: Mesh) -> &mut ShapeBatch
Sourcepub fn add_triangle(&mut self, v: [Vertex; 3]) -> &mut ShapeBatch
pub fn add_triangle(&mut self, v: [Vertex; 3]) -> &mut ShapeBatch
Add a triangle to the batch specifying its 3 vertices
Examples found in repository?
examples/hello_triangle/main.rs (lines 22-26)
5pub fn main() -> Result<(), Box<dyn Error>> {
6 //Create app and main window.
7 let mut app = Tridify::new();
8 let window = app.create_window()?;
9
10 //Stores WGPU context and devices. Must be used in most GPU functions.
11 let gpu_ctx = window.ctx();
12
13 //Create brush to draw the shapes.
14 let mut brush = Brush::from_source(
15 BrushDesc::default(),
16 gpu_ctx,
17 include_str!("shader.wgsl").to_string(),
18 )?;
19
20 //Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
21 let buffer = ShapeBatch::new()
22 .add_triangle([
23 vertex!(-0.5, -0.5, 0.0, Color::SILVER),
24 vertex!(0.5, -0.5, 0.0, Color::SILVER),
25 vertex!(0.0, 0.5, 0.0, Color::SILVER),
26 ])
27 .bake_buffers(gpu_ctx);
28
29 window.set_render_loop(move |gpu, _| {
30 //Create a render pass builder which we will use to define multiple render passes (In this case, only one).
31 let mut pass_builder = gpu.create_render_builder();
32
33 //Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
34 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
35 render_pass.render_shapes(gpu, &mut brush, &buffer);
36 render_pass.finish();
37
38 //Execute all drawing commands from all render passes and render into screen.
39 pass_builder.finish_render(gpu);
40 });
41
42 //Start program logic cycle.
43 app.start(());
44}
Sourcepub fn add_rect(&mut self, rect: &Rect, color: Color) -> &mut ShapeBatch
pub fn add_rect(&mut self, rect: &Rect, color: Color) -> &mut ShapeBatch
Add a square using a Rect as input
Sourcepub fn add_2d_square(
&mut self,
center: Vec3,
w: f32,
h: f32,
color: Color,
) -> &mut ShapeBatch
pub fn add_2d_square( &mut self, center: Vec3, w: f32, h: f32, color: Color, ) -> &mut ShapeBatch
Add a square on axis XY to the batch specifying the center, width, height and color.
Sourcepub fn add_square(
&mut self,
center: Vec3,
up: Vec3,
normal: Vec3,
w: f32,
h: f32,
color: Color,
) -> &mut ShapeBatch
pub fn add_square( &mut self, center: Vec3, up: Vec3, normal: Vec3, w: f32, h: f32, color: Color, ) -> &mut ShapeBatch
Add a square to the batch specifying the center, width, height and color.
Sourcepub fn add_cube(
&mut self,
center: Vec3,
orientation: Quat,
scale: Vec3,
color: Color,
) -> &mut ShapeBatch
pub fn add_cube( &mut self, center: Vec3, orientation: Quat, scale: Vec3, color: Color, ) -> &mut ShapeBatch
Add a cube to the batch specifying the center, orientation, size and color.
Examples found in repository?
examples/texture_cube/main.rs (lines 38-43)
6fn main() -> Result<(), Box<dyn Error>> {
7 //Create app and main window.
8 let mut app = Tridify::new();
9 let window = app.create_window()?;
10 let gpu_ctx = window.ctx();
11
12 //Load texture from path.
13 let texture = Texture::from_path(gpu_ctx, Path::new(r#"examples/texture_cube/texture.png"#));
14
15 //Sampler defines how the texture will be rendered in shapes.
16 let sampler = Sampler::new_default(gpu_ctx);
17
18 let camera = Camera::new(
19 Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
20 Projection::default(),
21 );
22 let mut camera_buf = camera.build_buffer(gpu_ctx);
23
24 //Create brush to draw the shapes.
25 let mut brush = Brush::from_source(
26 BrushDesc::default(),
27 gpu_ctx,
28 include_str!("shader.wgsl").to_string(),
29 )?;
30 //Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
31 //in the shader.
32 brush.bind(0, 0, camera_buf.clone());
33 brush.bind(1, 0, texture);
34 brush.bind(1, 1, sampler);
35
36 //Create and bake a shape batch with a cube in it.
37 let shape_buffer = ShapeBatch::new()
38 .add_cube(
39 Vec3::ZERO,
40 Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
41 Vec3::ONE * 5.,
42 Color::WHITE,
43 )
44 .bake_buffers(gpu_ctx);
45
46 //Setup the window render loop.
47 window.set_render_loop(move |gpu, frame_ctx| {
48 let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
49 let mvp = camera.build_camera_matrix() * model;
50
51 //Updating the gpu buffer will update all brushes binded as well.
52 camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
53
54 //Render frame as usual.
55 let mut pass_builder = gpu.create_render_builder();
56 let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
57 render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
58 render_pass.finish();
59 pass_builder.finish_render(gpu);
60 });
61
62 // Start program.
63 app.start(());
64}
Trait Implementations§
Source§impl Debug for ShapeBatch
impl Debug for ShapeBatch
Source§impl Default for ShapeBatch
impl Default for ShapeBatch
Source§fn default() -> ShapeBatch
fn default() -> ShapeBatch
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ShapeBatch
impl RefUnwindSafe for ShapeBatch
impl Send for ShapeBatch
impl Sync for ShapeBatch
impl Unpin for ShapeBatch
impl UnwindSafe for ShapeBatch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian()
.