Stabler spine and better additional trackers support

This commit is contained in:
Erimel committed 2026-10-21 17:58:40 -04:00
1 parent 46390b49a4
commit 655fba5703
3 files changed
+22 -261

No files matched your search

@@ -1,237 +0,0 @@
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 io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.enums.enumEntries
// TODO get rid of this file once new spine is done
object OldSpine {
// At this default value, the user's spine should behave as we meant it to.
const val DEFAULT_SPINE_UPPER_LOWER = 0.5f
private data class Reliability(
val forward: Float,
val side: Float? = null, // Use forward if null
)
private enum class SpineSource(val parts: Array<BodyPart>) {
UPPER_CHEST(arrayOf(BodyPart.UPPER_CHEST)),
LOWER_CHEST(arrayOf(BodyPart.LOWER_CHEST)),
UPPER_WAIST(arrayOf(BodyPart.UPPER_WAIST)),
LOWER_WAIST(arrayOf(BodyPart.LOWER_WAIST)),
HIP(arrayOf(BodyPart.HIP)),
UPPER_LEGS(arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG)),
}
private val SPINE_SOURCES = enumEntries<SpineSource>().toTypedArray()
// The higher a value is, the more reliable that source 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.
// A negative value will go towards the opposite rotation.
// A From and To may be taken from different roots.
private val SPINE_SOURCE_RELIABILITY = arrayOf(
SpineSource.UPPER_CHEST to mapOf(
SpineSource.UPPER_CHEST to Reliability(-6.5f, -24f), // Itself
// To
SpineSource.LOWER_CHEST to Reliability(1f),
SpineSource.UPPER_WAIST to Reliability(1.5f),
SpineSource.LOWER_WAIST to Reliability(2f),
SpineSource.HIP to Reliability(4f),
SpineSource.UPPER_LEGS to Reliability(1.5f, 1.75f),
),
SpineSource.LOWER_CHEST to mapOf(
// From
SpineSource.UPPER_CHEST to Reliability(-10f, -42f),
SpineSource.LOWER_CHEST to Reliability(-11f, -45f), // Itself
// To
SpineSource.UPPER_WAIST to Reliability(1.5f, 10f),
SpineSource.LOWER_WAIST to Reliability(2f, 7f),
SpineSource.HIP to Reliability(4f),
SpineSource.UPPER_LEGS to Reliability(3.25f, 3.75f),
),
SpineSource.UPPER_WAIST to mapOf(
// From
SpineSource.UPPER_CHEST to Reliability(11f),
SpineSource.LOWER_CHEST to Reliability(11.5f),
SpineSource.UPPER_WAIST to Reliability(60f), // Itself
// To
SpineSource.LOWER_WAIST to Reliability(17f, 20f),
SpineSource.HIP to Reliability(5.5f, 8f),
SpineSource.UPPER_LEGS to Reliability(0.25f, 4.25f),
),
SpineSource.LOWER_WAIST to mapOf(
// From
SpineSource.UPPER_CHEST to Reliability(6f),
SpineSource.LOWER_CHEST to Reliability(6.5f),
SpineSource.UPPER_WAIST to Reliability(15f),
SpineSource.LOWER_WAIST to Reliability(44f), // Itself
// To
SpineSource.HIP to Reliability(10f, 12f),
SpineSource.UPPER_LEGS to Reliability(1.75f, 10f),
),
SpineSource.HIP to mapOf(
// From
SpineSource.UPPER_CHEST to Reliability(9.5f, 9f),
SpineSource.LOWER_CHEST to Reliability(9.75f, 9.25f),
SpineSource.UPPER_WAIST to Reliability(13f, 20f),
SpineSource.LOWER_WAIST to Reliability(14f, 30f),
SpineSource.HIP to Reliability(32f, 56f), // Itself
// To
SpineSource.UPPER_LEGS to Reliability(7f, 22f),
),
)
/** The spine bones this processor imputes, top down. Other sources are only read as references. */
val SPINE_IMPUTED_PARTS: List<BodyPart> = SPINE_SOURCE_RELIABILITY.flatMap { (source, _) -> source.parts.toList() }
private fun nearestActive(startIndex: Int, step: Int, sourceActive: Map<SpineSource, Boolean>): Int? {
var i = startIndex
while (i in SPINE_SOURCES.indices) {
if (sourceActive[SPINE_SOURCES[i]] == true) return i
i += step
}
return null
}
private fun getFromTo(selfIndex: Int, sourceActive: Map<SpineSource, Boolean>): Pair<Int?, Int?> {
// Find From, the first active bone in itself + its parents.
val from = nearestActive(selfIndex, -1, sourceActive)
// Find To, the next active bone in its children, skipping the immediate next if itself is active except if it's the only one (for hip).
// If To is null, we return null for the whole thing
val isActive = sourceActive[SPINE_SOURCES[selfIndex]] == true
val immediateNext = selfIndex + 1
val takeImmediateNext = (!isActive || selfIndex + 2 == SPINE_SOURCES.count()) && sourceActive[SPINE_SOURCES[immediateNext]] == true
val to = if (takeImmediateNext) immediateNext else nearestActive(selfIndex + 2, 1, sourceActive)
return from to to
}
/**
* Remaps the From-To ratio with From-To reliability.
*/
private fun remapRatioWithReliability(fromUpperToLower: Float, reliability: Float): Float = if (fromUpperToLower <= DEFAULT_SPINE_UPPER_LOWER) {
fromUpperToLower * (1f / DEFAULT_SPINE_UPPER_LOWER) * reliability
} else {
reliability + (fromUpperToLower - DEFAULT_SPINE_UPPER_LOWER) * (1f / DEFAULT_SPINE_UPPER_LOWER) * (1f - reliability)
}
private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReliability: Float, toReliability: Float, isActive: Boolean): Float {
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)
}
}
private fun averageRotation(inputSkeleton: InputSkeleton, takeBodyParts: Array<BodyPart> = arrayOf()): Quaternion {
val bonesToAverage = inputSkeleton.values.filter { it.bodyPart in takeBodyParts }
return bonesToAverage.map { it.rotation }
.reduceIndexedOrNull { index, acc, rotation ->
// Very important that this is interpQ as lerpQ will break on axes/polarity
acc.interpQ(rotation, 1f / (index + 1))
} ?: Quaternion.IDENTITY
}
/**
* Interpolates between 2 rotations with a different ratio for forward and the rest/side.
*/
fun interpolateForwardSide(fromRotation: Quaternion, toRotation: Quaternion, forwardRatio: Float, sideRatio: Float?): Quaternion {
if (sideRatio == null) return fromRotation.interpQ(toRotation, forwardRatio)
// Work with the rotation delta to avoid axes being ambiguous
val delta = fromRotation.inv() * toRotation
// Decompose the delta into its twist (forward) and swing (remaining/sides) along the x-axis
val forward = delta.project(Vector3.POS_X).unit()
val side = delta / forward
// Scale each axis of the delta with our ratios
val scaledForward = forward.pow(forwardRatio)
val scaledSide = side.pow(sideRatio)
// Recompose the delta. We removed formward from delta to get side, so we must add forward to side back.
val scaledDelta = scaledSide * scaledForward
// Return scaled delta applied to From rotation
return fromRotation * scaledDelta
}
/**
* Handles imputing the rotation of spine bones that are not actively receiving data from the rotations
* of nearby bones.
*/
class SpineInputProcessor(val settings: Settings) : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
val ratios = settings.context.state.value.data.skeletonConfig.ratios
val boneInputs = BodyPartMap(mutableInputSkeleton)
val sourceActive = SPINE_SOURCES.associateWith { it.parts.all { part -> boneInputs[part]?.isRotationActive == true } }
val fromTo = SPINE_SOURCE_RELIABILITY.withIndex().associate { (selfIndex, source) ->
source.first to getFromTo(selfIndex, sourceActive)
}
for ((spineIndex, spineSourceReliability) in SPINE_SOURCE_RELIABILITY.withIndex()) {
val spineSource = spineSourceReliability.first
// For optimization's sake, assume only one BodyPart per SpineSource we traverse.
val bodyPart = spineSource.parts.first()
val bone = boneInputs[bodyPart] ?: continue
val isActive = bone.isRotationActive
// Get reliabilities mapped to this spine bone
val reliabilities = SPINE_SOURCE_RELIABILITY[spineIndex].second
// Get the spine sources for the current spineSource
val (fromIndex, toIndex) = fromTo[spineSource] ?: error("No fromTo for $spineSource found.")
// toIndex will be null if the bone has no active children.
if (toIndex == null) continue
val (fromSpineSource, toSpineSource) = if (fromIndex != null) {
SPINE_SOURCES[fromIndex] to SPINE_SOURCES[toIndex]
} else {
// Fall back to itself to its to's to
val toTo = fromTo[SPINE_SOURCES[toIndex]] ?: continue
spineSource to SPINE_SOURCES[toTo.second ?: continue]
}
// We are interpolating as-if we were the To, but using self instead of to
val sourceActive = isActive || fromIndex == null
// Get the interpolation ratios. sideRatio is null if nothing specified for side.
val fromReliability = reliabilities[fromSpineSource] ?: continue
val toReliability = reliabilities[toSpineSource] ?: continue
val forwardRatio = interpolateRatio(ratios.imputeSpineFromUpperToLower, ratios.spineCurvature, fromReliability.forward, toReliability.forward, sourceActive)
val sideRatio = if (fromReliability.side != null || toReliability.side != null) {
interpolateRatio(ratios.imputeSpineFromUpperToLower, ratios.spineCurvature, fromReliability.side ?: fromReliability.forward, toReliability.side ?: toReliability.forward, sourceActive)
} else {
null
}
// If from is null, use to as from and use to's to as to
val fromParts = if (fromIndex != null) fromSpineSource.parts else SPINE_SOURCES[toIndex].parts
val toParts = toSpineSource.parts
// Interpolate between from and to using our interpolation ratio.
mutableInputSkeleton[bodyPart] = bone.copy(
rotation = interpolateForwardSide(
averageRotation(boneInputs, fromParts),
averageRotation(boneInputs, toParts),
forwardRatio,
sideRatio,
),
)
}
}
}
}
@@ -38,43 +38,43 @@ private val SPINE_SOURCES = enumEntries<SpineSource>().toTypedArray()
// Order matters! Missing elements of the inner arrays will be skipped gracefully.
private val SPINE_SOURCE_RELIABILITY = arrayOf(
SpineSource.UPPER_CHEST to arrayOf(
SpineSource.UPPER_CHEST to Reliability(-7f, -20f), // Itself
SpineSource.UPPER_CHEST to Reliability(-11.5f, -20f), // Itself
// To
SpineSource.LOWER_CHEST to Reliability(-6f, -15f),
SpineSource.UPPER_WAIST to Reliability(1.5f, 4f,),
SpineSource.LOWER_WAIST to Reliability(2f, 5f),
SpineSource.HIP to Reliability(4f, 6f, 0.25f),
SpineSource.LOWER_CHEST to Reliability(-6f, -15f, 0f),
SpineSource.UPPER_WAIST to Reliability(1.5f, 4f, 0.4f),
SpineSource.LOWER_WAIST to Reliability(2f, 5f, 0.4f),
SpineSource.HIP to Reliability(4f, 6.25f, 0.3f),
SpineSource.UPPER_LEGS to Reliability(4f, 5f),
),
SpineSource.LOWER_CHEST to arrayOf(
// From
SpineSource.UPPER_CHEST to Reliability(-10f, 40f),
SpineSource.LOWER_CHEST to Reliability(-11f, 42f), // Itself
SpineSource.UPPER_CHEST to Reliability(-18f, 40f),
SpineSource.LOWER_CHEST to Reliability(30f), // Itself
// To
SpineSource.UPPER_WAIST to Reliability(1.5f, 10f),
SpineSource.LOWER_WAIST to Reliability(2f, 8f),
SpineSource.HIP to Reliability(4f, 7f, 0.33f),
SpineSource.UPPER_WAIST to Reliability(1.5f, 10f,0.4f),
SpineSource.LOWER_WAIST to Reliability(2f, 8f, 0.4f),
SpineSource.HIP to Reliability(4f, 7.25f, 0.3f),
SpineSource.UPPER_LEGS to Reliability(4f, 6f),
),
SpineSource.UPPER_WAIST to arrayOf(
// From
SpineSource.UPPER_CHEST to Reliability(11f),
SpineSource.UPPER_CHEST to Reliability(10.5f),
SpineSource.LOWER_CHEST to Reliability(11.5f),
SpineSource.UPPER_WAIST to Reliability(50f), // Itself
SpineSource.UPPER_WAIST to Reliability(40f), // Itself
// To
SpineSource.LOWER_WAIST to Reliability(17f, 20f),
SpineSource.HIP to Reliability(5.5f, 13f, 0.85f),
SpineSource.UPPER_LEGS to Reliability(0.25f, 11f),
SpineSource.LOWER_WAIST to Reliability(16f, 18f, 0.85f),
SpineSource.HIP to Reliability(4.5f, 13.5f, 0.85f),
SpineSource.UPPER_LEGS to Reliability(0.25f, 12f),
),
SpineSource.LOWER_WAIST to arrayOf(
// From
SpineSource.UPPER_CHEST to Reliability(6f),
SpineSource.LOWER_CHEST to Reliability(6.5f),
SpineSource.UPPER_WAIST to Reliability(15f),
SpineSource.LOWER_WAIST to Reliability(44f), // Itself
SpineSource.LOWER_CHEST to Reliability(6.75f),
SpineSource.UPPER_WAIST to Reliability(25f),
SpineSource.LOWER_WAIST to Reliability(38f), // Itself
// To
SpineSource.HIP to Reliability(10f, 14f, 0.9f),
SpineSource.UPPER_LEGS to Reliability(1.75f, 12f),
SpineSource.HIP to Reliability(5.25f, 13f, 0.85f),
SpineSource.UPPER_LEGS to Reliability(1.75f, 11f),
),
SpineSource.HIP to arrayOf(
// From
@@ -82,9 +82,9 @@ private val SPINE_SOURCE_RELIABILITY = arrayOf(
SpineSource.LOWER_CHEST to Reliability(9.75f, 9.25f),
SpineSource.UPPER_WAIST to Reliability(13f, 20f),
SpineSource.LOWER_WAIST to Reliability(14f, 30f),
SpineSource.HIP to Reliability(38f, 60f), // Itself
SpineSource.HIP to Reliability(38f, 60f, 0.9f), // Itself
// To
SpineSource.UPPER_LEGS to Reliability(7f, 22f),
SpineSource.UPPER_LEGS to Reliability(7f, 17f),
),
)
private val SPINE_SOURCE_RELIABILITY_INDEXED = SPINE_SOURCE_RELIABILITY.withIndex()
@@ -14,7 +14,6 @@ import dev.slimevr.skeleton.inputprocessors.FallbackInputProcessor
import dev.slimevr.skeleton.inputprocessors.FingersInputProcessor
import dev.slimevr.skeleton.inputprocessors.HeadPositionFallbackInputProcessor
import dev.slimevr.skeleton.inputprocessors.NeckInputProcessor
import dev.slimevr.skeleton.inputprocessors.OldSpine
import dev.slimevr.skeleton.inputprocessors.RotationPredictionInputProcessor
import dev.slimevr.skeleton.inputprocessors.RotationSmoothingInputProcessor
import dev.slimevr.skeleton.inputprocessors.SpineInputProcessor
@@ -282,7 +281,6 @@ class Skeleton(
HeadPositionFallbackInputProcessor(),
FallbackInputProcessor(),
UpperLegsRollAlignInputProcessor(settings),
// OldSpine.SpineInputProcessor(settings),
SpineInputProcessor(settings),
NeckInputProcessor(),
DirectLinkInputProcessor(),