Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ mannequin the user reshapes with sliders. Nothing else is in scope yet.
## What v1 does

- Renders a realistic parametric human with Filament.
- Sliders are generated at runtime from the model's 24 glTF morph targets.
- Sliders are generated at runtime from the model's 24 glTF morph targets. `*_min`/`*_max`
pairs are combined into one slider per body dimension (0.5 = neutral).
- An "Enter measurements" panel maps height, weight, chest, waist and hips to body shape;
results are approximate and can be refined with the sliders.
- One-finger drag orbits, pinch zooms.
- Slider values persist across restarts (DataStore).

Expand Down Expand Up @@ -70,7 +73,7 @@ with the Blender command above when the mesh changes.

## Next milestones (not started)

1. Skin tone and preset body types
1. MakeHuman CC0 skin textures and more region targets
2. Photo-based face, then photo-derived body
3. Wardrobe item CRUD (photos + tags)
4. Clothing layered onto the avatar
Expand Down
Binary file modified app/src/main/assets/models/avatar.glb
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AvatarViewModel(application: Application) : AndroidViewModel(application)
}
}

fun setWeight(name: String, value: Float) = update { it.with(name, value) }
fun setWeights(weights: Map<String, Float>) = update { old -> old.copy(weights = old.weights + weights) }
fun reset() = update { AvatarConfig() }

