From ac6b6b85a2e69104a27da0af1e445cce88b876fa Mon Sep 17 00:00:00 2001 From: Erimel Date: Tue, 6 Oct 2026 14:39:46 -0400 Subject: [PATCH] Rewrite bone fallback processor --- .../inputprocessors/acceleration-fallback.kt | 43 ---------- .../inputprocessors/bone-yaw-fallback.kt | 45 ---------- .../{bone-direct-link.kt => direct-link.kt} | 14 ++-- .../skeleton/inputprocessors/fallback.kt | 83 +++++++++++++++++++ .../main/java/dev/slimevr/skeleton/module.kt | 10 +-- 5 files changed, 95 insertions(+), 100 deletions(-) delete mode 100644 server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt delete mode 100644 server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt rename server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/{bone-direct-link.kt => direct-link.kt} (67%) create mode 100644 server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt deleted file mode 100644 index dd42ffe9a..000000000 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt +++ /dev/null @@ -1,43 +0,0 @@ -package dev.slimevr.skeleton.inputprocessors - -import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonInputProcessor -import dev.slimevr.skeleton.forEachBone -import dev.slimevr.skeleton.iterateBodyPartHierarchy -import solarxr_protocol.datatypes.BodyPart -import kotlin.collections.set - -/** - * Handles replacing accelerations of boneInputs that are not actively receiving any by - * falling back to their parent's, or child's, acceleration. - */ -// TODO merge this with BoneYawFallbackInputProcessor -class AccelerationFallbackInputProcessor : SkeletonInputProcessor { - override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - val processedParts: MutableSet = mutableSetOf() - mutableInputSkeleton.forEachBone { parentPart, parentBone -> - if (!parentBone.isAccelerationActive) { - if (parentPart in processedParts) return@forEachBone - - // Parent doesn't have rotation and didn't get any earlier in the chain. - // Use this parent's children to set its own yaw. - val childAccel = iterateBodyPartHierarchy(parentPart, true).firstOrNull { (_, childPart) -> - mutableInputSkeleton[childPart]?.isAccelerationActive ?: false - }?.let { (_, childPart) -> - mutableInputSkeleton[childPart]?.acceleration ?: return@forEachBone - } ?: return@forEachBone - - mutableInputSkeleton[parentPart] = parentBone.copy(acceleration = childAccel) - } else { - val parentAccel = parentBone.acceleration - for ((_, childPart) in iterateBodyPartHierarchy(parentPart, true)) { - val childBone = mutableInputSkeleton[childPart] ?: continue - if (childBone.isAccelerationActive) continue // Child needs to be inactive - - processedParts.add(childPart) - mutableInputSkeleton[childPart] = childBone.copy(acceleration = parentAccel) - } - } - } - } -} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt deleted file mode 100644 index 6b213abd2..000000000 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt +++ /dev/null @@ -1,45 +0,0 @@ -package dev.slimevr.skeleton.inputprocessors - -import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.SkeletonInputProcessor -import dev.slimevr.skeleton.forEachBone -import dev.slimevr.skeleton.iterateBodyPartHierarchy -import solarxr_protocol.datatypes.BodyPart -import kotlin.collections.set - -/** - * Handles replacing rotations of boneInputs that are not actively receiving data by - * falling back to their parent's, then children's, yaw. - */ -class BoneYawFallbackInputProcessor : SkeletonInputProcessor { - // TODO add logic to prevent the head from taking the fingers' yaw. Same with acceleration fallback processor. - // (Head taking spine yaw is fine. No taking arm yaw, and probably no legs. Maybe other cases) - override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - val processedParts: MutableSet = mutableSetOf() - mutableInputSkeleton.forEachBone { parentPart, parentBone -> - if (!parentBone.isRotationActive) { - if (parentPart in processedParts) return@forEachBone - - // Parent doesn't have rotation and didn't get any earlier in the chain. - // Use this parent's children to set its own yaw. - val childYaw = iterateBodyPartHierarchy(parentPart, true).firstOrNull { (_, childPart) -> - mutableInputSkeleton[childPart]?.isRotationActive ?: false - }?.let { (_, childPart) -> - mutableInputSkeleton[childPart]?.rotation?.eulerHeading() ?: return@forEachBone - } ?: return@forEachBone - - mutableInputSkeleton[parentPart] = parentBone.copy(rotation = childYaw) - } else { - // Use this parent's yaw to set the children's yaw. - val parentYaw = parentBone.rotation.eulerHeading() - for ((_, childPart) in iterateBodyPartHierarchy(parentPart, true)) { - val childBone = mutableInputSkeleton[childPart] ?: continue - if (childBone.isRotationActive) continue // Child needs to be inactive - - processedParts.add(childPart) - mutableInputSkeleton[childPart] = childBone.copy(rotation = parentYaw) - } - } - } - } -} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/direct-link.kt similarity index 67% rename from server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/direct-link.kt index a2cc4119e..6bb1540da 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/direct-link.kt @@ -30,18 +30,20 @@ val BONE_DIRECT_LINK_SOURCES = arrayOf( ) /** - * Handles setting the rotation of an inactive bone with its source bone. + * Handles setting the rotation and acceleration of an inactive bone with its source bone. */ -class BoneDirectLinkInputProcessor : SkeletonInputProcessor { +class DirectLinkInputProcessor : SkeletonInputProcessor { override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { for ((bodyPart, source) in BONE_DIRECT_LINK_SOURCES) { val bone = mutableInputSkeleton[bodyPart] ?: continue - if (bone.isRotationActive) continue + if (bone.isRotationActive && bone.isAccelerationActive) continue - val rotation = mutableInputSkeleton[source]?.rotation ?: continue - if (rotation == bone.rotation) continue + val rotation = if (!bone.isRotationActive) mutableInputSkeleton[source]?.rotation ?: bone.rotation else bone.rotation + val acceleration = if (!bone.isAccelerationActive) mutableInputSkeleton[source]?.acceleration ?: bone.acceleration else bone.acceleration - mutableInputSkeleton[bodyPart] = bone.copy(rotation = rotation) + if (rotation == bone.rotation && acceleration == bone.acceleration) continue + + mutableInputSkeleton[bodyPart] = bone.copy(rotation = rotation, acceleration = acceleration) } } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt new file mode 100644 index 000000000..d71021f89 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/fallback.kt @@ -0,0 +1,83 @@ +package dev.slimevr.skeleton.inputprocessors + +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonInputProcessor +import dev.slimevr.skeleton.findFirstParent +import dev.slimevr.skeleton.forEachBone +import solarxr_protocol.datatypes.BodyPart +import kotlin.collections.set + +// Only allow child fallbacks in certain cases +val CHILD_FALLBACKS = mapOf( + // Head and neck will fall back on spine + BodyPart.HEAD to arrayOf( + BodyPart.NECK, + BodyPart.UPPER_CHEST, + BodyPart.LOWER_CHEST, + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, + BodyPart.HIP, + ), + BodyPart.NECK to arrayOf( + BodyPart.UPPER_CHEST, + BodyPart.LOWER_CHEST, + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, + BodyPart.HIP, + ), + + // Upper spine will fall back on lower spine + BodyPart.UPPER_CHEST to arrayOf( + BodyPart.LOWER_CHEST, + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, + BodyPart.HIP, + ), + BodyPart.LOWER_CHEST to arrayOf( + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, + BodyPart.HIP, + ), + BodyPart.UPPER_WAIST to arrayOf( + BodyPart.LOWER_WAIST, + BodyPart.HIP, + ), + BodyPart.LOWER_WAIST to arrayOf( + BodyPart.HIP, + ), + + // Upper leg will fall back on lower leg + BodyPart.LEFT_UPPER_LEG to arrayOf(BodyPart.LEFT_LOWER_LEG, BodyPart.LEFT_FOOT), + BodyPart.RIGHT_UPPER_LEG to arrayOf(BodyPart.RIGHT_LOWER_LEG, BodyPart.RIGHT_FOOT), + BodyPart.LEFT_LOWER_LEG to arrayOf(BodyPart.LEFT_FOOT), + BodyPart.RIGHT_LOWER_LEG to arrayOf(BodyPart.RIGHT_FOOT), +) + +/** + * Handles replacing rotations and accelerations of boneInputs that are not actively receiving data by + * falling back to their parents or children. + * + * This does not produce reliable data. For that, see the direct link processor. + */ +class FallbackInputProcessor : SkeletonInputProcessor { + override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { + mutableInputSkeleton.forEachBone { bodyPart, bone -> + if (bone.isRotationActive && bone.isAccelerationActive) return@forEachBone + val newRotation = if (!bone.isRotationActive) { + val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true } + ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true } + mutableInputSkeleton[fallbackPart]?.rotation?.eulerHeading() ?: bone.rotation + } else { + bone.rotation + } + val newAcceleration = if (!bone.isAccelerationActive) { + val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isAccelerationActive == true } + ?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isAccelerationActive == true } + mutableInputSkeleton[fallbackPart]?.acceleration ?: bone.acceleration + } else { + bone.acceleration + } + mutableInputSkeleton[bodyPart] = bone.copy(rotation = newRotation, acceleration = newAcceleration) + } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/module.kt b/server/core/src/main/java/dev/slimevr/skeleton/module.kt index e144705d7..2863bd445 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -8,10 +8,9 @@ import dev.slimevr.skeleton.computedprocessors.VelocityComputedProcessor import dev.slimevr.skeleton.fkprocessors.FootPlantFkProcessor import dev.slimevr.skeleton.fkprocessors.LocalizerFkProcessor import dev.slimevr.skeleton.fkprocessors.ToeSnapFkProcessor -import dev.slimevr.skeleton.inputprocessors.AccelerationFallbackInputProcessor -import dev.slimevr.skeleton.inputprocessors.BoneDirectLinkInputProcessor -import dev.slimevr.skeleton.inputprocessors.BoneYawFallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.ConstraintInputProcessor +import dev.slimevr.skeleton.inputprocessors.DirectLinkInputProcessor +import dev.slimevr.skeleton.inputprocessors.FallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.FingersInputProcessor import dev.slimevr.skeleton.inputprocessors.HeadPositionFallbackProcessor import dev.slimevr.skeleton.inputprocessors.HipYawRollAlignInputProcessor @@ -277,15 +276,14 @@ class Skeleton( RotationSmoothingInputProcessor(settings), TrackerOffsetInputProcessor(), HeadPositionFallbackProcessor(settings), - BoneYawFallbackInputProcessor(), + FallbackInputProcessor(), SpineInputProcessor(settings), HipYawRollAlignInputProcessor(settings), UpperLegsRollAlignInputProcessor(settings), - BoneDirectLinkInputProcessor(), + DirectLinkInputProcessor(), FingersInputProcessor(), ToesInputProcessor(), ConstraintInputProcessor(settings), - AccelerationFallbackInputProcessor(), ), // Run on the result of FK and persist fkComputedProcessors = listOf(