Skip to content

Item previews

A picture of one item, for wardrobes and item pickers. The item is worn alone on a grey stand-in body and the camera is framed by its slot: a hat shows the head, shoes show the feet, a ring shows the hand, a prop shows the prop.

How it works

  • A preview is an ordinary avatar of the ctx with a made-up manifest: nothing worn but the item, no hair, no face features, an average shape, skin 0xff9a9a9a (the web runtime's blank figure). The body is the one the item was made for (the id's third group: ...-0001-... male, ...-0002-... female, else male).
  • It builds through the same queue as every avatar. Decoded items, face sheets and GPU buffers are shared, so a grid of previews uploads the body once.
  • It is hidden. sona_draw and sona_snapshot never draw it, sona_avatar_set_visible has no effect on it and sona_camera_fit_crowd ignores it. sona_item_preview_draw and sona_item_preview_snapshot draw it alone.
  • Worn items stand in the still pose (Stand0, held at a time set to 5.383 s). A prop holds its fullest frame (the frame with the most of the prop showing) unless you ask it to play.

Framing

The framing follows the web's item renders:

  1. The item's category picks one of the 17 view presets of the original editor (the same table the web uses). The category comes from the id: prop, then costume (more than one of shirt, trousers and shoes), then gloves, then shirt, trousers, shoes, hat, hair, glasses, earrings, wristwear, ring. A shirt that also sets the wrist bit is framed as a shirt. sona_preview_options.slot overrides it.
  2. The preset gives the direction: its eye and target, hung off its joint, with the figure's turn applied to the camera. Ears, Ring and Wristwear can look from either side; the side whose camera is nearer the item wins, with 2 cm in favour of the usual side (the editor's presetCamera).
  3. The item's own triangles are skinned on the CPU and measured in that camera's frame. Triangles with no area (hidden parts scaled to a point) do not count. A pair seen from one side (two earrings, two wristbands) counts its near half only. A held prop leaves out a stray part: one cut off from the rest by an empty gap of a tenth of the prop's size that holds at most a fifth of its surface.
  4. The camera keeps the preset's direction, aims at the middle of that extent and backs off until the extent fits with margin around it (1.36 by default, the web's crop). Small items get a smallest frame, in metres across the picture's shorter side:
Preset Items Smallest frame
Head hats, glasses, eyebrows, facial hair 0.34 m (the whole head)
Hair hair 0.34 m
Ears earrings, ears 0.17 m
Eyes eyes, eye shadow 0.13 m
Mouth mouth 0.12 m
Chin, Facial features chin, facial features 0.143 m
Nose nose 0.245 m
Ring rings 0.256 m (the hand)
Wristwear wristwear 0.2 m
others shirts, trousers, shoes, costumes, gloves, props none

Face items (eyes, mouth, eyebrows, facial hair, eye shadow, facial features) and head shapes (chin, nose, ears) have no geometry of their own: they change the head. Their preview frames the head.

Types

sona_preview_options

Field Meaning Default
gender 1 male, 2 female; 0: the body the item was made for 0
slot the category mask to frame by (0x40 hat, 0x38 costume, 0x880 gloves, ...); 0: from the item 0
flags SONA_PREVIEW_PLAY: a prop plays its clip, framed for all of it. SONA_PREVIEW_COLOURS: wear the item in colours 0
skin 0xAARRGGBB; 0: 0xff9a9a9a 0
colours[3] 0xAARRGGBB primary, secondary, tertiary; used with SONA_PREVIEW_COLOURS 0
priority build order, as sona_add_options.priority 0
time props: seconds into the prop's clip to hold (or, with PLAY, to start at); negative: the fullest frame -1
margin room around the item: 1 touches the edges; 0 means 1.36 1.36

40 bytes, every field 4-byte aligned. Start from sona_preview_defaults.

Functions

All calls follow the rules every function follows: one owner thread per ctx, no blocking I/O.

Warning

The avatar pointer stays valid until sona_avatar_remove or sona_close. After either, the handle is gone: passing it to any call is undefined, as for any avatar.

A preview is a still picture. sona_avatar_play, sona_avatar_crossfade and sona_avatar_queue return SONA_E_ARGUMENT for a preview, and sona_avatar_set_visible does nothing. Joint overrides (sona_avatar_override) are not part of the measured frame: leave them off a preview. sona_avatar_move is fine, since the box and the camera follow the placement.

sona_preview_defaults

void sona_preview_defaults(sona_preview_options *o)

Fills the defaults above. NULL does nothing.

sona_item_preview_add

sona_result sona_item_preview_add(sona_ctx *ctx, const char *item_id, const sona_preview_options *opts, sona_avatar **out)

