raea/sprite

2D pictures and flat shapes, drawn by Bevy's 2D renderer inside a Canvas3D.

View as Markdown

<Sprite> is Bevy's sprite: a picture in the 2D world, or one cell of a sprite sheet or atlas. <Mesh2d> holds a flat shape and a ColorMaterial; the shapes are Bevy's 2D primitives (Circle, Rectangle, Ellipse, Annulus, RegularPolygon, Triangle2d and the rest). They draw inside a <Canvas3D>, in Bevy's 2D coordinates: the centre of the frame is the origin, +Y is up, and a unit is a pixel.

TSX
import { staticFile } from "raea";
import { Canvas3D } from "raea/camera";
import { Circle, ColorMaterial, Mesh2d, Sprite } from "raea/sprite";

const Shot = () => (
  <Canvas3D layered clearColor="rgb(43, 44, 47)">
    <Sprite src={staticFile("branding/bevy_bird_dark.png")} x={640} y={360} />
    <Mesh2d transform={{ translation: [-300, 0, 0] }}>
      <Circle radius={50} />
      <ColorMaterial color="#e8b04a" />
    </Mesh2d>
  </Canvas3D>
);
TS
import { Annulus, Capsule2d, Circle, CircularSector } from "raea/sprite"

raea/sprite exports 17 components, 1 class, 20 types. Bevy's counterpart: bevy_sprite. Source: sdk/raea/src/sprite.ts.

Components

Annulus

From Bevy's bevy_shape::Annulus: A primitive shape formed by the region between two circles, also known as a ring.

PropTypeWhat it does
inner_circle{ radius: number }Bevy: The inner circle of the annulus
outer_circle{ radius: number }Bevy: The outer circle of the annulus

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

Capsule2d

From Bevy's bevy_shape::Capsule2d: A 2D capsule primitive, also known as a stadium or pill shape.

PropTypeWhat it does
radiusnumberBevy: The radius of the capsule
half_lengthnumberBevy: Half the height of the capsule, excluding the semicircles

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

Circle

bevy_math's Circle (radius 0.5 by default, as Bevy's)

Bevy: bevy_shape::Circle.

PropTypeWhat it does
radiusnumberBevy: The radius of the circle

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

CircularSector

bevy_math's other 2D primitives, their fields as Bevy reflects them (each over Bevy's default)

Bevy: bevy_shape::CircularSector.

Takes ArcProps's props.

Used in: 2d/2d_shapes.

CircularSegment

From Bevy's bevy_shape::CircularSegment: A primitive representing a circular segment: the area enclosed by the arc of a circle and its chord (the line between its endpoints).

Takes ArcProps's props.

Used in: 2d/2d_shapes.

ColorMaterial

bevy_sprite_render's ColorMaterial: a colour (white by default), a texture it tints, and its AlphaMode2d

PropTypeWhat it does
colorstring | Srgba | LinearRgbawhite by default; it tints the texture, and its alpha is the material's (Bevy's Srgba or LinearRgba, or a hex)
texturestring | SampledImagea picture, by the mesh's UVs, or one with its own sampler ({ path, sampler }, Bevy's load_with_settings)
uv_transformAffine2the texture's UVs scaled, turned and moved (glam's Affine2: Affine2.fromScale([2, 3]))
alpha_modeAlphaMode2dBevy's AlphaMode2d, "Blend" by default

Used in: 2d/mesh2d_vertex_color_texture, 2d/mesh2d_alpha_mode, 2d/mesh2d, 2d/mesh2d_repeated_texture, 2d/2d_shapes.

ConvexPolygon

From Bevy's bevy_shape::ConvexPolygon: A convex polygon with N vertices.

Takes VerticesProps's props.

Ellipse

bevy_math's Ellipse (half_size [1, 0.5] by default)

Bevy: bevy_shape::Ellipse.

PropTypeWhat it does
half_sizeVec2Bevy: Half of the width and height of the ellipse.

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

