Skip to content

Drawing avatars with your own renderer

The SDK can hand you each avatar posed, as data, for a renderer of your own to draw in its own pass: the meshes, the materials, the textures and the joint palette, refreshed by every sona_update. The Unity, Godot and Unreal packages draw this way. When the SDK's own renderer draws the avatars (sona_draw), nothing here is needed.

You need Where it is
The posed avatar sona_pose(avatar, &pose): a sona_pose_data, valid until the next sona_update
Its meshes pose->meshes: 76-byte vertices, a uint16_t triangle list, a material, a pass (opaque or transparent), a palette
The pose pose->palette: 72 joints, three float4 rows each; a held prop's meshes use pose->props[i].palette
Where it stands pose->world: position, turn and scale as one row-major matrix
Its look pose->materials and pose->textures; the shading constants from sona_shading_get; the SDK's own shaders from sona_shader_source
The credit The badge's pixels from sona_credit_get, drawn by you (credit)

Each frame

After sona_update, read the pose and draw its meshes. A mesh's key stays the same while its geometry lives, and avatars wearing the same item share it, so upload a mesh once by its key and draw it every frame; only the palette changes from frame to frame. A held prop's meshes are drawn only while its clip plays.

const sona_pose_data *pose = NULL;
if (sona_pose(avatar, &pose) == SONA_OK) {
  for (uint32_t i = 0; i < pose->mesh_count; i++) {
    const sona_mesh *m = &pose->meshes[i];
    const float *palette = pose->palette;
    if (m->palette >= 0) {
      /* a held prop's mesh: its own palette, and only while its clip
         plays */
      if (!pose->props[m->palette].playing) continue;
      palette = pose->props[m->palette].palette;
    }
    my_upload_once(m->key, m->vertices,
        m->vertex_count * SONA_VERTEX_STRIDE,
                   m->indices, m->index_count);
    my_draw(m->key, &pose->materials[m->material],
            m->pass == SONA_PASS_TRANSPARENT, pose->world, palette);
  }
}
sona::Pose pose = avatar.pose();
for (const sona_mesh &m : pose.meshes()) {
  const float *palette = pose.palette().data();
  if (m.palette >= 0) {
    // a held prop's mesh: its own palette, and only while its clip plays
    if (!pose.props()[m.palette].playing) continue;
    palette = pose.props()[m.palette].palette;
  }
  my_upload_once(m.key, m.vertices, m.vertex_count * SONA_VERTEX_STRIDE,
                 m.indices, m.index_count);
  my_draw(m.key, &pose.materials()[m.material],
          m.pass == SONA_PASS_TRANSPARENT, pose.world().data(), palette);
}
if (avatar.TryGetPose(out SonaPose pose))
{
    for (int i = 0; i < pose.MeshCount; i++)
    {
        SonaMeshData m = pose.Mesh(i);
        ReadOnlySpan<float> palette = pose.Palette;
        if (m.Palette >= 0)
        {
            // a held prop's mesh: its own palette, and only while its clip
            // plays
            if (!pose.Prop(m.Palette).Playing) continue;
            palette = pose.Prop(m.Palette).Palette;
        }
        MyUploadOnce(m.Key, m.Vertices, m.Indices);
        MyDraw(m.Key, pose.Material(m.Material),
            m.Pass == SonaPass.Transparent, pose, palette);
    }
}
let frame = ctx.frame()?;
let pose = frame.pose(avatar)?;
for m in pose.meshes() {
    let palette = match m.palette() {
        // a held prop's mesh: its own palette, and only while its clip
        // plays
        Some(p) if !pose.props()[p].playing() => continue,
        Some(p) => pose.props()[p].palette(),
        None => pose.palette(),
    };
    my_upload_once(m.key(), m.vertices(), m.indices());
    let material = &pose.materials()[m.material() as usize];
    my_draw(m.key(), material, m.pass() == Pass::Transparent, pose.world(),
        palette);
}
pose = avatar.pose()
for m in pose.meshes:
    palette = pose.palette
    if m.palette is not None:
        # a held prop's mesh: its own palette, and only while its clip plays
        if not pose.props[m.palette].playing:
            continue
        palette = pose.props[m.palette].palette
    my_upload_once(m.key, m.vertices, m.indices)
    my_draw(m.key, pose.materials[m.material], m.transparent, pose.world,
        palette)

The vertex and the skinning

A vertex is 76 bytes in the machine's byte order. Give your vertex layout these attributes:

Offset Format Attribute
+0 3 floats Position, in the bind pose, in avatar space
+12 4 unsigned bytes, not normalised Joints: each is a row of the palette (the joint's index times 3)
+16 4 unsigned bytes, normalised Weights: they add up to 1
+20 4 unsigned bytes, normalised Normal, as n * 0.5 + 0.5
+24 4 unsigned bytes, normalised Vertex colour: baked occlusion, and alpha
+28, +44, +60 4 floats each Texture coordinates: uv0 and uv1, uv2 and uv3, uv4 and uv5

The palette is the skinning: per joint three float4 rows, a 3x4 matrix that takes a bind-pose point to the posed one in avatar space. A vertex blends the matrices of its joints by their weights; world then places the avatar. Upload the palette each frame as a uniform array, or as a float texture three texels wide with a row per joint. A vertex shader in GLSL 3.30:

vec4 row(int k) {
  ivec4 j = ivec4(a_joints) + k;
  return a_weights.x * u_palette[j.x] + a_weights.y * u_palette[j.y]
       + a_weights.z * u_palette[j.z] + a_weights.w * u_palette[j.w];
}

void main() {
  vec4 r0 = row(0), r1 = row(1), r2 = row(2);
  vec4 p = vec4(a_position, 1.0);
  vec3 skinned = vec3(dot(r0, p), dot(r1, p), dot(r2, p));
  vec3 n = a_normal.xyz * 2.0 - 1.0;
  vec3 normal = vec3(dot(r0.xyz, n), dot(r1.xyz, n), dot(r2.xyz, n));
  v_normal = mat3(u_world) * normal;
  v_colour = a_colour;
  v_uv01 = a_uv01;
  gl_Position = u_view_proj * (u_world * vec4(skinned, 1.0));
}

The whole shader, with its inputs, is skin.vert. world is row-major for row vectors (v' = v * W); uploaded as it is to a GLSL mat4, which is column-major, it is the matrix that multiplies a column vector on the left, as above. The SDK's space has Y up and an avatar facing -Z at turn 0, left-handed: a right-handed engine negates Z on the way in and out, as the Godot extension does.

Skinning on the CPU

A renderer that cannot skin on the GPU (a software rasteriser, a physics query, a mesh export) asks the SDK for one mesh's posed positions and normals. It writes vertex_count of each, stride floats apart, in avatar space or in world space:

static float positions[65536 * 3], normals[65536 * 3];
const sona_pose_data *pose = NULL;
if (sona_pose(avatar, &pose) == SONA_OK && pose->mesh_count > 0) {
  const sona_mesh *m = &pose->meshes[0];
  if (m->vertex_count <= 65536 &&
      sona_pose_vertices(avatar, 0, SONA_SPACE_WORLD, positions, normals,
          3) == SONA_OK)
    my_draw_positions(positions, normals, m->vertex_count, m->indices,
                      m->index_count);
}

Materials, textures and the look

A mesh's material (pose->materials[m->material]) names its shader family (shader_id), its SONA_MAT_* flags, its textures by index into pose->textures (-1 is neutral: white for colour, black with full alpha for intensity and decal), their address modes and 40 shader parameters. A texture has a key that stays while it lives and a version that changes when its pixels do: the face sheet's changes as the avatar's expression does, so upload a texture again when its version is new. Opaque meshes first, then the transparent ones in the order given.

For the same look as the SDK's own renderer, draw with the SDK's shaders and its shading constants:

size_t len = 0;
const void *vs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 0,
                                    &len);
const void *fs = sona_shader_source(SONA_LANG_GLSL_330, SONA_PROG_CLOTH, 1,
                                    &len);
sona_shading shading;
if (vs && fs && sona_shading_get(ctx, &shading) == SONA_OK) {
  /* shading.irradiance (a cube, 16 x 16 per face), shading.rim[4],
     shading.shadow_rows[12]:
     the constants those shaders read */
}

SONA_PROG_SKIN, SONA_PROG_REFL and SONA_PROG_REFL_SKIN are the other three families; the languages are GLSL ES 3.00, GLSL 3.30, SPIR-V, HLSL and MSL, as the build has them (NULL for a language the build lacks). Or draw with your engine's own materials from the colour texture, the normal and the vertex colour, for a simpler look.

The credit

When your renderer draws the avatars, the SDK draws no badge: show "Powered by 360sona" yourself, with the pixels of sona_credit_get linked to its address, wherever the avatars appear (credit).

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