Queues a preview and returns at once. item_id is a lowercase or uppercase item id. NULL opts takes the defaults. The preview reaches READY (or FAILED) through sona_update and sends the usual SONA_EVENT_READY or SONA_EVENT_FAILED. A missing item file does not fail the preview: it reaches READY without the item, and sona_item_preview_bounds says SONA_E_NOT_FOUND.

  • SONA_E_ARGUMENT: NULL ctx or out, an id that does not parse, an item that is not worn, a face item or a head shape (clips, for example), gender above 2, a negative, infinite or NaN margin, an infinite or NaN time.
  • SONA_E_LIMIT: the ctx already holds max_avatars avatars. Previews count toward it.
  • SONA_E_MEMORY.

*out is NULL on any error.

sona_item_preview_bounds

sona_result sona_item_preview_bounds(sona_avatar *a, float lo[3], float hi[3])

The item's box in world metres: the corners of the measured extent (step 3 above) with the preview's placement applied. For a prop that plays, it covers the whole clip, sampled at 24 times. Measured on the first call to any of _bounds, _view, _draw or _snapshot after READY, then kept.

  • SONA_E_NOT_READY before READY. SONA_E_ARGUMENT for NULL arguments or an avatar that is not a preview. SONA_E_NOT_FOUND when no triangle of the item was drawn.

sona_item_preview_view

sona_result sona_item_preview_view(sona_avatar *a, uint32_t width, uint32_t height, sona_view *out)

The framed camera for a picture of width x height pixels (0 counts as 1). Every field of *out is set: fov_deg 28.65, near_z 0.05 (closer for a very near camera), far_z the item's distance plus 10 m, width and height as given. The aspect changes the distance, not the direction. An item that drew nothing gets its preset's own camera and SONA_OK. Errors as sona_item_preview_bounds, without NOT_FOUND.

sona_item_preview_draw

sona_result sona_item_preview_draw(sona_renderer *r, sona_avatar *a, const sona_view *view, const sona_target *target, const sona_draw_options *opts)

sona_draw with only this preview in the frame, into the host's target. NULL view: the framed view for the target's size. A playing prop is posed for the current frame before it is drawn. One frame, with the same frames_in_flight and host-state rules as sona_draw; the renderer must belong to the preview's ctx (SONA_E_ARGUMENT otherwise). Also returns what sona_draw returns, and the errors of sona_item_preview_view.

sona_item_preview_snapshot

sona_result sona_item_preview_snapshot(sona_renderer *r, sona_avatar *a, uint32_t width, uint32_t height, const sona_draw_options *opts, uint8_t *rgba_out)

sona_snapshot of this preview alone, framed for width x height: blocks until the GPU finishes, writes width * height * 4 bytes of straight RGBA8, row 0 at the top. The credit is stamped unless SONA_DRAW_NO_CREDIT; a picker usually passes it and shows the credit once elsewhere in the app, as the licence still requires.

Thumbnails from the gate (1.2)

For a grid, ask the gate for its own picture of the item before drawing a preview. sona_item_thumbnail(ctx, id, fn, user) queues it and returns at once; fn runs once, from a later sona_update, with the PNG (about 300 px square, a few KB): the picture 360sona.com's wardrobe shows. Nothing is decoded or drawn, a grid's tiles go 120 to a request, and the gate meters thumbnails apart from items, so scrolling a wardrobe never spends the budget your avatars need. SONA_E_NOT_FOUND means the gate has no picture of that item (a custom item, say): draw a preview for it instead. The picture for an id never changes, so keep it on disk and ask once per machine.

static void got_thumb(void *user, const char *id, sona_result r,
    const void *png, size_t len) {
  /* yours: decode, upload, write to your cache */
  if (r == SONA_OK) save_and_show(id, png, len);
  /* draw it with sona_item_preview_add */
  else if (r == SONA_E_NOT_FOUND) queue_preview(id);
}
/* for each tile on screen, and a screen or two ahead, that is not in
   your cache: */
sona_item_thumbnail(ctx, "00000008-0003-a112-c506-974d58411202", got_thumb,
                    NULL);

It needs the ctx's gate source (SONA_E_UNSUPPORTED without one, and in the WASM build, where the bridge has the web's own thumbnails). SDK Studio's Wardrobe works this way: thumbnails first, a cache that outlives SDK versions, and a preview only for items the gate has no picture of.

Cost

preview_grid_vk on an RTX 4090 machine, Vulkan, cold start, the median of three runs:

preview_grid_vk Result
Previews (every slot kind) 30
All READY after (ms) 26
sona_item_preview_snapshot at 256 x 256, ms on average 1.2
The slowest snapshot, ms (the first: pipeline warm-up) 16.4
GPU MB, all previews 6.9
CPU MB, all previews (sona_stats) 23.2

