Build a 3D avatar editor

Start with one edit you can see clearly. A hairstyle selector shows how to read the catalog, change a single recipe value, wait for the new geometry, then save the result.

Read the guide
Jules · Thinking

Choose the controls your screen needs

A compact profile editor might expose hair, eyewear and complexion. A full editor can group all 95 fields by their catalog categories. Each field has a stable ID, a display name, a kind and an initial value. A menu has named choices with numeric values. A float accepts −1 through 1. A Boolean uses 0 or 1.

Skin and shade live at the top level of the recipe. Hair, glasses and most other features sit inside values. The base complexion is an index into the 17 skin entries. Use the displayed label for people and the catalog value when writing their recipe.

Wire up a hairstyle menu

In the page with your mounted avatar, add a labeled select with id hairstyle and a status element with id edit-status. This module code reads the current selection, then disables the picker briefly while Clayfaces applies each edit. Starting from the current recipe preserves the other features.

JavaScript
const field = avatar.fields.find(item => item.id === 'Hairstyle/Style');
const picker = document.querySelector('#hairstyle');
const status = document.querySelector('#edit-status');
for (const option of field.options) {
  picker.add(new Option(option.name, String(option.value)));
}
const selected = () => String(avatar.getAppearance().values[field.id] ?? field.initial);
picker.value = selected();
picker.onchange = async () => {
  picker.disabled = true;
  try {
    const look = avatar.getAppearance();
    look.values[field.id] = Number(picker.value);
    await avatar.setAppearance(look);
    status.textContent = 'Hairstyle applied.';
  } catch {
    status.textContent = 'Could not apply this hairstyle.';
  } finally {
    picker.value = selected();
    picker.disabled = false;
  }
};

Test what survives the edit

Give the character round glasses and a few freckles. Change only the hairstyle, then inspect avatar.getAppearance(). The glasses, freckle selection and complexion should still be present. A selected value equal to its catalog default can disappear from values, which is why the picker falls back to field.initial.

On iOS the package already supplies an editor. Present ClayfacesEditor(appearance: $look) from a sheet and pass look to the visible ClayfacesView. A saved recipe from either editor can be reopened on the other platform.

Reference

Related guides

Share a character between iOS and webAdd a 3D avatar to SwiftUI