diff --git a/gui/app/src/utils/skeletonHelper.ts b/gui/app/src/utils/skeletonHelper.ts index 2477b688a..1675640b8 100644 --- a/gui/app/src/utils/skeletonHelper.ts +++ b/gui/app/src/utils/skeletonHelper.ts @@ -29,11 +29,10 @@ const BONE_COLOR_GROUPS: [BodyPart[], string][] = [ 'chartreuse', ], [[BodyPart.LOWER_CHEST], 'purple'], - [[BodyPart.UPPER_WAIST], 'pink'], [[BodyPart.LOWER_WAIST, BodyPart.LEFT_LOWER_ARM, BodyPart.RIGHT_LOWER_ARM], 'red'], [[BodyPart.HIP], 'orange'], [[BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG], 'teal'], - [[BodyPart.LEFT_UPPER_ARM, BodyPart.RIGHT_UPPER_ARM], 'indianred'], + [[BodyPart.UPPER_WAIST, BodyPart.LEFT_UPPER_ARM, BodyPart.RIGHT_UPPER_ARM], 'indianred'], [[BodyPart.LEFT_HAND, BodyPart.RIGHT_HAND], 'fuchsia'], [[BodyPart.LEFT_SHOULDER, BodyPart.RIGHT_SHOULDER], '#00ffff'], ]; diff --git a/server/core/src/main/java/dev/slimevr/config/settings.kt b/server/core/src/main/java/dev/slimevr/config/settings.kt index f10b5ad3b..bc673197a 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -139,7 +139,7 @@ data class SkeletonTogglesConfig( @Serializable data class SkeletonRatiosConfig( val imputeSpineFromUpperToLower: Float = DEFAULT_SPINE_UPPER_LOWER, - val imputeSpineCurvature: Float = 0.8f, + val imputeSpineCurvature: Float = 0.75f, val interpolateHipWithUpperLegs: Float = 0.25f, val interpolateUpperLegsTwistWithLowerLegs: Float = 0.85f, val skatingCorrectionStrength: Float = 0.3f, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/relaxed-spine.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/relaxed-spine.kt index 22bded38e..a76f0ce9d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/relaxed-spine.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/relaxed-spine.kt @@ -2,10 +2,12 @@ package dev.slimevr.skeleton.inputprocessors import com.jme3.math.FastMath import dev.slimevr.config.Settings +import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import io.github.axisangles.ktmath.Quaternion import solarxr_protocol.datatypes.BodyPart +import java.util.EnumMap // At this default value, the user's spine should behave as we meant it to. const val DEFAULT_SPINE_UPPER_LOWER = 0.5f @@ -19,43 +21,42 @@ private enum class SpineSource(val parts: Array) { UPPER_LEGS(arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG)), } -private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float): Float { - // Remap the upper-lower ratio with reliability - val reliabilityAdjusted = remapRatioWithReliability(fromUpperToLower, fromReliability, toReliability) - - // Lerp from upper-lower to reliability-adjusted according to curvature - return FastMath.lerp(fromUpperToLower, reliabilityAdjusted, curvature) -} - -/** - * Remaps a ratio This assumes a default ratio of 50%. - */ -private fun remapRatioWithReliability(ratio: Float, fromReliability: Float, toReliability: Float): Float { - val reliability = (toReliability / fromReliability) * DEFAULT_SPINE_UPPER_LOWER - - return if (ratio <= DEFAULT_SPINE_UPPER_LOWER) { - ratio * 2f * reliability - } else { - reliability + (ratio - DEFAULT_SPINE_UPPER_LOWER) * 2f * (1f - reliability) - } -} - -private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array = arrayOf()): Quaternion { - val bonesToAverage = inputSkeleton.values.filter { it.bodyPart in takeBodyParts } - return bonesToAverage.map { it.rotation } - .reduceIndexedOrNull { index, acc, rotation -> - acc.lerpQ(rotation, 1f / (index + 1)) - } ?: Quaternion.IDENTITY -} +private val SPINE_BONES_TO_RELAX = arrayOf( + BodyPart.UPPER_CHEST, + BodyPart.LOWER_CHEST, + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, + BodyPart.HIP, +) +// TODO : fine tuning! :3 +// (also, refactoring this whole file as it has become a mess!) // The higher a value is, the more reliable it is. // If a "To" is 2x the "From", it'll use 100% "To". // If a "To" is the same as "From", it'll do 50-50. private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when (bodyPart) { + BodyPart.UPPER_CHEST -> when (source) { + // From + SpineSource.UPPER_CHEST -> 7f + + // To (negative to relax the spine + SpineSource.LOWER_CHEST -> -1f + + SpineSource.UPPER_WAIST -> -1.5f + + SpineSource.LOWER_WAIST -> -2f + + SpineSource.HIP -> -4f + + SpineSource.UPPER_LEGS -> -3f + } + BodyPart.LOWER_CHEST -> when (source) { // From SpineSource.UPPER_CHEST -> 10f + SpineSource.LOWER_CHEST -> 20f + // To (negative to relax the spine SpineSource.UPPER_WAIST -> -1.5f @@ -64,22 +65,20 @@ private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when SpineSource.HIP -> -4f SpineSource.UPPER_LEGS -> -3f - - else -> error("Invalid spine combination $bodyPart, $source") } BodyPart.UPPER_WAIST -> when (source) { // From SpineSource.UPPER_CHEST, SpineSource.LOWER_CHEST -> 10f + SpineSource.UPPER_WAIST -> 68f + // To SpineSource.LOWER_WAIST -> 17f SpineSource.HIP -> 5.5f SpineSource.UPPER_LEGS -> 4.5f - - else -> error("Invalid spine combination $bodyPart, $source") } BodyPart.LOWER_WAIST -> when (source) { @@ -88,12 +87,12 @@ private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when SpineSource.UPPER_WAIST -> 6.25f + SpineSource.LOWER_WAIST -> 44f + // To - SpineSource.HIP -> 11f + SpineSource.HIP -> 10f - SpineSource.UPPER_LEGS -> 4.5f - - else -> error("Invalid spine combination $bodyPart, $source") + SpineSource.UPPER_LEGS -> 4.25f } BodyPart.HIP -> when (source) { @@ -104,15 +103,47 @@ private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when SpineSource.LOWER_WAIST -> 13f + SpineSource.HIP -> 48f + // To SpineSource.UPPER_LEGS -> 13f - - else -> error("Invalid spine combination $bodyPart, $source") } else -> error("Invalid missing spine body part $bodyPart") } +private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float, isActive: Boolean): Float { + // Compute reliability ratio + val reliability = (toReliability / fromReliability) * DEFAULT_SPINE_UPPER_LOWER + + return if (isActive) { + // Use a reliability-adjusted curvature directly if relaxing an active bone + reliability * curvature + } else { + // Remap the upper-lower ratio with reliability + val reliabilityAdjusted = remapRatioWithReliability(fromUpperToLower, reliability) + // Lerp from upper-lower to reliability-adjusted according to curvature + FastMath.lerp(fromUpperToLower, reliabilityAdjusted, curvature) + } +} + +/** + * Remaps the From-To ratio with From-To reliability. + */ +private fun remapRatioWithReliability(ratio: Float, reliability: Float): Float = if (ratio <= DEFAULT_SPINE_UPPER_LOWER) { + ratio * (1f / DEFAULT_SPINE_UPPER_LOWER) * reliability +} else { + reliability + (ratio - DEFAULT_SPINE_UPPER_LOWER) * (1f / DEFAULT_SPINE_UPPER_LOWER) * (1f - reliability) +} + +private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array = arrayOf()): Quaternion { + val bonesToAverage = inputSkeleton.values.filter { it.bodyPart in takeBodyParts } + return bonesToAverage.map { it.rotation } + .reduceIndexedOrNull { index, acc, rotation -> + acc.lerpQ(rotation, 1f / (index + 1)) + } ?: Quaternion.IDENTITY +} + /** * Handles imputing the rotation of spine bones that are not actively receiving data from the rotations * of nearby bones. @@ -121,27 +152,23 @@ private fun reliabilityOf(bodyPart: BodyPart, source: SpineSource): Float = when */ class RelaxedSpineInputProcessor(val settings: Settings) : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { + val fixedInputs = BodyPartMap(mutableInputSkeleton) + val ratios = settings.context.state.value.data.skeletonConfig.ratios - val hasUpperChest = mutableInputSkeleton[BodyPart.UPPER_CHEST]?.isRotationActive == true - val hasLowerChest = mutableInputSkeleton[BodyPart.LOWER_CHEST]?.isRotationActive == true - val hasUpperWaist = mutableInputSkeleton[BodyPart.UPPER_WAIST]?.isRotationActive == true - val hasLowerWaist = mutableInputSkeleton[BodyPart.LOWER_WAIST]?.isRotationActive == true - val hasHip = mutableInputSkeleton[BodyPart.HIP]?.isRotationActive == true - val hasUpperLegs = mutableInputSkeleton[BodyPart.LEFT_UPPER_LEG]?.isRotationActive == true && mutableInputSkeleton[BodyPart.RIGHT_UPPER_LEG]?.isRotationActive == true - val missingSpineParts = buildList { - if (!hasLowerChest) add(BodyPart.LOWER_CHEST) - if (!hasUpperWaist) add(BodyPart.UPPER_WAIST) - if (!hasLowerWaist) add(BodyPart.LOWER_WAIST) - if (!hasHip) add(BodyPart.HIP) - } + val hasUpperChest = fixedInputs[BodyPart.UPPER_CHEST]?.isRotationActive == true + val hasLowerChest = fixedInputs[BodyPart.LOWER_CHEST]?.isRotationActive == true + val hasUpperWaist = fixedInputs[BodyPart.UPPER_WAIST]?.isRotationActive == true + val hasLowerWaist = fixedInputs[BodyPart.LOWER_WAIST]?.isRotationActive == true + val hasHip = fixedInputs[BodyPart.HIP]?.isRotationActive == true + val hasUpperLegs = fixedInputs[BodyPart.LEFT_UPPER_LEG]?.isRotationActive == true && fixedInputs[BodyPart.RIGHT_UPPER_LEG]?.isRotationActive == true - for (bodyPart in missingSpineParts) { - val bone = mutableInputSkeleton[bodyPart] ?: continue + for (bodyPart in SPINE_BONES_TO_RELAX) { + val bone = fixedInputs[bodyPart] ?: continue // Get the first active bones above and below this one in the chain val (fromSource, toSource) = when (bodyPart) { - BodyPart.LOWER_CHEST -> { + BodyPart.UPPER_CHEST -> { val from = SpineSource.UPPER_CHEST.takeIf { hasUpperChest } ?: continue val to = when { hasUpperWaist -> SpineSource.UPPER_WAIST @@ -153,8 +180,25 @@ class RelaxedSpineInputProcessor(val settings: Settings) : SkeletonInputProcesso from to to } + BodyPart.LOWER_CHEST -> { + val from = when { + hasLowerChest -> SpineSource.LOWER_CHEST + hasUpperChest -> SpineSource.UPPER_CHEST + else -> continue + } + val to = when { + hasUpperWaist -> SpineSource.UPPER_WAIST + hasLowerWaist -> SpineSource.LOWER_WAIST + hasHip -> SpineSource.HIP + hasUpperLegs -> SpineSource.UPPER_LEGS + else -> continue + } + from to to + } + BodyPart.UPPER_WAIST -> { val from = when { + hasUpperWaist -> SpineSource.UPPER_WAIST hasLowerChest -> SpineSource.LOWER_CHEST hasUpperChest -> SpineSource.UPPER_CHEST else -> continue @@ -170,6 +214,7 @@ class RelaxedSpineInputProcessor(val settings: Settings) : SkeletonInputProcesso BodyPart.LOWER_WAIST -> { val from = when { + hasLowerWaist -> SpineSource.LOWER_WAIST hasUpperWaist -> SpineSource.UPPER_WAIST hasLowerChest -> SpineSource.LOWER_CHEST hasUpperChest -> SpineSource.UPPER_CHEST @@ -185,6 +230,7 @@ class RelaxedSpineInputProcessor(val settings: Settings) : SkeletonInputProcesso BodyPart.HIP -> { val from = when { + hasHip -> SpineSource.HIP hasLowerWaist -> SpineSource.LOWER_WAIST hasUpperWaist -> SpineSource.UPPER_WAIST hasLowerChest -> SpineSource.LOWER_CHEST @@ -203,10 +249,11 @@ class RelaxedSpineInputProcessor(val settings: Settings) : SkeletonInputProcesso ratios.imputeSpineCurvature, reliabilityOf(bodyPart, fromSource), reliabilityOf(bodyPart, toSource), + bodyPart == fromSource.parts.first(), ) - val fromRotation = averageRotation(mutableInputSkeleton, fromSource.parts) - val toRotation = averageRotation(mutableInputSkeleton, toSource.parts) + val fromRotation = averageRotation(fixedInputs, fromSource.parts) + val toRotation = averageRotation(fixedInputs, toSource.parts) mutableInputSkeleton[bodyPart] = bone.copy(rotation = fromRotation.interpQ(toRotation, interpolateRatio)) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt index f2c8fa846..f558ff6f1 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt @@ -264,12 +264,12 @@ private val TOES = listOf( ), Toe( BodyPart.LEFT_INDEX_TOE to BodyPart.RIGHT_INDEX_TOE, - lengthFraction = 0.25f, + lengthFraction = 0.24f, headOffset = Vector3(0.12f, 0f, 0f), ), Toe( BodyPart.LEFT_MIDDLE_TOE to BodyPart.RIGHT_MIDDLE_TOE, - lengthFraction = 0.22f, + lengthFraction = 0.23f, headOffset = Vector3(0f, 0f, 0f), ), Toe( @@ -279,7 +279,7 @@ private val TOES = listOf( ), Toe( BodyPart.LEFT_LITTLE_TOE to BodyPart.RIGHT_LITTLE_TOE, - lengthFraction = 0.2f, + lengthFraction = 0.18f, headOffset = Vector3(-0.26f, 0f, 0f), ), ) diff --git a/server/core/src/main/java/dev/slimevr/vmc/mapping.kt b/server/core/src/main/java/dev/slimevr/vmc/mapping.kt index 2db5fbcf7..7ae6040f7 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/mapping.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/mapping.kt @@ -14,7 +14,7 @@ val BODY_PART_TO_UNITY_BONE: BodyPartMap> = BodyPartMap( BodyPart.NECK to arrayOf("Neck"), BodyPart.UPPER_CHEST to arrayOf("UpperChest"), BodyPart.LOWER_CHEST to arrayOf("Chest"), - BodyPart.LOWER_WAIST to arrayOf("Spine"), + BodyPart.UPPER_WAIST to arrayOf("Spine"), BodyPart.HIP to arrayOf("Hips"), BodyPart.LEFT_SHOULDER to arrayOf("LeftShoulder"), BodyPart.RIGHT_SHOULDER to arrayOf("RightShoulder"), @@ -81,8 +81,8 @@ val VMC_SUPPORTED_BONES: Set = BODY_PART_TO_UNITY_BONE.keys // and figure out how to deal with it (check if present in VRM?) val VMC_HIERARCHY_MAP: BodyPartMap> = BodyPartMap( mapOf( - BodyPart.HIP to arrayOf(BodyPart.LOWER_WAIST, BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG), - BodyPart.LOWER_WAIST to arrayOf(BodyPart.LOWER_CHEST), + BodyPart.HIP to arrayOf(BodyPart.UPPER_WAIST, BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG), + BodyPart.UPPER_WAIST to arrayOf(BodyPart.LOWER_CHEST), BodyPart.LOWER_CHEST to arrayOf(BodyPart.UPPER_CHEST, BodyPart.NECK, BodyPart.LEFT_SHOULDER, BodyPart.RIGHT_SHOULDER), BodyPart.NECK to arrayOf(BodyPart.HEAD), BodyPart.LEFT_UPPER_LEG to arrayOf(BodyPart.LEFT_LOWER_LEG), diff --git a/server/core/src/main/java/dev/slimevr/vmc/vrm.kt b/server/core/src/main/java/dev/slimevr/vmc/vrm.kt index b20be4ea7..e5f9a96a1 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/vrm.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/vrm.kt @@ -109,7 +109,7 @@ fun buildVrmGeometry(reader: VrmReader): VrmGeometry { val vrmHeight = ( offset(BodyPart.HIP) + - offset(BodyPart.LOWER_WAIST) + + offset(BodyPart.UPPER_WAIST) + offset(BodyPart.LOWER_CHEST) + offset(BodyPart.UPPER_CHEST) + offset(BodyPart.NECK) diff --git a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt index d216c5c6b..f155f1041 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt @@ -124,7 +124,7 @@ class InputDecoderTest { val bones = BodyPartMap( mapOf( BodyPart.HIP to bone(BodyPart.HIP, Quaternion.rotationAroundYAxis(0.1f), Vector3.ZERO), - BodyPart.LOWER_WAIST to bone(BodyPart.LOWER_WAIST, Quaternion.rotationAroundXAxis(0.2f), Vector3(0f, 0.15f, 0f)), + BodyPart.UPPER_WAIST to bone(BodyPart.UPPER_WAIST, Quaternion.rotationAroundXAxis(0.2f), Vector3(0f, 0.15f, 0f)), BodyPart.LOWER_CHEST to bone(BodyPart.LOWER_CHEST, chestRotation, chestHead), BodyPart.UPPER_CHEST to bone(BodyPart.UPPER_CHEST, chestRotation, chestHead), BodyPart.NECK to bone(BodyPart.NECK, Quaternion.rotationAroundZAxis(0.15f), Vector3(0f, 0.5f, 0f)),