private fun update(transform: (AvatarConfig) -> AvatarConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.example.clothingsystem.avatar

data class MorphControl(
val key: String,
val label: String,
val paired: Boolean,
)

object MorphMapping {
private val labels = mapOf(
"gender" to "Gender",
"age" to "Age",
"muscle" to "Muscle",
"weight" to "Weight",
"proportions" to "Proportions",
"height" to "Height",
"cupsize" to "Cup size",
"firmness" to "Firmness",
)

fun controls(names: List<String>): List<MorphControl> {
val pairedBases = names.filter { it.endsWith("_min") }.map { it.removeSuffix("_min") }.toSet()
return names.mapNotNull { name ->
when {
name.endsWith("_min") && pairedBases.contains(name.removeSuffix("_min")) -> {
val base = name.removeSuffix("_min")
MorphControl(base, pretty(base), true)
}
name.endsWith("_max") && pairedBases.contains(name.removeSuffix("_max")) -> null
else -> MorphControl(name, pretty(name), false)
}
}
}

fun weights(control: MorphControl, value: Float): Map<String, Float> {
if (!control.paired) return mapOf(control.key to value)
val min = ((0.5f - value) * 2f).coerceIn(0f, 1f)
val max = ((value - 0.5f) * 2f).coerceIn(0f, 1f)
return mapOf("${control.key}_min" to min, "${control.key}_max" to max)
}

fun value(control: MorphControl, weights: Map<String, Float>, defaults: Map<String, Float>): Float {
if (!control.paired) return weights[control.key] ?: defaults[control.key] ?: 0f
val min = weights["${control.key}_min"] ?: defaults["${control.key}_min"] ?: 0f
val max = weights["${control.key}_max"] ?: defaults["${control.key}_max"] ?: 0f
return (0.5f + (max - min) / 2f).coerceIn(0f, 1f)
}

private fun pretty(name: String): String = labels[name] ?: name
.replace("\$md-", "detail ")
.replace("\$as", "Asian")
.replace("\$ca", "Caucasian")
.replace("\$af", "African")
.replace("\$fe", "female")
.replace("\$ma", "male")
.replace("\$yn", "young")
.replace("\$av", "average")
.replace("\$mu", "muscle")
.replace("\$wg", "weight")
.replace("\$", "")
.replace('-', ' ')
.trim()
.replaceFirstChar { it.uppercase() }
}

data class Measurements(
val heightCm: Float = 170f,
val weightKg: Float = 70f,
val chestCm: Float = 95f,
val waistCm: Float = 80f,
val hipsCm: Float = 98f,
) {
fun toControlValues(): Map<String, Float> {
fun norm(value: Float, min: Float, max: Float) = ((value - min) / (max - min)).coerceIn(0f, 1f)
val proportions = listOf(
norm(chestCm, 70f, 130f),
norm(waistCm, 55f, 120f),
norm(hipsCm, 75f, 140f),
).average().toFloat()
return mapOf(
"height" to norm(heightCm, 140f, 210f),
"weight" to norm(weightKg, 40f, 150f),
"proportions" to proportions,
)
}
}
105 changes: 84 additions & 21 deletions app/src/main/java/com/example/clothingsystem/ui/AvatarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package com.example.clothingsystem.ui

import android.view.Choreographer
import android.view.SurfaceView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
Expand All @@ -22,37 +24,32 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.clothingsystem.R
import com.example.clothingsystem.avatar.AvatarModel
import com.example.clothingsystem.avatar.AvatarRenderer
import com.example.clothingsystem.avatar.AvatarViewModel
import com.example.clothingsystem.avatar.Measurements
import com.example.clothingsystem.avatar.MorphControl
import com.example.clothingsystem.avatar.MorphMapping
import com.google.android.filament.utils.ModelViewer

private data class FrameHandle(
val choreographer: Choreographer,
val callback: Choreographer.FrameCallback,
)

private fun labelFor(name: String): String = name
.replace("\$md-", "")
.replace("_min", " (low)")
.replace("_max", " (high)")
.replace('-', ' ')
.replace('$', ' ')
.trim()
.replaceFirstChar { it.uppercase() }

@Composable
fun AvatarScreen(viewModel: AvatarViewModel = viewModel()) {
val config by viewModel.config.collectAsState()
val latestConfig = remember { mutableStateOf(config) }
latestConfig.value = config
val model = remember { mutableStateOf<AvatarModel?>(null) }

Column(modifier = Modifier.fillMaxSize().background(Color(0xFF20242B))) {
Column(modifier = Modifier.fillMaxSize()) {
Box(modifier = Modifier.weight(1f).fillMaxWidth()) {
AndroidView(
modifier = Modifier.fillMaxSize(),
Expand Down Expand Up @@ -98,16 +95,21 @@ fun AvatarScreen(viewModel: AvatarViewModel = viewModel()) {
Column(modifier = Modifier.fillMaxWidth().weight(1f).verticalScroll(rememberScrollState())) {
val current = model.value
if (current != null) {
for (index in current.morphNames.indices) {
val name = current.morphNames[index]
val value = config.weight(name, current.defaultWeights.getOrNull(index) ?: 0f)
Text(text = labelFor(name), color = Color.White, modifier = Modifier.padding(start = 24.dp, top = 8.dp, end = 24.dp))
Slider(
value = value,
onValueChange = { viewModel.setWeight(name, it) },
valueRange = 0f..1f,
modifier = Modifier.padding(horizontal = 24.dp),
)
val defaults = current.morphNames.zip(current.defaultWeights.toTypedArray()).toMap()
val controls = MorphMapping.controls(current.morphNames)
for (control in controls) {
val value = MorphMapping.value(control, config.weights, defaults)
MorphSlider(control, value) { viewModel.setWeights(MorphMapping.weights(control, it)) }
}
MeasurementsPanel { measurements ->
val mapped: Map<String, Float> = measurements.toControlValues().entries
.flatMap { entry ->
val control = controls.firstOrNull { it.key == entry.key }
if (control != null) MorphMapping.weights(control, entry.value).entries
else emptySet<Map.Entry<String, Float>>()
}
.associate { it.key to it.value }
viewModel.setWeights(mapped)
}
}
TextButton(
Expand All @@ -119,3 +121,64 @@ fun AvatarScreen(viewModel: AvatarViewModel = viewModel()) {
}
}
}

@Composable
private fun MorphSlider(control: MorphControl, value: Float, onChange: (Float) -> Unit) {
Text(
text = control.label,
color = Color.White,
modifier = Modifier.padding(start = 24.dp, top = 8.dp, end = 24.dp),
)
Slider(
value = value,
onValueChange = onChange,
valueRange = 0f..1f,
modifier = Modifier.padding(horizontal = 24.dp),
)
}

@Composable
private fun MeasurementsPanel(onApply: (Measurements) -> Unit) {
val expanded = remember { mutableStateOf(false) }
val fields = listOf("Height (cm)", "Weight (kg)", "Chest (cm)", "Waist (cm)", "Hips (cm)")
val values = remember { mutableStateOf(List(5) { "" }) }

TextButton(onClick = { expanded.value = !expanded.value }, modifier = Modifier.padding(horizontal = 16.dp)) {
Text(text = if (expanded.value) "Hide measurements" else "Enter measurements", color = Color.White)
}
if (expanded.value) {
fields.forEachIndexed { index, label ->
OutlinedTextField(
value = values.value[index],
onValueChange = { input ->
values.value = values.value.toMutableList().also { it[index] = input }
},
label = { Text(label) },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
singleLine = true,
modifier = Modifier.fillMaxWidth().padding(horizontal = 24.dp, vertical = 4.dp),
)
}
Button(
onClick = {
val numbers = values.value.map { it.toFloatOrNull() }
if (numbers.all { it != null }) {
onApply(
Measurements(
heightCm = numbers[0]!!, weightKg = numbers[1]!!,
chestCm = numbers[2]!!, waistCm = numbers[3]!!, hipsCm = numbers[4]!!,
),
)
}
},
modifier = Modifier.padding(start = 24.dp, top = 8.dp),
) {
Text("Apply measurements")
}
Text(
text = "Approximate: height, weight and proportions are mapped to body shape. Refine with the sliders above.",
color = Color.White.copy(alpha = 0.6f),
modifier = Modifier.padding(horizontal = 24.dp, vertical = 8.dp),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.clothingsystem.avatar

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class MorphMappingTest {
@Test
fun pairsMinAndMaxIntoOneControl() {
val controls = MorphMapping.controls(listOf("height_min", "height_max", "age_min", "age_max", "face_a"))
assertEquals(listOf("height", "age", "face_a"), controls.map { it.key })
assertTrue(controls[0].paired)
assertTrue(!controls[2].paired)
}

@Test
fun pairedSliderNeutralGivesZeroWeights() {
val control = MorphMapping.controls(listOf("height_min", "height_max")).first()
assertEquals(mapOf("height_min" to 0f, "height_max" to 0f), MorphMapping.weights(control, 0.5f))
}

@Test
fun pairedSliderExtremes() {
val control = MorphMapping.controls(listOf("height_min", "height_max")).first()
assertEquals(mapOf("height_min" to 1f, "height_max" to 0f), MorphMapping.weights(control, 0f))
assertEquals(mapOf("height_min" to 0f, "height_max" to 1f), MorphMapping.weights(control, 1f))
}

@Test
fun readsValueBackFromWeights() {
val control = MorphMapping.controls(listOf("height_min", "height_max")).first()
assertEquals(0.25f, MorphMapping.value(control, MorphMapping.weights(control, 0.25f), emptyMap()))
}

@Test
fun measurementsMapWithinRange() {
val values = Measurements(heightCm = 210f, weightKg = 40f).toControlValues()
assertEquals(1f, values["height"])
assertEquals(0f, values["weight"])
assertTrue(values.getValue("proportions") in 0f..1f)
}
}
9 changes: 9 additions & 0 deletions tools/make_avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ def main():
human = HumanService.create_human()
human.name = "Avatar"

skin = bpy.data.materials.new("Skin")
skin.use_nodes = True
principled = skin.node_tree.nodes.get("Principled BSDF")
principled.inputs["Base Color"].default_value = (0.76, 0.59, 0.48, 1.0)
principled.inputs["Roughness"].default_value = 0.82
principled.inputs["Metallic"].default_value = 0.0
human.data.materials.clear()
human.data.materials.append(skin)

build_shape_keys(human)

output = Path(__file__).resolve().parents[1] / "app/src/main/assets/models/avatar.glb"
Expand Down
Loading