Skip to content

Sprite

Billboarded quad that always faces the active camera.

Sprite is a flat, unit-sized quad oriented toward the camera at all times. It is usually textured (often with transparency) and exposes the minimal Renderable interface so it can be submitted to the renderer.

Common use cases include particles, labels, icons, and world-space markers.

cpp
auto MyNode::OnAttached(SharedContextPointer context) -> void override {
  context->texture_loader->LoadAsync(
    "assets/sprite.tex",
    [this](auto result) {
      if (result) {
        auto mat = vglx::SpriteMaterial::Create();
        mat->albedo_map = result.value();
        auto sprite = Sprite::Create(mat);
        sprite->SetScale(0.5f);
        Add(sprite);
      } else {
        std::println(stderr, "{}", result.error());
      }
    }
  );
}

Construction

Constructors


Sprite()

Constructs a sprite with an optional material.

If material is null, a new instance of SpriteMaterial will be created.

cpp
Sprite::Sprite(std::shared_ptr<SpriteMaterial> material);
ParameterDescription
materialMaterial used to render the sprite (may be null).

Factories preferred


Sprite::Create() auto

Creates a shared pointer to a Sprite object with material.

cpp
static auto Sprite::Create(std::shared_ptr<SpriteMaterial> material=nullptr);
ParameterDescription
materialShared pointer to a sprite material.

Properties

anchor Vector2

Normalized anchor point inside the sprite. Defines the pivot used for placement and rotation of the sprite.

(0.0f, 0.0f) = lower-left corner of the sprite.(0.5f, 0.5f) = center of the sprite (default).(1.0f, 1.0f) = upper-right corner of the sprite.

The sprite's world-space position corresponds to this anchor point. Rotation is applied around this pivot.

cpp
Vector2 anchor {Vector2 {0.5f, 0.5f}};

rotation float

View-space rotation angle in radians applied to the sprite.

cpp
float rotation {0.0f};

Functions

GetMaterial() std::shared_ptr<Material>

Returns the material associated with the sprite.

cpp
std::shared_ptr<Material> Sprite::GetMaterial() override;

GetNodeType() Node::Type

Returns node type.

cpp
Type vglx::Sprite::GetNodeType() const override;

SetMaterial() auto

Sets the material used to render the sprite.

cpp
auto Sprite::SetMaterial(std::shared_ptr<SpriteMaterial> material);
ParameterDescription
materialShared pointer to a sprite material.

Released under the MIT License.