Skip to content

C API: Hair and cloth sway

Long hair, skirts, dresses, robes, coat tails and earrings swing as the avatar moves, under the ctx's gravity and wind, off the body's capsules. Off by default, per avatar. What sways is read from each worn mesh; those vertices are re-skinned in a shared copy through palette joints the mesh never reads, and the copy draws with its own palette, listed after the props in sona_pose. Each struct starts with size = sizeof(struct) and is refused (SONA_E_ARGUMENT) when size is smaller; the _defaults functions set it. Avatar calls belong to the owner thread. The guide, with what counts as hanging, the solver and the cost, is Hair and cloth sway.

Struct Bytes Fields
sona_sway 60 size, parts, quality, stiffness, damping, wind_scale, min_pixels, reserved[8]
sona_sway_world 68 size, gravity[3], wind[3], gust, gust_seconds, reserved[8]
sona_sway_info 68 size, parts, meshes, chains, nodes, vertices, steps, resting, max_offset, reserved[8]

Parts: SONA_SWAY_HAIR (1), SONA_SWAY_CLOTH (2), SONA_SWAY_ACCESSORIES (4, earrings), SONA_SWAY_HEADWEAR (8, costume and hat pieces below the head), SONA_SWAY_DEFAULT (7), SONA_SWAY_ALL (15). Quality: SONA_SWAY_LOW (0, 30 steps a second), SONA_SWAY_MEDIUM (1, 60), SONA_SWAY_HIGH (2, 120).

sona_sway_defaults

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

Returns Nothing.

Example (also in Hair and cloth sway):

sona_sway o;
/* hair, cloth and earrings; medium quality */
sona_sway_defaults(&o);
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
/* a 4 m/s breeze along +X, gusting */
w.wind[0] = 4;
sona_sway_world_set(ctx, &w);

/* the chains step inside the poses of this update */
sona_update(ctx, dt, &view, NULL);

Parts SONA_SWAY_DEFAULT, medium quality, stiffness and damping 0.5, wind scale 1, 48 minimum pixels.

sona_avatar_sway

sona_result sona_avatar_sway(sona_avatar *a, const sona_sway *o);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
o
const sona_sway *
A sona_sway (required)

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

Example (also in Hair and cloth sway):

sona_sway o;
/* hair, cloth and earrings; medium quality */
sona_sway_defaults(&o);
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
/* a 4 m/s breeze along +X, gusting */
w.wind[0] = 4;
sona_sway_world_set(ctx, &w);

/* the chains step inside the poses of this update */
sona_update(ctx, dt, &view, NULL);

Turns sway on with these options, before or after READY (it applies to every later build too). A change of parts re-skins at once and bumps build_version. NULL turns it off: the items' own meshes again. SONA_E_ARGUMENT for a small size, no part or an unknown one, a quality past SONA_SWAY_HIGH, stiffness or damping outside 0..1, a wind scale outside 0..100 or negative minimum pixels; SONA_E_DESTROYED for an avatar being removed.

sona_avatar_sway_reset

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

Returns Nothing.

Example:

sona_avatar_sway_reset(avatar);

Back to the modelled shape, at rest, at the next pose. NULL is ignored.

sona_sway_world_defaults

void sona_sway_world_defaults(sona_sway_world *w);
Parameter and type Meaning
w
sona_sway_world *
The sona_sway_world to fill with the defaults (required)

Returns Nothing.

Example (also in Hair and cloth sway):

sona_sway o;
/* hair, cloth and earrings; medium quality */
sona_sway_defaults(&o);
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
/* a 4 m/s breeze along +X, gusting */
w.wind[0] = 4;
sona_sway_world_set(ctx, &w);

/* the chains step inside the poses of this update */
sona_update(ctx, dt, &view, NULL);

Gravity 0 -9.81 0, no wind, gust 0.35 with a period of 1.5 s.

sona_sway_world_set

sona_result sona_sway_world_set(sona_ctx *ctx, const sona_sway_world *w);
Parameter and type Meaning
ctx
sona_ctx *
The context (required)
w
const sona_sway_world *
A sona_sway_world (required)

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

Example (also in Hair and cloth sway):

sona_sway o;
/* hair, cloth and earrings; medium quality */
sona_sway_defaults(&o);
sona_avatar_sway(avatar, &o);            /* before or after READY */

sona_sway_world w;
sona_sway_world_defaults(&w);            /* gravity 0 -9.81 0, no wind */
/* a 4 m/s breeze along +X, gusting */
w.wind[0] = 4;
sona_sway_world_set(ctx, &w);

/* the chains step inside the poses of this update */
sona_update(ctx, dt, &view, NULL);

The gravity and wind every swaying avatar of the ctx feels from its next pose; resting avatars are posed again. NULL: the defaults. SONA_E_ARGUMENT for a small size, a gust outside 0..1, gust seconds outside 0.05..600, gravity over 1000 or wind over 100 on an axis, or a value that is not finite.

sona_avatar_sway_info

sona_result sona_avatar_sway_info(sona_avatar *a, sona_sway_info *out);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
out
sona_sway_info *
Receives the sona_sway_info (required)

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

Example:

sona_sway_info x_sway_info;
memset(&x_sway_info, 0, sizeof x_sway_info);
x_sway_info.size = sizeof x_sway_info;
sona_result result = sona_avatar_sway_info(avatar, &x_sway_info);
if (result != SONA_OK) printf("sona_avatar_sway_info: %s\n",
    sona_result_name(result));

What sways (parts, meshes, chains, nodes, vertices) and what the last pose did (steps, resting, max_offset in metres). With sway off: parts as found in what it wears, the rest 0. SONA_E_NOT_READY before READY.

sona_avatar_sway_mesh_parts

uint32_t sona_avatar_sway_mesh_parts(const sona_avatar *a, uint32_t mesh);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)
mesh
uint32_t
A mesh index of the pose data

Returns uint32_t.

Example:

uint32_t x_got = sona_avatar_sway_mesh_parts(avatar, 0);
printf("sona_avatar_sway_mesh_parts: %lld\n", (long long)x_got);

The SONA_SWAY_* kinds one of sona_pose's meshes sways with; 0 for one that does not, a bad index, or an avatar with sway off or not READY.

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