Skip to content

C API: Avatars

An avatar moves through SONA_AVATAR_QUEUED, LOADING, READY, and ends in FAILED or REMOVED.

sona_placement holds position[3] (metres, default 0, 0, 0), turn_deg (degrees about Y; 0 faces -Z) and scale (default 1).

sona_add_options:

Field Meaning
place Where it stands
clip A clip key, a raw clip id, "prop", "idle" or "still"; NULL: "idle"
loop Default 1
speed Default 1
start_at Seconds; negative means random, to desynchronise a crowd
visible Default 1
priority Higher builds sooner; default 0

sona_play_options holds loop (default 1), speed (default 1) and from (seconds into the clip, default 0).

sona_add_defaults

void sona_add_defaults(sona_add_options *o);
Parameter and type Meaning
o
sona_add_options *
The sona_add_options to fill with the defaults (required)

Returns Nothing.

Example (also in Crowds: level of detail and sprites):

/* 10,000 avatars in rows round a pitch, facing in */
sona_lod_set(ctx, NULL);
for (i = 0; i < 10000; i++) {
  sona_add_options o;
  sona_add_defaults(&o);
  seat(i, o.place.position, &o.place.turn_deg);   /* your layout */
  /* random start: a crowd out of step */
  o.start_at = -1;
  sona_avatar_add_base64(ctx, looks[i % 12], &o, &av[i]);
}

Fills *o with the defaults above.

sona_avatar_add

sona_result sona_avatar_add(sona_ctx *ctx,
                            const uint8_t manifest[SONA_MANIFEST_BYTES],
                            const sona_add_options *opts,
                            sona_avatar **out);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
manifest
const uint8_t [SONA_MANIFEST_BYTES]
The manifest's 1,000 bytes (required)
opts
const sona_add_options *
The options, a sona_add_options (required)
out
sona_avatar **
Receives the new avatar (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_LIMIT, SONA_E_MEMORY, SONA_E_MANIFEST (what each means).

Example (also in Untrusted files, long sessions and speed):

sona_manifest m;
/* any 1,000 bytes from a player */
if (sona_manifest_parse(manifest, &m) != SONA_OK)
  puts("not an avatar");                            /* SONA_E_MANIFEST */
else
  /* its items load through your sources */
  sona_avatar_add(ctx, manifest, NULL, &avatar);

Queues an avatar and returns at once. A bad manifest does not fail here: the avatar moves to FAILED with SONA_E_MANIFEST, as the web's add() never throws for one. It fails at once only with SONA_E_LIMIT (over max_avatars) or SONA_E_ARGUMENT.

sona_avatar_add_base64

sona_result sona_avatar_add_base64(sona_ctx *ctx, const char *b64,
                                   const sona_add_options *opts,
                                   sona_avatar **out);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
b64
const char *
The manifest as base64 text, at most 4096 characters (required)
opts
const sona_add_options *
The options, a sona_add_options (required)
out
sona_avatar **
Receives the new avatar (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_LIMIT, SONA_E_MEMORY (what each means).

Example (also in Getting started):

sona_avatar *avatar = NULL;
/* b64: the manifest; returns at once */
sona_avatar_add_base64(ctx, b64, NULL, &avatar);
/* remembered until it is ready */
sona_avatar_play(avatar, "GenericWave", NULL);

The same, from base64 text of at most 4096 characters, as the web.

sona_avatar_state_of

sona_avatar_state sona_avatar_state_of(const sona_avatar *a);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)

Returns sona_avatar_state.

Example (also in VR and XR bodies (sona_xr)):

sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
/* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.flags = SONA_XR_HAS_HEAD;
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr,
    avatar);
sona_update(ctx, dt, &view, NULL);

The avatar's state.

sona_avatar_id

uint32_t sona_avatar_id(const sona_avatar *a);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)

Returns uint32_t.

Example (also in Lip sync: a microphone or any audio drives the mouth):

sona_voice_mouth v[64];
for (i = 0; i < n; i++) sona_voice_mouth_init(&v[i],
    sona_avatar_id(avatars[i]));
/* each frame: */
uint32_t changed = sona_voice_mouths(avatars, v, levels, n, dt);

A number unique within the ctx.

sona_avatar_play

sona_result sona_avatar_play(sona_avatar *a, const char *clip,
                             const sona_play_options *opts);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
clip
const char *
A clip key or id (required)
opts
const sona_play_options *
The options, a sona_play_options (NULL: the defaults)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED (what each means).

Example (also in Getting started):

sona_avatar *avatar = NULL;
/* b64: the manifest; returns at once */
sona_avatar_add_base64(ctx, b64, NULL, &avatar);
/* remembered until it is ready */
sona_avatar_play(avatar, "GenericWave", NULL);

Plays a clip. Called before READY, it is remembered and applied when the avatar appears. NULL opts takes the defaults. Since 1.1 it also clears the clip queue, and returns SONA_E_ARGUMENT for an item preview.

sona_avatar_stop

void sona_avatar_stop(sona_avatar *a);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)

Returns Nothing.

Example (also in 3D print export):

/* no loop, speed 1, from 1.25 s */
sona_play_options pl = { 0, 1.0f, 1.25f };
sona_avatar_play(a, "GenericWave", &pl);
sona_update(ctx, 0, NULL, NULL);
sona_avatar_stop(a);
sona_update(ctx, 0, NULL, NULL);
sona_print_avatars(&a, 1, &opts, &print);

Holds the current pose.

sona_avatar_move

void sona_avatar_move(sona_avatar *a, const sona_placement *to,
                      float over_seconds);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
to
const sona_placement *
Where to stand, a sona_placement (required)
over_seconds
float
Seconds to ease over; 0: at once (0: at once)

Returns Nothing.

Example (also in Getting started):

sona_placement to = { { 1.0f, 0.0f, -2.0f }, 90.0f, 1.0f };
/* over half a second */
sona_avatar_move(avatar, &to, 0.5f);
sona_avatar_crossfade(avatar, "GenericWave", NULL, 0.2f);

Moves the avatar to a new placement over over_seconds.

sona_avatar_set_visible

void sona_avatar_set_visible(sona_avatar *a, int visible);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
visible
int
1 drawn, 0 hidden

Returns Nothing.

Example:

sona_avatar_set_visible(avatar, 1);

Shows or hides it. A hidden avatar is not drawn. It does nothing to an item preview, which sona_draw never draws.

sona_avatar_bounds

void sona_avatar_bounds(const sona_avatar *a, float center[3],
                        float *radius);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)
center
float [3]
Receives the centre of the bounding sphere, world space (required)
radius
float *
Receives the sphere's radius (required)

Returns Nothing.

A bounding sphere, in metres.

sona_avatar_remove

void sona_avatar_remove(sona_avatar *a);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)

Returns Nothing.

Example (also in Cookbook: recipes for game moments):

sona_avatar_remove(avatar);

Removes the avatar and frees what only it used. The handle must not be used afterwards.

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