Constrains and RotationFallback processors

This commit is contained in:
loucass003 committed 2026-09-15 03:19:30 +02:00
1 parent 4b4aa89f37
commit efeada35fa
22 files changed
+296 -301

No files matched your search

@@ -53,9 +53,6 @@ inline fun <V : Any> BodyPartMap<V>.forEachBone(action: (BodyPart, V) -> Unit) {
}
}
/** Converts a [BodyPartMap] to a plain [BoneId]-keyed [Map]; no registry needed since every key is standard. */
fun <V : Any> BodyPartMap<V>.resolveToBoneIds(): Map<BoneId, V> = mapKeys { it.key.boneId }
/**
* Dense array-backed map keyed by [BoneId], scoped to the [registry] it was built for.
* IDs run `1..registry.maxId`, slot 0 is unused.
@@ -49,14 +49,23 @@ data class CompiledOffset(val base: Vector3, val terms: List<CompiledOffsetTerm>
class BoneOffsets(val tail: BoneMap<Vector3>, val head: BoneMap<Vector3>)
/**
* The compiled bone registry, proportion catalog, and per-bone offset produced by
* [compileResourcePacks].
* The compiled bone registry, proportion catalog, and per-bone offset, constraint, and
* rotation-fallback rule produced by [compileResourcePacks]. [copyRotationFallbacks] (bone to
* source) and [firstActiveRotationFallbacks] (bone to source list) are each in
* ancestor-before-descendant order on their own; every core bone's `copy` source that itself needs
* resolving first is its own parent, and a `firstActive` bone's only source that ever needs
* resolving first is its parent foot, both always a `copy` bone, so running every `copy` fallback
* before any `firstActive` one is enough to keep both schedules correctly ordered relative to
* each other without interleaving them into one.
*/
class CompiledSkeleton(
val registry: BoneRegistry,
val proportions: Map<String, CompiledProportion>,
private val tailOffsets: BoneMap<CompiledOffset>,
private val headOffsets: BoneMap<CompiledOffset>,
val constraints: BoneMap<Constraint>,
val copyRotationFallbacks: List<Pair<BoneId, BoneId>>,
val firstActiveRotationFallbacks: List<Pair<BoneId, List<BoneId>>>,
) {
/** Every proportion's default value at [height], keyed by proportion key. */
fun defaultProportionValues(height: Float = REFERENCE_HEIGHT): Map<String, Float> {
@@ -1,4 +1,4 @@
package dev.slimevr.skeleton
package dev.slimevr.bones
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
@@ -1,5 +1,8 @@
package dev.slimevr.bones
import dev.slimevr.resourcepacks.CopyRotationFallback
import dev.slimevr.resourcepacks.FirstActiveRotationFallback
import dev.slimevr.resourcepacks.NoRotationFallback
import dev.slimevr.resourcepacks.Offset
import dev.slimevr.resourcepacks.ParsedResourcePack
import dev.slimevr.resourcepacks.ProportionDefinition
@@ -7,7 +10,12 @@ import dev.slimevr.resourcepacks.ResourcePackCatalog
import dev.slimevr.resourcepacks.SourcedResource
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.connection.BoneDefinition
import com.jme3.math.FastMath.DEG_TO_RAD as degToRad
import dev.slimevr.resourcepacks.BoneDefinition as PackBoneDefinition
import dev.slimevr.resourcepacks.Constraint as PackConstraint
import dev.slimevr.resourcepacks.HingeConstraint as PackHingeConstraint
import dev.slimevr.resourcepacks.LooseHingeConstraint as PackLooseHingeConstraint
import dev.slimevr.resourcepacks.TwistSwingConstraint as PackTwistSwingConstraint
import solarxr_protocol.connection.BoneRegistry as WireBoneRegistry
data class ResourcePackCompilationDiagnostic(val packId: String, val path: String, val message: String)
@@ -135,15 +143,68 @@ fun compileResourcePacks(catalog: ResourcePackCatalog): CompiledSkeleton {
val tailOffsets = BoneMap.of<CompiledOffset>(registry)
val headOffsets = BoneMap.of<CompiledOffset>(registry)
val constraints = BoneMap.of<Constraint>(registry)
val copyFallbacksByBoneId = mutableMapOf<BoneId, Pair<BoneContribution, CopyRotationFallback>>()
val firstActiveFallbacksByBoneId = mutableMapOf<BoneId, Pair<BoneContribution, FirstActiveRotationFallback>>()
for (contribution in allContributions) {
val definition = contribution.resource.value
val boneId = registry[definition.key] ?: continue
definition.tailOffset?.let { tailOffsets[boneId] = compileOffset(it, contribution, proportionsByKey, diagnostics) }
definition.headOffset?.let { headOffsets[boneId] = compileOffset(it, contribution, proportionsByKey, diagnostics) }
definition.constraint?.let { constraints[boneId] = compileConstraint(it) }
when (val fallback = definition.rotationFallback) {
null, is NoRotationFallback -> {}
is CopyRotationFallback -> copyFallbacksByBoneId[boneId] = contribution to fallback
is FirstActiveRotationFallback -> firstActiveFallbacksByBoneId[boneId] = contribution to fallback
}
}
val hierarchyOrder = registry.hierarchyFrom(registry.root).map { it.second }
val copyRotationFallbacks = hierarchyOrder.mapNotNull { boneId ->
copyFallbacksByBoneId[boneId]?.let { (contribution, fallback) -> boneId to resolveFallbackBone(fallback.source, contribution, registry, diagnostics) }
}
val firstActiveRotationFallbacks = hierarchyOrder.mapNotNull { boneId ->
firstActiveFallbacksByBoneId[boneId]?.let { (contribution, fallback) ->
boneId to fallback.sources.map { resolveFallbackBone(it, contribution, registry, diagnostics) }
}
}
if (diagnostics.isNotEmpty()) throw ResourcePackCompilationException(diagnostics)
return CompiledSkeleton(registry, proportions, tailOffsets, headOffsets)
return CompiledSkeleton(registry, proportions, tailOffsets, headOffsets, constraints, copyRotationFallbacks, firstActiveRotationFallbacks)
}
private fun compileConstraint(constraint: PackConstraint): Constraint = when (constraint) {
is PackTwistSwingConstraint -> TwistSwingConstraint(
twist = constraint.twistDegrees * degToRad,
swing = constraint.swingDegrees * degToRad,
allowedDeviation = (constraint.allowedDeviationDegrees ?: 0f) * degToRad,
)
is PackHingeConstraint -> HingeConstraint(
min = constraint.minDegrees * degToRad,
max = constraint.maxDegrees * degToRad,
hingeAxis = Vector3(constraint.axis.x, constraint.axis.y, constraint.axis.z),
)
is PackLooseHingeConstraint -> LooseHingeConstraint(
min = constraint.minDegrees * degToRad,
max = constraint.maxDegrees * degToRad,
allowedDeviation = constraint.allowedDeviationDegrees * degToRad,
hingeAxis = Vector3(constraint.axis.x, constraint.axis.y, constraint.axis.z),
)
}
/** Resolves one rotation-fallback source key (or the `"parent"` shorthand) to a [BoneId]. */
private fun resolveFallbackBone(
key: String,
contribution: BoneContribution,
registry: BoneRegistry,
diagnostics: MutableList<ResourcePackCompilationDiagnostic>,
): BoneId {
val target = if (key == "parent") contribution.resource.value.parent else key
val boneId = target?.let { registry[it] }
if (boneId != null) return boneId
diagnostics += ResourcePackCompilationDiagnostic(contribution.pack.manifest.value.id, contribution.resource.path, "Unknown bone '$key' in rotationFallback")
return BoneId(0u)
}
private fun compileOffset(
@@ -17,22 +17,19 @@ data class Offset(val base: Vector3? = null, val terms: List<OffsetTerm>? = null
@Serializable
@JsonClassDiscriminator("type")
sealed interface RotationFallback {
val before: List<String>?
val after: List<String>?
}
sealed interface RotationFallback
@Serializable
@SerialName("none")
data class NoRotationFallback(override val before: List<String>? = null, override val after: List<String>? = null) : RotationFallback
data object NoRotationFallback : RotationFallback
@Serializable
@SerialName("copy")
data class CopyRotationFallback(val source: String, override val before: List<String>? = null, override val after: List<String>? = null) : RotationFallback
data class CopyRotationFallback(val source: String) : RotationFallback
@Serializable
@SerialName("firstActive")
data class FirstActiveRotationFallback(val sources: List<String>, override val before: List<String>? = null, override val after: List<String>? = null) : RotationFallback
data class FirstActiveRotationFallback(val sources: List<String>) : RotationFallback
@Serializable
@JsonClassDiscriminator("type")
@@ -6,7 +6,6 @@ import dev.slimevr.bones.BoneSet
import dev.slimevr.bones.CompiledSkeleton
import dev.slimevr.bones.boneId
import dev.slimevr.bones.mutateCopy
import dev.slimevr.bones.resolveToBoneIds
import dev.slimevr.config.Settings
import dev.slimevr.config.UserConfig
import dev.slimevr.logging.AppLogger
@@ -172,6 +171,7 @@ private class TickTimings(private val hz: Int, private val window: Duration, pri
class ComputedSkeletonBehaviour(
val hz: Int,
val definition: CompiledSkeleton,
val inputProcessors: List<SkeletonInputProcessor> = emptyList(),
val fkComputedProcessors: List<SkeletonComputedProcessor> = emptyList(),
val fkProcessors: List<SkeletonFkProcessor> = emptyList(),
@@ -181,7 +181,7 @@ class ComputedSkeletonBehaviour(
) : SkeletonBehaviour {
private val intervalDuration = (1.0 / hz).seconds
private val headId = BodyPart.HEAD.boneId
private val constraints = BODY_PART_CONSTRAINT_MAP.resolveToBoneIds()
private val constraints = definition.constraints
private val ikChains = resolveIkChains()
/** Shortest gap between two tick starts before the later one is pushed to the next slot */
@@ -1,151 +0,0 @@
package dev.slimevr.skeleton
import com.jme3.math.FastMath
import dev.slimevr.bones.BodyPart
import dev.slimevr.bones.BodyPartMap
import dev.slimevr.bones.BoneId
import io.github.axisangles.ktmath.Quaternion
// TODO Actually figure out good values for these constraints, maybe a source would be
// good?
val BODY_PART_CONSTRAINT_MAP: BodyPartMap<Constraint> = BodyPartMap(
mapOf(
BodyPart.LEFT_SHOULDER to TwistSwingConstraint(
0f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_UPPER_ARM to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
180f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_LOWER_ARM to LooseHingeConstraint(
-180f * FastMath.DEG_TO_RAD,
0f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_HAND to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_SHOULDER to TwistSwingConstraint(
0f * FastMath.DEG_TO_RAD,
30f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_UPPER_ARM to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
180f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_LOWER_ARM to LooseHingeConstraint(
-180f * FastMath.DEG_TO_RAD,
0f * FastMath.DEG_TO_RAD,
40f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_HAND to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.UPPER_CHEST to TwistSwingConstraint(
90f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_CHEST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.UPPER_WAIST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.LOWER_WAIST to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.HIP to TwistSwingConstraint(
60f * FastMath.DEG_TO_RAD,
120f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_UPPER_LEG to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_LOWER_LEG to LooseHingeConstraint(
-5f * FastMath.DEG_TO_RAD,
180f * FastMath.DEG_TO_RAD,
10f * FastMath.DEG_TO_RAD,
),
BodyPart.LEFT_FOOT to LooseHingeConstraint(
-60f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
60f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_UPPER_LEG to TwistSwingConstraint(
120f * FastMath.DEG_TO_RAD,
170f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_LOWER_LEG to LooseHingeConstraint(
-5f * FastMath.DEG_TO_RAD,
180f * FastMath.DEG_TO_RAD,
10f * FastMath.DEG_TO_RAD,
),
BodyPart.RIGHT_FOOT to LooseHingeConstraint(
-60f * FastMath.DEG_TO_RAD,
90f * FastMath.DEG_TO_RAD,
60f * FastMath.DEG_TO_RAD,
),
),
)
fun constrainOffset(
constraint: Constraint,
parent: Quaternion,
bone: Quaternion,
offset: Quaternion,
): Quaternion {
// TODO: Ensure the quaternion multiplication order is correct here
return constraint.apply(parent, offset * bone) * bone.inv()
}
fun constrainOffsetWithSkeleton(
boneId: BoneId,
offset: Quaternion,
bones: ComputedSkeleton,
constraints: Map<BoneId, Constraint>,
): Quaternion {
val registry = bones.registry
val constraint = constraints[boneId] ?: return offset
val boneRot = bones[boneId]?.rotation ?: return offset
val parentRot = registry.parentOf(boneId)?.let { parent ->
bones[parent]?.rotation
} ?: Quaternion.IDENTITY
return constrainOffset(
constraint,
parentRot,
boneRot,
offset,
)
}
fun constrainSkeleton(
bones: InputSkeleton,
constraints: Map<BoneId, Constraint>,
): InputSkeleton {
val registry = bones.registry
// Apply constraints top-down, from the root
for ((parentId, boneId) in registry.hierarchyFrom(registry.root)) {
val bone = bones[boneId] ?: continue
val constraint = constraints[boneId] ?: continue
val parentRotation = parentId?.let { bones[it]?.rotation } ?: Quaternion.IDENTITY
bones[boneId] = bone.copy(
rotation = constraint.apply(
parentRotation,
bone.rotation,
),
)
}
return bones
}
@@ -0,0 +1,56 @@
package dev.slimevr.skeleton
import dev.slimevr.bones.BoneId
import dev.slimevr.bones.Constraint
import io.github.axisangles.ktmath.Quaternion
fun constrainOffset(
constraint: Constraint,
parent: Quaternion,
bone: Quaternion,
offset: Quaternion,
): Quaternion {
// TODO: Ensure the quaternion multiplication order is correct here
return constraint.apply(parent, offset * bone) * bone.inv()
}
fun constrainOffsetWithSkeleton(
boneId: BoneId,
offset: Quaternion,
bones: ComputedSkeleton,
constraints: Map<BoneId, Constraint>,
): Quaternion {
val registry = bones.registry
val constraint = constraints[boneId] ?: return offset
val boneRot = bones[boneId]?.rotation ?: return offset
val parentRot = registry.parentOf(boneId)?.let { parent ->
bones[parent]?.rotation
} ?: Quaternion.IDENTITY
return constrainOffset(
constraint,
parentRot,
boneRot,
offset,
)
}
fun constrainSkeleton(
bones: InputSkeleton,
constraints: Map<BoneId, Constraint>,
): InputSkeleton {
val registry = bones.registry
// Apply constraints top-down, from the root
for ((parentId, boneId) in registry.hierarchyFrom(registry.root)) {
val bone = bones[boneId] ?: continue
val constraint = constraints[boneId] ?: continue
val parentRotation = parentId?.let { bones[it]?.rotation } ?: Quaternion.IDENTITY
bones[boneId] = bone.copy(
rotation = constraint.apply(
parentRotation,
bone.rotation,
),
)
}
return bones
}
@@ -3,6 +3,7 @@ package dev.slimevr.skeleton
import com.jme3.math.FastMath
import dev.slimevr.bones.BoneId
import dev.slimevr.bones.BoneSet
import dev.slimevr.bones.Constraint
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
@@ -1,49 +0,0 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.bones.BodyPart
import dev.slimevr.bones.BoneId
import dev.slimevr.bones.boneId
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
/**
* Handles setting the rotation of an inactive bone with its source bone.
*/
class BoneDirectLinkInputProcessor : SkeletonInputProcessor {
/**
* First element is the linked bone.
*
* Second element is the bone the first element is linked to.
*/
private val linkedToSource: Array<Pair<BoneId, BoneId>> = arrayOf(
BodyPart.HEAD to BodyPart.NECK,
BodyPart.NECK to BodyPart.HEAD,
BodyPart.LEFT_FOOT to BodyPart.LEFT_LOWER_LEG,
BodyPart.RIGHT_FOOT to BodyPart.RIGHT_LOWER_LEG,
BodyPart.LEFT_SHOULDER to BodyPart.UPPER_CHEST,
BodyPart.RIGHT_SHOULDER to BodyPart.UPPER_CHEST,
BodyPart.LEFT_UPPER_ARM to BodyPart.LEFT_SHOULDER,
BodyPart.RIGHT_UPPER_ARM to BodyPart.RIGHT_SHOULDER,
BodyPart.LEFT_LOWER_ARM to BodyPart.LEFT_UPPER_ARM,
BodyPart.RIGHT_LOWER_ARM to BodyPart.RIGHT_UPPER_ARM,
BodyPart.LEFT_HAND to BodyPart.LEFT_LOWER_ARM,
BodyPart.RIGHT_HAND to BodyPart.RIGHT_LOWER_ARM,
).map { (bodyPart, source) -> bodyPart.boneId to source.boneId }.toTypedArray()
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
for ((boneId, sourceId) in linkedToSource) {
val bone = mutableInputSkeleton[boneId] ?: continue
if (bone.isRotationActive) continue
val rotation = mutableInputSkeleton[sourceId]?.rotation ?: continue
if (rotation == bone.rotation) continue
mutableInputSkeleton[boneId] = bone.copy(rotation = rotation)
}
}
}
@@ -1,18 +1,16 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.bones.BoneId
import dev.slimevr.bones.resolveToBoneIds
import dev.slimevr.bones.CompiledSkeleton
import dev.slimevr.config.Settings
import dev.slimevr.skeleton.BODY_PART_CONSTRAINT_MAP
import dev.slimevr.skeleton.Constraint
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
import dev.slimevr.skeleton.constrainSkeleton
class ConstraintInputProcessor(
val settings: Settings,
definition: CompiledSkeleton,
) : SkeletonInputProcessor {
private val constraints: Map<BoneId, Constraint> = BODY_PART_CONSTRAINT_MAP.resolveToBoneIds()
private val constraints = definition.constraints
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
val skeletonConfig = settings.context.state.value.data.skeletonConfig
@@ -0,0 +1,23 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.bones.BoneId
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
/**
* Copies another bone's rotation onto a bone that has no live tracker of its own. [schedule] pairs
* a bone with its source, in ancestor-before-descendant order, so a source that itself needed
* copying this pass is already resolved by the time it's read.
*/
class CopyRotationFallbackInputProcessor(
private val schedule: List<Pair<BoneId, BoneId>>,
) : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
for ((boneId, source) in schedule) {
val bone = mutableInputSkeleton[boneId] ?: continue
if (bone.isRotationActive) continue
val rotation = mutableInputSkeleton[source]?.rotation ?: continue
mutableInputSkeleton[boneId] = bone.copy(rotation = rotation)
}
}
}
@@ -0,0 +1,23 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.bones.BoneId
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
/**
* Sets an untracked bone's rotation from the first actively-tracked bone in its source list,
* falling back to the list's last entry regardless of its activity if none are active.
*/
class FirstActiveRotationFallbackInputProcessor(
private val schedule: List<Pair<BoneId, List<BoneId>>>,
) : SkeletonInputProcessor {
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
for ((boneId, sources) in schedule) {
val bone = mutableInputSkeleton[boneId] ?: continue
if (bone.isRotationActive) continue
val active = sources.firstNotNullOfOrNull { mutableInputSkeleton[it]?.takeIf { b -> b.isRotationActive } }
val rotation = (active ?: sources.lastOrNull()?.let { mutableInputSkeleton[it] })?.rotation ?: continue
mutableInputSkeleton[boneId] = bone.copy(rotation = rotation)
}
}
}
@@ -1,48 +0,0 @@
package dev.slimevr.skeleton.inputprocessors
import dev.slimevr.bones.BodyPart
import dev.slimevr.bones.BoneId
import dev.slimevr.bones.boneId
import dev.slimevr.skeleton.InputSkeleton
import dev.slimevr.skeleton.SkeletonInputProcessor
/**
* Handles setting the rotation of an inactive toe bone with the first active toe bone in its sources, or keeps
* the old rotation if none of them are active.
*/
class ToeActiveLinkInputProcessor : SkeletonInputProcessor {
/**
* First element is the bone whose BoneInput is not actively receiving data.
*
* Second element contains a list of bones whose rotation should be used as a fallback prioritized from first to last.
* First active bone will be prioritized. If none are active, the last bone is used.
*/
private val toeToSources: Array<Pair<BoneId, Array<BoneId>>> = arrayOf(
BodyPart.LEFT_BIG_TOE to arrayOf(BodyPart.LEFT_INDEX_TOE, BodyPart.LEFT_MIDDLE_TOE, BodyPart.LEFT_RING_TOE, BodyPart.LEFT_LITTLE_TOE, BodyPart.LEFT_FOOT),
BodyPart.LEFT_INDEX_TOE to arrayOf(BodyPart.LEFT_MIDDLE_TOE, BodyPart.LEFT_RING_TOE, BodyPart.LEFT_LITTLE_TOE, BodyPart.LEFT_BIG_TOE, BodyPart.LEFT_FOOT),
BodyPart.LEFT_MIDDLE_TOE to arrayOf(BodyPart.LEFT_INDEX_TOE, BodyPart.LEFT_RING_TOE, BodyPart.LEFT_LITTLE_TOE, BodyPart.LEFT_BIG_TOE, BodyPart.LEFT_FOOT),
BodyPart.LEFT_RING_TOE to arrayOf(BodyPart.LEFT_MIDDLE_TOE, BodyPart.LEFT_INDEX_TOE, BodyPart.LEFT_LITTLE_TOE, BodyPart.LEFT_BIG_TOE, BodyPart.LEFT_FOOT),
BodyPart.LEFT_LITTLE_TOE to arrayOf(BodyPart.LEFT_RING_TOE, BodyPart.LEFT_MIDDLE_TOE, BodyPart.LEFT_INDEX_TOE, BodyPart.LEFT_BIG_TOE, BodyPart.LEFT_FOOT),
BodyPart.RIGHT_BIG_TOE to arrayOf(BodyPart.RIGHT_INDEX_TOE, BodyPart.RIGHT_MIDDLE_TOE, BodyPart.RIGHT_RING_TOE, BodyPart.RIGHT_LITTLE_TOE, BodyPart.RIGHT_FOOT),
BodyPart.RIGHT_INDEX_TOE to arrayOf(BodyPart.RIGHT_MIDDLE_TOE, BodyPart.RIGHT_RING_TOE, BodyPart.RIGHT_LITTLE_TOE, BodyPart.RIGHT_BIG_TOE, BodyPart.RIGHT_FOOT),
BodyPart.RIGHT_MIDDLE_TOE to arrayOf(BodyPart.RIGHT_INDEX_TOE, BodyPart.RIGHT_RING_TOE, BodyPart.RIGHT_LITTLE_TOE, BodyPart.RIGHT_BIG_TOE, BodyPart.RIGHT_FOOT),
BodyPart.RIGHT_RING_TOE to arrayOf(BodyPart.RIGHT_MIDDLE_TOE, BodyPart.RIGHT_INDEX_TOE, BodyPart.RIGHT_LITTLE_TOE, BodyPart.RIGHT_BIG_TOE, BodyPart.RIGHT_FOOT),
BodyPart.RIGHT_LITTLE_TOE to arrayOf(BodyPart.RIGHT_RING_TOE, BodyPart.RIGHT_MIDDLE_TOE, BodyPart.RIGHT_INDEX_TOE, BodyPart.RIGHT_BIG_TOE, BodyPart.RIGHT_FOOT),
).map { (bodyPart, sources) ->
bodyPart.boneId to sources.map { it.boneId }.toTypedArray()
}.toTypedArray()
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
for ((boneId, sources) in toeToSources) {
val bone = mutableInputSkeleton[boneId] ?: continue
if (bone.isRotationActive) continue
val closestActiveBoneOrLast = sources.firstNotNullOfOrNull { id ->
mutableInputSkeleton[id]?.takeIf { it.isRotationActive }
} ?: sources.lastOrNull()?.let { mutableInputSkeleton[it] } ?: continue
mutableInputSkeleton[boneId] = bone.copy(rotation = closestActiveBoneOrLast.rotation)
}
}
}
@@ -15,16 +15,16 @@ 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.BoneDirectLinkInputProcessor
import dev.slimevr.skeleton.inputprocessors.BoneYawFallbackInputProcessor
import dev.slimevr.skeleton.inputprocessors.ConstraintInputProcessor
import dev.slimevr.skeleton.inputprocessors.CopyRotationFallbackInputProcessor
import dev.slimevr.skeleton.inputprocessors.FingerImputeInputProcessor
import dev.slimevr.skeleton.inputprocessors.FirstActiveRotationFallbackInputProcessor
import dev.slimevr.skeleton.inputprocessors.HeadPositionFallbackProcessor
import dev.slimevr.skeleton.inputprocessors.HipYawRollAlignInputProcessor
import dev.slimevr.skeleton.inputprocessors.PredictionInputProcessor
import dev.slimevr.skeleton.inputprocessors.SmoothingInputProcessor
import dev.slimevr.skeleton.inputprocessors.SpineInputProcessor
import dev.slimevr.skeleton.inputprocessors.ToeActiveLinkInputProcessor
import dev.slimevr.skeleton.inputprocessors.UpperLegsRollAlignInputProcessor
import dev.slimevr.skeleton.targetprocessors.FloorClipTargetProcessor
import dev.slimevr.skeleton.targetprocessors.SkatingCorrectionTargetProcessor
@@ -234,6 +234,7 @@ class Skeleton(
// YouSpinMeRightRoundBehaviour(inputHz = 50f),
ComputedSkeletonBehaviour(
hz = hz,
definition = definition,
waiter = waiter,
inputProcessors = listOf(
PredictionInputProcessor(settings).also { resettableSkeletonProcessors.add(it) },
@@ -243,10 +244,10 @@ class Skeleton(
SpineInputProcessor(settings),
HipYawRollAlignInputProcessor(settings),
UpperLegsRollAlignInputProcessor(settings),
BoneDirectLinkInputProcessor(),
CopyRotationFallbackInputProcessor(definition.copyRotationFallbacks),
FingerImputeInputProcessor(),
ToeActiveLinkInputProcessor(),
ConstraintInputProcessor(settings),
FirstActiveRotationFallbackInputProcessor(definition.firstActiveRotationFallbacks),
ConstraintInputProcessor(settings, definition),
),
fkComputedProcessors = listOf(
VelocityComputedProcessor().also { resettableSkeletonProcessors.add(it) },
@@ -3,7 +3,7 @@
"key": "slimevr:head",
"nameKey": "slimevr:bone.head",
"tailOffset": {"terms": [{"proportion": "slimevr:head", "direction": {"x": 0, "y": 0, "z": 1}}]},
"rotationFallback": {"type": "copy", "source": "slimevr:neck", "before": ["slimevr:neck"]},
"rotationFallback": {"type": "copy", "source": "slimevr:neck"},
"outputs": {
"vmc": {"name": "Head"},
"vrchat": {
@@ -74,14 +74,6 @@
"source": {
"description": "Bone to copy from when this bone is untracked. `parent` refers to this bone's own `parent` field rather than repeating its key.",
"anyOf": [{"const": "parent"}, {"$ref": "common.schema.json#/$defs/key"}]
},
"before": {
"description": "Run this bone's fallback before the fallbacks owned by these bones, when both participate in the same order-dependent write cycle (e.g. two bones whose fallbacks copy each other).",
"$ref": "common.schema.json#/$defs/orderReferences"
},
"after": {
"description": "Run this bone's fallback after the fallbacks owned by these bones, when both participate in the same order-dependent write cycle.",
"$ref": "common.schema.json#/$defs/orderReferences"
}
}
},
@@ -100,14 +92,6 @@
"uniqueItems": true,
"description": "Ordered candidates. The first one with a live, active tracker is used. If none are active, the final entry is used unconditionally, so a bone intended as the ultimate fallback (e.g. the parent foot for a toe) belongs last.",
"items": {"anyOf": [{"const": "parent"}, {"$ref": "common.schema.json#/$defs/key"}]}
},
"before": {
"description": "Run this bone's fallback before the fallbacks owned by these bones, when both participate in the same order-dependent write cycle.",
"$ref": "common.schema.json#/$defs/orderReferences"
},
"after": {
"description": "Run this bone's fallback after the fallbacks owned by these bones, when both participate in the same order-dependent write cycle.",
"$ref": "common.schema.json#/$defs/orderReferences"
}
}
}
@@ -3,12 +3,6 @@
"$defs": {
"key": {"type": "string", "pattern": "^[a-z0-9][a-z0-9_.-]*:[a-z0-9][a-z0-9_./-]*$"},
"translationKey": {"type": "string", "pattern": "^[a-z0-9][a-z0-9_.-]*:[a-z0-9][a-z0-9_.-]*$"},
"orderReferences": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {"$ref": "#/$defs/key"}
},
"vector": {
"type": "object",
"additionalProperties": false,
@@ -1,4 +1,4 @@
package dev.slimevr.skeleton
package dev.slimevr.bones
import com.jme3.math.FastMath
import dev.slimevr.quaternionAssertEquals
@@ -1,11 +1,13 @@
package dev.slimevr.bones
import com.jme3.math.FastMath.DEG_TO_RAD
import dev.slimevr.resourcepacks.ClasspathResourcePackSource
import dev.slimevr.resourcepacks.InMemoryResourcePackSource
import dev.slimevr.resourcepacks.ResourcePackCatalog
import dev.slimevr.resourcepacks.ResourcePackParser
import io.github.axisangles.ktmath.Vector3
import kotlinx.coroutines.test.runTest
import solarxr_protocol.datatypes.BodyPart
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@@ -84,6 +86,103 @@ class ResourcePackCompilerTest {
for (key in defaults.keys) assertFloatEquals(defaults.getValue(key), recovered.getValue(key), key)
}
@Test
fun `bundled core pack compiles constraints matching the retired hardcoded table`() = runTest {
val core = ResourcePackParser().parse(ClasspathResourcePackSource.core(javaClass.classLoader))
val definition = compileResourcePacks(catalog(core))
val constraints = definition.constraints
assertEquals(19, constraints.entries.size)
fun twistSwing(part: BodyPart, twist: Float, swing: Float) {
val c = constraints[definition.registry[part.key]!!] as TwistSwingConstraint
assertFloatEquals(twist * DEG_TO_RAD, c.twist, "${part.key} twist")
assertFloatEquals(swing * DEG_TO_RAD, c.swing, "${part.key} swing")
}
fun looseHinge(part: BodyPart, min: Float, max: Float, allowedDeviation: Float) {
val c = constraints[definition.registry[part.key]!!] as LooseHingeConstraint
assertFloatEquals(min * DEG_TO_RAD, c.min, "${part.key} min")
assertFloatEquals(max * DEG_TO_RAD, c.max, "${part.key} max")
assertFloatEquals(allowedDeviation * DEG_TO_RAD, c.allowedDeviation, "${part.key} allowedDeviation")
}
twistSwing(BodyPart.LEFT_SHOULDER, 0f, 30f)
twistSwing(BodyPart.LEFT_UPPER_ARM, 120f, 180f)
looseHinge(BodyPart.LEFT_LOWER_ARM, -180f, 0f, 40f)
twistSwing(BodyPart.LEFT_HAND, 120f, 120f)
twistSwing(BodyPart.UPPER_CHEST, 90f, 120f)
twistSwing(BodyPart.LOWER_CHEST, 60f, 120f)
twistSwing(BodyPart.UPPER_WAIST, 60f, 120f)
twistSwing(BodyPart.LOWER_WAIST, 60f, 120f)
twistSwing(BodyPart.HIP, 60f, 120f)
twistSwing(BodyPart.LEFT_UPPER_LEG, 120f, 170f)
looseHinge(BodyPart.LEFT_LOWER_LEG, -5f, 180f, 10f)
looseHinge(BodyPart.LEFT_FOOT, -60f, 90f, 60f)
}
@Test
fun `bundled core pack's copy rotation-fallback schedule matches the retired BoneDirectLinkInputProcessor table`() = runTest {
val core = ResourcePackParser().parse(ClasspathResourcePackSource.core(javaClass.classLoader))
val definition = compileResourcePacks(catalog(core))
val schedule = definition.copyRotationFallbacks
val registry = definition.registry
val expectedBones = setOf(
BodyPart.HEAD, BodyPart.NECK,
BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT,
BodyPart.LEFT_SHOULDER, BodyPart.RIGHT_SHOULDER,
BodyPart.LEFT_UPPER_ARM, BodyPart.RIGHT_UPPER_ARM,
BodyPart.LEFT_LOWER_ARM, BodyPart.RIGHT_LOWER_ARM,
BodyPart.LEFT_HAND, BodyPart.RIGHT_HAND,
).map { registry[it.key]!! }.toSet()
assertEquals(expectedBones, schedule.map { it.first }.toSet())
val order = schedule.map { it.first }
fun indexOf(part: BodyPart) = order.indexOf(registry[part.key]!!)
assertTrue(indexOf(BodyPart.HEAD) < indexOf(BodyPart.NECK), "head before neck")
assertTrue(indexOf(BodyPart.LEFT_SHOULDER) < indexOf(BodyPart.LEFT_UPPER_ARM), "shoulder before upper_arm")
assertTrue(indexOf(BodyPart.LEFT_UPPER_ARM) < indexOf(BodyPart.LEFT_LOWER_ARM), "upper_arm before lower_arm")
assertTrue(indexOf(BodyPart.LEFT_LOWER_ARM) < indexOf(BodyPart.LEFT_HAND), "lower_arm before hand")
val headFallback = schedule.first { it.first == registry[BodyPart.HEAD.key]!! }
assertEquals(registry[BodyPart.NECK.key]!!, headFallback.second)
}
@Test
fun `bundled core pack's firstActive rotation-fallback schedule matches the retired ToeActiveLinkInputProcessor table`() = runTest {
val core = ResourcePackParser().parse(ClasspathResourcePackSource.core(javaClass.classLoader))
val definition = compileResourcePacks(catalog(core))
val schedule = definition.firstActiveRotationFallbacks
val registry = definition.registry
val expectedBones = setOf(
BodyPart.LEFT_BIG_TOE, BodyPart.LEFT_INDEX_TOE, BodyPart.LEFT_MIDDLE_TOE, BodyPart.LEFT_RING_TOE, BodyPart.LEFT_LITTLE_TOE,
BodyPart.RIGHT_BIG_TOE, BodyPart.RIGHT_INDEX_TOE, BodyPart.RIGHT_MIDDLE_TOE, BodyPart.RIGHT_RING_TOE, BodyPart.RIGHT_LITTLE_TOE,
).map { registry[it.key]!! }.toSet()
assertEquals(expectedBones, schedule.map { it.first }.toSet())
// left_foot itself is copy-type, not firstActive, so it's resolved by
// CopyRotationFallbackInputProcessor before this schedule ever runs.
val bigToeFallback = schedule.first { it.first == registry[BodyPart.LEFT_BIG_TOE.key]!! }
assertEquals(registry[BodyPart.LEFT_FOOT.key]!!, bigToeFallback.second.last())
}
@Test
fun `compiler reports an unknown bone named in a rotation fallback`() = runTest {
val core = ResourcePackParser().parse(ClasspathResourcePackSource.core(javaClass.classLoader))
val user = ResourcePackParser().parse(
InMemoryResourcePackSource(
mapOf(
"manifest.json" to manifest("example:extra"),
"data/bones/extra.json" to """{"key":"example:extra","nameKey":"example:bone.extra","parent":"slimevr:head","rotationFallback":{"type":"copy","source":"example:missing"}}""",
"assets/lang/en.json" to """{"example:bone.extra":"Extra bone"}""",
),
),
)
val error = assertFailsWith<ResourcePackCompilationException> { compileResourcePacks(catalog(core, user)) }
assertTrue(error.diagnostics.any { "Unknown bone" in it.message && "rotationFallback" in it.message })
}
@Test
fun `compiler includes user-pack bones after the core registry`() = runTest {
val core = ResourcePackParser().parse(ClasspathResourcePackSource.core(javaClass.classLoader))
@@ -137,7 +137,7 @@ class ResourcePackParserTest {
"manifest.json" to manifest("example:branches"),
"data/bones/branch.json" to """
{"key":"example:branch","nameKey":"example:name",
"rotationFallback":{"type":"firstActive","sources":["example:first","example:last"],"after":["example:after"]},
"rotationFallback":{"type":"firstActive","sources":["example:first","example:last"]},
"constraint":{"type":"hinge","minDegrees":-1,"maxDegrees":1,"axis":{"x":1,"y":0,"z":0}},
"outputs":{"vrchat":{"emit":{"/example":{"from":"rotation","value":[
{"euler":"x"},{"euler":{"order":"YXZ","unit":"radians"}},{"scale":2},{"divide":{"x":2}},{"offset":{"z":1}},{"clamp":[-1,1]},{"greaterThan":0},{"lessThan":2}
@@ -4,7 +4,7 @@ import dev.slimevr.bones.BodyPart
import dev.slimevr.bones.BoneMap
import dev.slimevr.bones.BoneRegistry
import dev.slimevr.bones.boneId
import dev.slimevr.bones.resolveToBoneIds
import dev.slimevr.testCompiledSkeleton
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Vector3
import kotlin.test.Test
@@ -54,7 +54,7 @@ class IKTest {
target,
)
val ikOut = ccdIk(boneInputs, bones, listOf(goal), BODY_PART_CONSTRAINT_MAP.resolveToBoneIds(), 0.01f, 100)
val ikOut = ccdIk(boneInputs, bones, listOf(goal), testCompiledSkeleton.constraints, 0.01f, 100)
assert(ikOut.goalsReached.all { it.value }) {
val boneRots = ikOut.bones.values.joinToString {
"${it.boneId}: ${it.rotation.toEulerAngles(EulerOrder.YZX)}"