Rectangle node
Rectangle is the simplest "2D" node, it can be used to create "2D" graphics. 2D is in quotes here because the node is actually a 3D node, like everything else in the engine. Here is an example scene made with the rectangle nodes and an orthographic camera:
As you can see it is a good basis for 2D games.
How to create
Use the RectangleBuilder to create Rectangle nodes:
#![allow(unused)] fn main() { fn create_rect(graph: &mut Graph, resource_manager: ResourceManager) -> Handle<Node> { let mut material = Material::standard_2d(); material .set_texture( &"diffuseTexture".into(), Some(resource_manager.request::<Texture>("path/to/your_texture.jpg")), ) .unwrap(); // Material resources can be shared across multiple rectangles (via simple `clone`). // This significantly improves performance if you have multiple rectangles with the // same material. let material_resource = MaterialResource::new_ok(ResourceKind::Embedded, material); RectangleBuilder::new( BaseBuilder::new().with_local_transform( TransformBuilder::new() // Size of the rectangle is defined only by scale. .with_local_scale(Vector3::new(0.4, 0.2, 1.0)) .build(), ), ) .with_color(Color::RED) .with_material(material_resource) .build(graph) } }
Specifying image portion for rendering
By default, Rectangle node uses entire image for rendering, but for some applications it is not enough. For example,
you may want to use sprite sheets to animate your 2D entities. In this case you need to be able to use only portion
of an image. It is possible to do by using set_uv_rect
method of the Rectangle node. Here's an example of setting
right-top quarter of an image to be used by a Rectangle node:
#![allow(unused)] fn main() { fn set_2nd_quarter_image_portion(rectangle: &mut Rectangle) { rectangle.set_uv_rect(Rect::new( 0.5, // Offset by 50% to the right 0.0, // No need to offset to bottom. 0.5, // Use half (50%) of width and height 0.5, )); } }
Keep in mind that every part of uv rectangle is proportional. For example 0.5 means 50%, 1.5 = 150% and so on. If width or height is exceeding 1.0 and the texture being used is set to Wrapping mode at respective axis, the image will tile across axes.
Animation
See Sprite Animation chapter for more info.
Performance
Rectangles use specialized renderer that is heavily optimized to render tons of rectangles at once, so you can use rectangles almost for everything in 2D games.