Mesh2d

Bevy's Mesh2d with its Transform, a primitive and a ColorMaterial: in Bevy's 2D world.

Takes Mesh2dProps's props.

Used in: shader/shader_material_2d_bindless, shader/shader_material_2d, shader/pipeline_constants, 2d/mesh2d_vertex_color_texture, 2d/mesh2d_alpha_mode, 2d/mesh2d, 2d/mesh2d_repeated_texture, 2d/2d_shapes.

Polyline2d

From Bevy's bevy_shape::Polyline2d: A series of connected line segments in 2D space.

Takes VerticesProps's props.

Used in: 2d/2d_shapes.

Rectangle

bevy_math's Rectangle (half_size [0.5, 0.5] by default)

PropTypeWhat it does
half_sizeVec2half its width and height

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: shader/shader_material_2d_bindless, shader/shader_material_2d, shader/pipeline_constants, 2d/mesh2d_vertex_color_texture, 2d/mesh2d_alpha_mode, 2d/mesh2d, 2d/mesh2d_repeated_texture, 2d/2d_shapes.

RegularPolygon

bevy_math's RegularPolygon (circumradius 0.5, 6 sides by default)

Bevy: bevy_shape::RegularPolygon.

PropTypeWhat it does
circumcircle{ radius: number }Bevy: The circumcircle on which all vertices lie
sidesnumberBevy: The number of sides

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

Rhombus

From Bevy's bevy_shape::Rhombus: A rhombus primitive, also known as a diamond shape. A four sided polygon, centered on the origin, where opposite sides are parallel but without requiring right angles.

PropTypeWhat it does
half_diagonalsVec2Bevy: Size of the horizontal and vertical diagonals of the rhombus

Also takes the props of MeshAttributes (vertex_colors); RingProps (ring, inner).

Used in: 2d/2d_shapes.

Segment2d

From Bevy's bevy_shape::Segment2d: A line segment defined by two endpoints in 2D space.

Takes VerticesProps's props.

Used in: 2d/2d_shapes.

Shape2D

A flat shape in 2D, over the 3D, in one colour: Bevy's Mesh2d with a ColorMaterial (raea-sys-sprite). Put it inside the Canvas3D.

Bevy: bevy_sprite_render::mesh2d::color_material::ColorMaterial, bevy_sprite_render::mesh2d::material::MeshMaterial2d<bevy_sprite_render::mesh2d::color_material::ColorMaterial>, bevy_mesh::components::Mesh2d.

Takes Shape2DProps's props.

Used in: animation/color_animation, 2d/text2d.

Sprite

A picture in 2D, over the 3D: Bevy's Sprite, drawn as a quad with its sprite material, a sprite sheet's cells cut by a texture atlas (raea-sys-sprite). Put it inside the Canvas3D.

Bevy: bevy_sprite::sprite::Sprite, bevy_sprite_render::sprite_mesh::sprite_mesh_material::SpriteMeshMaterial, bevy_sprite_render::mesh2d::material::MeshMaterial2d<bevy_sprite_render::sprite_mesh::sprite_mesh_material::SpriteMeshMaterial>, bevy_sprite_render::sprite_mesh::sprite_extended_material::SpriteMaterial, bevy_image::texture_atlas::TextureAtlasLayout.

Takes SpriteProps's props.

Triangle2d

From Bevy's bevy_shape::Triangle2d: A triangle in 2D space

Takes VerticesProps's props.

Used in: 2d/2d_shapes.

Classes

TextureAtlasBuilder

Pictures packed into one atlas, as Bevy's TextureAtlasBuilder: padding([x, y]) px around each, addTexture in order, then build({ sampler }) (the atlas's own sampler, Bevy's default otherwise).

Bevy: bevy_image::texture_atlas_builder::TextureAtlasBuilder, bevy_image::texture_atlas::TextureAtlasSources.