Previews of the same item share their GPU buffers.

  • A preview's face sheet is 128 x 128 (256 for a face item), and previews do not count toward the automatic face size of the scene's avatars. sona_config.face_size overrides both.

sona_stats counts previews in avatars and ready; draw_calls reports the last draw, preview or not.

Example

#include "sona.h"
#include <stdlib.h>

/* One 256 x 256 picture of a hat. ctx is open and r is a renderer of it. */
uint8_t *hat_picture(sona_ctx *ctx, sona_renderer *r) {
  sona_preview_options o;
  sona_draw_options d = { { 0, 0, 0, 0 }, SONA_DRAW_NO_CREDIT,
      SONA_CORNER_BOTTOM_RIGHT };
  sona_avatar *hat;
  sona_avatar_state s;
  uint8_t *rgba;
  sona_preview_defaults(&o);
  if (sona_item_preview_add(ctx, "00000040-00e4-0001-c1c8-f109a19cb2e0", &o,
      &hat) != SONA_OK) return NULL;
  do {
    sona_update(ctx, 1.0 / 60, NULL, NULL);
    s = sona_avatar_state_of(hat);
  } while (s == SONA_AVATAR_QUEUED || s == SONA_AVATAR_LOADING);
  rgba = malloc(256 * 256 * 4);
  if (s != SONA_AVATAR_READY || !rgba || sona_item_preview_snapshot(r, hat,
      256, 256, &d, rgba) != SONA_OK) {
    free(rgba);
    rgba = NULL;
  }
  sona_avatar_remove(hat);
  return rgba;
}
#include <vector>
#include "sona.hpp"

// One 256 x 256 picture of a hat. ctx is open and r is a renderer of it.
std::vector<uint8_t> hat_picture(sona::Context &ctx, sona::Renderer &r) {
  std::vector<uint8_t> rgba;
  sona::Avatar hat = ctx.add_item_preview("00000040-00e4-0001-c1c8-f109a19cb2e0");
  while (hat.state() == SONA_AVATAR_QUEUED ||
      hat.state() == SONA_AVATAR_LOADING) ctx.update(1.0 / 60);
  const sona_draw_options d = { { 0, 0, 0, 0 }, SONA_DRAW_NO_CREDIT,
      SONA_CORNER_BOTTOM_RIGHT };
  if (hat.ready()) r.preview_snapshot(hat, 256, 256, rgba, &d);
  hat.remove();
  return rgba;
}
// One 256 x 256 picture of a hat. ctx is open and r is a renderer of it.
static byte[] HatPicture(SonaContext ctx, SonaRenderer r)
{
    var hat = ctx.AddItemPreview("00000040-00e4-0001-c1c8-f109a19cb2e0");
    while (hat.State == SonaAvatarState.Queued ||
        hat.State == SonaAvatarState.Loading)
        ctx.Update(1.0 / 60);
    var rgba = hat.State == SonaAvatarState.Ready ? r.SnapshotPreview(hat,
        256, 256, SonaDrawFlags.NoCredit) : null;
    hat.Remove();
    return rgba;
}
// One 256 x 256 picture of a hat. ctx is open and r is a renderer of it.
fn hat_picture<'c>(ctx: &'c Context, r: &mut Renderer<'c>) -> Result<Option<Vec<u8>>> {
    let mut hat = ctx.add_item_preview("00000040-00e4-0001-c1c8-f109a19cb2e0", &preview_defaults())?;
    while matches!(hat.state(),
        AvatarState::Queued | AvatarState::Loading) {
        ctx.frame()?.update(1.0 / 60.0, None)?;
    }
    if hat.state() != AvatarState::Ready {
        return Ok(None);
    }
    let opts = DrawOptions { flags: sona_sys::SONA_DRAW_NO_CREDIT,
        ..Default::default() };
    // the hat goes when it is dropped
    r.preview_snapshot(&mut hat, 256, 256, Some(&opts)).map(Some)
}
def hat_picture(ctx):
    """One 256 x 256 picture of a hat, drawn with sona.Headless."""
    hat = ctx.add_item_preview("00000040-00e4-0001-c1c8-f109a19cb2e0")
    while not hat.settled:
        ctx.update(1 / 60)
    rgba = None
    if hat.ready:
        with sona.Headless(ctx) as h:
            rgba = h.render(hat, width=256, height=256)
    hat.remove()
    return rgba

A host that shows many previews adds them all first and waits once: they build together and share the body. samples/c/preview_grid_vk.c draws a 30-cell grid that way and writes it to a PNG.

This page as Markdown · All the docs as one file (llms-full.txt)