Rewrite bone fallback processor

This commit is contained in:
Erimel committed 2026-10-06 14:39:46 -04:00
1 parent dc2452a666
commit ac6b6b85a2
5 files changed
+95 -100

No files matched your search

@@ -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<BodyPart> = 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)
}
}
}
}
}
@@ -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<BodyPart> = 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)
}
}
}
}
}
@@ -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)
}
}
}
@@ -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)
}
}
}
@@ -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(