MemberWhat it does
.padding(p: [number, number]): TextureAtlasBuilderWhole px of space around each picture, [x, y].
.addTexture(src: string): TextureAtlasBuilderAdds a picture, packed in the order added.
.build({ sampler }?: { sampler?: SamplerSpec | undefined; }): BuiltAtlasPacks the atlas, with its own sampler if given (Bevy's default otherwise).

Used in: 2d/texture_atlas.

Types

AlphaMode2d

bevy_sprite_render's AlphaMode2d

TS
type AlphaMode2d = "Opaque" | "Blend" | { Mask: number }

AnnulusProps

The props of Annulus, tabled there.

ArcProps

an arc: its radius and half its angle in radians (Bevy's CircularSector::new(r, a) gives half_angle a)

FieldTypeWhat it is
arc{ radius: number; half_angle: number }its radius and half its angle in radians
vertex_colors(LinearRgba | [number, number, number, number])[]one linear colour a vertex, in the mesh's vertex order (ATTRIBUTE_COLOR)
ringnumbera thickness: the shape made a ring that thick (to_ring)
innerRecord<string, unknown>the ring's inner shape, as the same shape's fields

AtlasSprite

What a takes to show a built atlas: the whole of it, or one of its pictures (atlasPick).

FieldTypeWhat it is
src (required)stringthe atlas picture
samplerSamplerSpechow it's sampled (the atlas's own sampler)
atlasPicknumberwhich of its pictures shows (the whole atlas when left out)

BuiltAtlas

A built atlas: image the whole atlas as a sprite's picture, sources.handle(texture) one picture of it (TextureAtlasSources::handle).

FieldTypeWhat it is
image (required)AtlasSpritethe whole atlas, as a sprite's picture
sources (required){ handle(texture: string): AtlasSprite }`handle(texture)`: one of the pictures packed into it, by its file

Capsule2dProps

The props of Capsule2d, tabled there.

CircleProps

The props of Circle, tabled there.

ColorMaterialProps

The props of ColorMaterial, tabled there.

EllipseProps

The props of Ellipse, tabled there.

Mesh2dProps

FieldTypeWhat it is
transformTransformValuewhere it stands: translation, rotation (a quaternion) and scale
visibility"Inherited" | "Visible" | "Hidden""Hidden" draws nothing; "Visible" and "Inherited" draw
childrenReactNodeone of bevy_math's 2D primitives (, ,...) and a , or your own
namestringthe entity's name, for picking, the inspector and world.*
onClick(e: Event3d) => voida press and release over the part
onDrag(e: Event3d) => voidthe part pressed is being dragged, each frame it moves
onDragEnd(e: Event3d) => voidthe drag of the part pressed ended
onDragEnter(e: Event3d) => voida dragged part came over this one (dragged names it)
onDragLeave(e: Event3d) => voida dragged part left this one
onDragOver(e: Event3d) => voida dragged part is over this one
onDragStart(e: Event3d) => voida drag began on the part pressed
onDrop(e: Event3d) => voida dragged part was dropped on this one
onPointerDown(e: Event3d) => voida button pressed over the part
onPointerMove(e: Event3d) => voidthe pointer moved over the part
onPointerUp(e: Event3d) => voida button released over the part
onPointerEnter(e: Event3d) => voidthe pointer came over the part (doesn't bubble)
onPointerLeave(e: Event3d) => voidthe pointer left the part (doesn't bubble)
onPointerOver(e: Event3d) => voidthe pointer came over the part (bubbles, as the DOM's)
onPointerOut(e: Event3d) => voidthe pointer left the part (bubbles)
onWheel(e: Event3d) => voidthe wheel turned over the part (deltaX, deltaY)
quatQuatBevy's rotation as it is, a quaternion [x, y, z, w] in Bevy's axes (y up, z toward the viewer), in place of rotation: a turn out of the frame's plane too, as a Sprite's Transform takes it (camera/2d_on_ui)
opacitynumber0 to 1 (1 by default)

MeshAttributes

a primitive's mesh attributes: its Mesh::ATTRIBUTE_COLOR, one linear colour per vertex (as mesh.insert_attribute)

FieldTypeWhat it is
vertex_colors(LinearRgba | [number, number, number, number])[]one linear colour a vertex, in the mesh's vertex order (ATTRIBUTE_COLOR)

Place2dProps

Where a 2D part is: composition px from the frame's top left, y down (as the page's), its centre there; and its pointer and drag handlers, called as a 3D part's are (2D parts are picked first, the top one by z).

FieldTypeWhat it is
namestringthe entity's name, for picking, the inspector and world.*
xnumberpx from the frame's left edge to the part's centre
ynumberpx from the frame's top edge to the part's centre, down positive
rotationnumberradians, clockwise (as CSS rotate)
quatQuatBevy's rotation as it is, a quaternion [x, y, z, w] in Bevy's axes (y up, z toward the viewer), in place of rotation: a turn out of the frame's plane too, as a Sprite's Transform takes it (camera/2d_on_ui)
scalenumber | [number, number]1 by default; [x, y] to stretch
znumberdraw order among the 2D parts: higher draws over lower (0 by default)
opacitynumber0 to 1 (1 by default)
onClick(e: Event3d) => voida press and release over the part
onPointerDown(e: Event3d) => voida button pressed over the part
onPointerUp(e: Event3d) => voida button released over the part
onPointerOver(e: Event3d) => voidthe pointer came over the part (bubbles, as the DOM's)
onPointerOut(e: Event3d) => voidthe pointer left the part (bubbles)
onPointerEnter(e: Event3d) => voidthe pointer came over the part (doesn't bubble)
onPointerLeave(e: Event3d) => voidthe pointer left the part (doesn't bubble)
onPointerMove(e: Event3d) => voidthe pointer moved over the part
onWheel(e: Event3d) => voidthe wheel turned over the part (deltaX, deltaY)
onDragStart(e: Event3d) => voida drag began on the part pressed
onDrag(e: Event3d) => voidthe part pressed is being dragged, each frame it moves
onDragEnd(e: Event3d) => voidthe drag of the part pressed ended
onDragEnter(e: Event3d) => voida dragged part came over this one (dragged names it)
onDragOver(e: Event3d) => voida dragged part is over this one
onDragLeave(e: Event3d) => voida dragged part left this one
onDrop(e: Event3d) => voida dragged part was dropped on this one

RectangleProps

The props of Rectangle, tabled there.

RegularPolygonProps

The props of RegularPolygon, tabled there.

RhombusProps

The props of Rhombus, tabled there.

RingProps

a 2D shape as Bevy's Ring: of it and inner (the same shape's fields), or its to_ring(ring, a thickness)

FieldTypeWhat it is
ringnumbera thickness: the shape made a ring that thick (to_ring)
innerRecord<string, unknown>the ring's inner shape, as the same shape's fields

Shape2DProps

FieldTypeWhat it is
shape"rect" | "circle" | "ellipse" | "triangle" | "polygon"a rect by default; a polygon has sides (5 by default)
size[number, number][width, height] in px ([100, 100] by default); a circle's and a polygon's diameter is the width
sidesnumbera polygon's number of sides (5)
colorstringa CSS colour (white by default); with a texture, it tints it
srgb[number, number, number]the colour as a Bevy Srgba's [r, g, b], unrounded, over color (a Mesh2d's ColorMaterial gives it)
texturestring | { path: string; sampler: SamplerSpec }a picture on it, mapped by the mesh's UVs (Bevy's ColorMaterial texture), or one with its own sampler (Bevy's load_with_settings: { path, sampler: { wrap: "repeat" } })
uv_transform{ scale: [number, number]; angle: number; translation: [number, number] }the texture's UVs scaled, turned and moved (ColorMaterial's uv_transform, glam's Affine2 as raea/transform's)
alpha_modeAlphaMode2dBevy's AlphaMode2d: "Blend" (Bevy's default), "Opaque", or { Mask: cutoff }
vertex_colors[number, number, number, number][]one linear [r, g, b, a] per vertex of the shape's mesh (Bevy's Mesh::ATTRIBUTE_COLOR): the material's colour and texture are multiplied by them. A rect has 4 (top right, top left, bottom left, bottom right), a triangle 3.
shaderRecord<string, unknown>its own Material2d in place of the ColorMaterial: the spec a in a Mesh2d makes; the shape's colour, texture and alpha_mode are then the material's to draw
primitiveRecord<string, unknown>one of Bevy's 2D primitives in place of shape, its reflected fields over Bevy's default: { primitive: "Capsule2d", radius: 25, half_length: 25 }; a 2D shape given inner (the same shape's fields) is Bevy's Ring, or given ring: thickness its to_ring (raea-sys-mesh primitive_mesh). A Mesh2d's primitive child makes it.
namestringthe entity's name, for picking, the inspector and world.*
xnumberpx from the frame's left edge to the part's centre
ynumberpx from the frame's top edge to the part's centre, down positive
rotationnumberradians, clockwise (as CSS rotate)
quatQuatBevy's rotation as it is, a quaternion [x, y, z, w] in Bevy's axes (y up, z toward the viewer), in place of rotation: a turn out of the frame's plane too, as a Sprite's Transform takes it (camera/2d_on_ui)
scalenumber | [number, number]1 by default; [x, y] to stretch
znumberdraw order among the 2D parts: higher draws over lower (0 by default)
opacitynumber0 to 1 (1 by default)
onClick(e: Event3d) => voida press and release over the part
onPointerDown(e: Event3d) => voida button pressed over the part
onPointerUp(e: Event3d) => voida button released over the part
onPointerOver(e: Event3d) => voidthe pointer came over the part (bubbles, as the DOM's)
onPointerOut(e: Event3d) => voidthe pointer left the part (bubbles)
onPointerEnter(e: Event3d) => voidthe pointer came over the part (doesn't bubble)
onPointerLeave(e: Event3d) => voidthe pointer left the part (doesn't bubble)
onPointerMove(e: Event3d) => voidthe pointer moved over the part
onWheel(e: Event3d) => voidthe wheel turned over the part (deltaX, deltaY)
onDragStart(e: Event3d) => voida drag began on the part pressed
onDrag(e: Event3d) => voidthe part pressed is being dragged, each frame it moves
onDragEnd(e: Event3d) => voidthe drag of the part pressed ended
onDragEnter(e: Event3d) => voida dragged part came over this one (dragged names it)
onDragOver(e: Event3d) => voida dragged part is over this one
onDragLeave(e: Event3d) => voida dragged part left this one
onDrop(e: Event3d) => voida dragged part was dropped on this one

SpriteImageMode

Bevy's SpriteImageMode, as its reflected value

Bevy: bevy_sprite::sprite::SpriteImageMode.

TS
type SpriteImageMode = | "Auto" | { Scale: "FillCenter" | "FillStart" | "FillEnd" | "FitCenter" | "FitStart" | "FitEnd" } | { Sliced: { border?: number | { min_inset: [number, number]; max_inset: [number, number] }; center_scale_mode?: SliceScaleMode; sides_scale_mode?: SliceScaleMode; max_corner_scale?: number } } | {…

SpriteProps

FieldTypeWhat it is
src (required)stringa picture in the media folder: staticFile("hero.png"), or a "render:NAME" image a camera or a draws (a frame may switch it to another by name)
widthnumbersize in px (the picture's, or one cell's, by default); one given, the other follows the picture's shape
heightnumberin px; given alone, the width follows the picture's shape
grid[number, number]a sprite sheet: the picture is [columns, rows] cells of equal size, read left to right, top to bottom
atlasPicknumberone picture of a built atlas (TextureAtlasBuilder's sources.handle gives it with its src): its index in the atlas's list
framenumberwith grid: which cell shows (0 is the top left); animate it to play the sheet: frame={Math.floor(f / 3)}
flipXbooleanmirrored left to right
flipYbooleanmirrored top to bottom
colorstringa tint multiplied over the picture (white by default)
tileXbooleanBevy's SpriteImageMode::Tiled: the picture repeats across the sprite's width (tileX) or height (tileY) instead of stretching to it, a tile every tileStretch x the picture's size (1 by default): a floor, a fence, a wall. Animate width and height to show more or fewer tiles
tileYbooleantile the picture down its height instead of stretching it
tileStretchnumbera tile every this many times the picture's size (1)
image_modeSpriteImageModeBevy's SpriteImageMode, as its reflected value: "Auto" (stretched to the size), { Scale: "FillCenter" } (and FillStart, FillEnd, FitCenter, FitStart, FitEnd: the picture kept to its shape, cropped or fitted), { Sliced: TextureSlicer } (nine-slice: border a number of px or { min_inset: [x, y], max_inset: [x, y] }; center_scale_mode and sides_scale_mode "Stretch" or { Tile: { stretch_value } }; max_corner_scale) or { Tiled: { tile_x, tile_y, stretch_value } }. Not with tileX or tileY
samplerSamplerSpecthe picture's sampler (Bevy's load_with_settings: { filter, mipmap, wrap, anisotropy }), Bevy's default (linear) otherwise; { filter: "nearest" } is pixel art as Bevy's ImagePlugin::default_nearest() draws it
childrenReactNodeyour own as its child: Bevy's SpriteMaterial, extending the sprite's own material (the shader reads the sprite's picture, tint and alpha with bevy_sprite_render::sprite_mesh::functions), its uniforms per frame
namestringthe entity's name, for picking, the inspector and world.*
xnumberpx from the frame's left edge to the part's centre
ynumberpx from the frame's top edge to the part's centre, down positive
rotationnumberradians, clockwise (as CSS rotate)
quatQuatBevy's rotation as it is, a quaternion [x, y, z, w] in Bevy's axes (y up, z toward the viewer), in place of rotation: a turn out of the frame's plane too, as a Sprite's Transform takes it (camera/2d_on_ui)
scalenumber | [number, number]1 by default; [x, y] to stretch
znumberdraw order among the 2D parts: higher draws over lower (0 by default)
opacitynumber0 to 1 (1 by default)
onClick(e: Event3d) => voida press and release over the part
onPointerDown(e: Event3d) => voida button pressed over the part
onPointerUp(e: Event3d) => voida button released over the part
onPointerOver(e: Event3d) => voidthe pointer came over the part (bubbles, as the DOM's)
onPointerOut(e: Event3d) => voidthe pointer left the part (bubbles)
onPointerEnter(e: Event3d) => voidthe pointer came over the part (doesn't bubble)
onPointerLeave(e: Event3d) => voidthe pointer left the part (doesn't bubble)
onPointerMove(e: Event3d) => voidthe pointer moved over the part
onWheel(e: Event3d) => voidthe wheel turned over the part (deltaX, deltaY)
onDragStart(e: Event3d) => voida drag began on the part pressed
onDrag(e: Event3d) => voidthe part pressed is being dragged, each frame it moves
onDragEnd(e: Event3d) => voidthe drag of the part pressed ended
onDragEnter(e: Event3d) => voida dragged part came over this one (dragged names it)
onDragOver(e: Event3d) => voida dragged part is over this one
onDragLeave(e: Event3d) => voida dragged part left this one
onDrop(e: Event3d) => voida dragged part was dropped on this one

VerticesProps

FieldTypeWhat it is
verticesVec2[]the outline's points, in order
vertex_colors(LinearRgba | [number, number, number, number])[]one linear colour a vertex, in the mesh's vertex order (ATTRIBUTE_COLOR)
ringnumbera thickness: the shape made a ring that thick (to_ring)
innerRecord<string, unknown>the ring's inner shape, as the same shape's fields