Struct tridify_rs::ShapeBatch
source · 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)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
pub fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
//Stores WGPU context and devices. Must be used in most GPU functions.
let gpu_ctx = window.ctx();
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
let buffer = ShapeBatch::new()
.add_triangle([
vertex!(-0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.0, 0.5, 0.0, Color::SILVER),
])
.bake_buffers(gpu_ctx);
window.set_render_loop(move |gpu, _| {
//Create a render pass builder which we will use to define multiple render passes (In this case, only one).
let mut pass_builder = gpu.create_render_builder();
//Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &buffer);
render_pass.finish();
//Execute all drawing commands from all render passes and render into screen.
pass_builder.finish_render(gpu);
});
//Start program logic cycle.
app.start(());
}
More examples
examples/texture_cube/main.rs (line 40)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
let gpu_ctx = window.ctx();
//Load texture from path.
let texture = Texture::from_path(
gpu_ctx,
Path::new(r#"D:\Development\Rust Crates\LDrawy\examples\draw_cube\texture.png"#),
);
//Sampler defines how the texture will be rendered in shapes.
let sampler = Sampler::new_default(gpu_ctx);
let camera = Camera::new(
Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
Projection::default(),
);
let mut camera_buf = camera.build_buffer(gpu_ctx);
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
//in the shader.
brush.bind(0, 0, camera_buf.clone());
brush.bind(1, 0, texture);
brush.bind(1, 1, sampler);
//Create and bake a shape batch with a cube in it.
let shape_buffer = ShapeBatch::new()
.add_cube(
Vec3::ZERO,
Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
Vec3::ONE * 5.,
Color::WHITE,
)
.bake_buffers(gpu_ctx);
//Setup the window render loop.
window.set_render_loop(move |gpu, frame_ctx| {
let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
let mvp = camera.build_camera_matrix() * model;
//Updating the gpu buffer will update all brushes binded as well.
camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
//Render frame as usual.
let mut pass_builder = gpu.create_render_builder();
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
render_pass.finish();
pass_builder.finish_render(gpu);
});
// Start program.
app.start(());
}
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)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
pub fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
//Stores WGPU context and devices. Must be used in most GPU functions.
let gpu_ctx = window.ctx();
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
let buffer = ShapeBatch::new()
.add_triangle([
vertex!(-0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.0, 0.5, 0.0, Color::SILVER),
])
.bake_buffers(gpu_ctx);
window.set_render_loop(move |gpu, _| {
//Create a render pass builder which we will use to define multiple render passes (In this case, only one).
let mut pass_builder = gpu.create_render_builder();
//Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &buffer);
render_pass.finish();
//Execute all drawing commands from all render passes and render into screen.
pass_builder.finish_render(gpu);
});
//Start program logic cycle.
app.start(());
}
More examples
examples/texture_cube/main.rs (line 47)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
let gpu_ctx = window.ctx();
//Load texture from path.
let texture = Texture::from_path(
gpu_ctx,
Path::new(r#"D:\Development\Rust Crates\LDrawy\examples\draw_cube\texture.png"#),
);
//Sampler defines how the texture will be rendered in shapes.
let sampler = Sampler::new_default(gpu_ctx);
let camera = Camera::new(
Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
Projection::default(),
);
let mut camera_buf = camera.build_buffer(gpu_ctx);
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
//in the shader.
brush.bind(0, 0, camera_buf.clone());
brush.bind(1, 0, texture);
brush.bind(1, 1, sampler);
//Create and bake a shape batch with a cube in it.
let shape_buffer = ShapeBatch::new()
.add_cube(
Vec3::ZERO,
Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
Vec3::ONE * 5.,
Color::WHITE,
)
.bake_buffers(gpu_ctx);
//Setup the window render loop.
window.set_render_loop(move |gpu, frame_ctx| {
let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
let mvp = camera.build_camera_matrix() * model;
//Updating the gpu buffer will update all brushes binded as well.
camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
//Render frame as usual.
let mut pass_builder = gpu.create_render_builder();
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
render_pass.finish();
pass_builder.finish_render(gpu);
});
// Start program.
app.start(());
}
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)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
pub fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
//Stores WGPU context and devices. Must be used in most GPU functions.
let gpu_ctx = window.ctx();
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Create a shape batch, add a triangle to it and create a GPU buffer with mesh data.
let buffer = ShapeBatch::new()
.add_triangle([
vertex!(-0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.5, -0.5, 0.0, Color::SILVER),
vertex!(0.0, 0.5, 0.0, Color::SILVER),
])
.bake_buffers(gpu_ctx);
window.set_render_loop(move |gpu, _| {
//Create a render pass builder which we will use to define multiple render passes (In this case, only one).
let mut pass_builder = gpu.create_render_builder();
//Build a render pass which will take care of the brush and shapes to draw them and binding it with the GPU.
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &buffer);
render_pass.finish();
//Execute all drawing commands from all render passes and render into screen.
pass_builder.finish_render(gpu);
});
//Start program logic cycle.
app.start(());
}
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 41-46)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn main() -> Result<(), Box<dyn Error>> {
//Create app and main window.
let mut app = Tridify::new();
let window = app.create_window()?;
let gpu_ctx = window.ctx();
//Load texture from path.
let texture = Texture::from_path(
gpu_ctx,
Path::new(r#"D:\Development\Rust Crates\LDrawy\examples\draw_cube\texture.png"#),
);
//Sampler defines how the texture will be rendered in shapes.
let sampler = Sampler::new_default(gpu_ctx);
let camera = Camera::new(
Transform::from_look_at(Vec3::NEG_Z * 10.0 + Vec3::Y * 10.0, Vec3::ZERO, Vec3::Y),
Projection::default(),
);
let mut camera_buf = camera.build_buffer(gpu_ctx);
//Create brush to draw the shapes.
let mut brush = Brush::from_source(
BrushDesc::default(),
gpu_ctx,
include_str!("shader.wgsl").to_string(),
)?;
//Bind camera, sampler and texture to the brush. Make sure group_index and loc_index are the same as
//in the shader.
brush.bind(0, 0, camera_buf.clone());
brush.bind(1, 0, texture);
brush.bind(1, 1, sampler);
//Create and bake a shape batch with a cube in it.
let shape_buffer = ShapeBatch::new()
.add_cube(
Vec3::ZERO,
Quat::from_rotation_x(35.) * Quat::from_rotation_y(35.),
Vec3::ONE * 5.,
Color::WHITE,
)
.bake_buffers(gpu_ctx);
//Setup the window render loop.
window.set_render_loop(move |gpu, frame_ctx| {
let model = Mat4::from_rotation_y(frame_ctx.elapsed_time as f32);
let mvp = camera.build_camera_matrix() * model;
//Updating the gpu buffer will update all brushes binded as well.
camera_buf.write(gpu, bytemuck::cast_slice(&mvp.to_cols_array()));
//Render frame as usual.
let mut pass_builder = gpu.create_render_builder();
let mut render_pass = pass_builder.build_render_pass(RenderOptions::default());
render_pass.render_shapes(gpu, &mut brush, &shape_buffer);
render_pass.finish();
pass_builder.finish_render(gpu);
});
// Start program.
app.start(());
}
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 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
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,
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()
.