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 bc673197a..279293a68 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -179,7 +179,7 @@ data class VMCConfig( /** Mirror the tracking before sending it (turn left <=> turn right, left leg <=> right leg) */ val mirrorTracking: Boolean = false, /** Anchor the tracking at the hip (sitting down)? */ - val anchorAtHips: Boolean = false, + val anchorAtHips: Boolean = true, /** JSON part of the VRM to be used */ val vrmJson: String? = null, ) diff --git a/server/core/src/main/java/dev/slimevr/util/side.kt b/server/core/src/main/java/dev/slimevr/util/side.kt index f93d25a72..1aef08561 100644 --- a/server/core/src/main/java/dev/slimevr/util/side.kt +++ b/server/core/src/main/java/dev/slimevr/util/side.kt @@ -1,5 +1,6 @@ package dev.slimevr.util +import dev.slimevr.skeleton.BodyPartMap import solarxr_protocol.datatypes.BodyPart /** @@ -16,65 +17,46 @@ val Side.opposite: Side Side.RIGHT -> Side.LEFT } +private val SIDED_BODY_PART_PAIRS: List> = listOf( + BodyPart.LEFT_UPPER_LEG to BodyPart.RIGHT_UPPER_LEG, + BodyPart.LEFT_LOWER_LEG to BodyPart.RIGHT_LOWER_LEG, + BodyPart.LEFT_FOOT to BodyPart.RIGHT_FOOT, + BodyPart.LEFT_UPPER_ARM to BodyPart.RIGHT_UPPER_ARM, + BodyPart.LEFT_LOWER_ARM to BodyPart.RIGHT_LOWER_ARM, + BodyPart.LEFT_HAND to BodyPart.RIGHT_HAND, + BodyPart.LEFT_SHOULDER to BodyPart.RIGHT_SHOULDER, + BodyPart.LEFT_THUMB_METACARPAL to BodyPart.RIGHT_THUMB_METACARPAL, + BodyPart.LEFT_THUMB_PROXIMAL to BodyPart.RIGHT_THUMB_PROXIMAL, + BodyPart.LEFT_THUMB_DISTAL to BodyPart.RIGHT_THUMB_DISTAL, + BodyPart.LEFT_INDEX_PROXIMAL to BodyPart.RIGHT_INDEX_PROXIMAL, + BodyPart.LEFT_INDEX_INTERMEDIATE to BodyPart.RIGHT_INDEX_INTERMEDIATE, + BodyPart.LEFT_INDEX_DISTAL to BodyPart.RIGHT_INDEX_DISTAL, + BodyPart.LEFT_MIDDLE_PROXIMAL to BodyPart.RIGHT_MIDDLE_PROXIMAL, + BodyPart.LEFT_MIDDLE_INTERMEDIATE to BodyPart.RIGHT_MIDDLE_INTERMEDIATE, + BodyPart.LEFT_MIDDLE_DISTAL to BodyPart.RIGHT_MIDDLE_DISTAL, + BodyPart.LEFT_RING_PROXIMAL to BodyPart.RIGHT_RING_PROXIMAL, + BodyPart.LEFT_RING_INTERMEDIATE to BodyPart.RIGHT_RING_INTERMEDIATE, + BodyPart.LEFT_RING_DISTAL to BodyPart.RIGHT_RING_DISTAL, + BodyPart.LEFT_LITTLE_PROXIMAL to BodyPart.RIGHT_LITTLE_PROXIMAL, + BodyPart.LEFT_LITTLE_INTERMEDIATE to BodyPart.RIGHT_LITTLE_INTERMEDIATE, + BodyPart.LEFT_LITTLE_DISTAL to BodyPart.RIGHT_LITTLE_DISTAL, + BodyPart.LEFT_BIG_TOE to BodyPart.RIGHT_BIG_TOE, + BodyPart.LEFT_INDEX_TOE to BodyPart.RIGHT_INDEX_TOE, + BodyPart.LEFT_MIDDLE_TOE to BodyPart.RIGHT_MIDDLE_TOE, + BodyPart.LEFT_RING_TOE to BodyPart.RIGHT_RING_TOE, + BodyPart.LEFT_LITTLE_TOE to BodyPart.RIGHT_LITTLE_TOE, +) + +private val OPPOSITE_BODY_PARTS: BodyPartMap = BodyPartMap( + SIDED_BODY_PART_PAIRS.flatMap { (left, right) -> listOf(left to right, right to left) }.toMap(), +) + +private val BODY_PART_SIDES: BodyPartMap = BodyPartMap( + SIDED_BODY_PART_PAIRS.flatMap { (left, right) -> listOf(left to Side.LEFT, right to Side.RIGHT) }.toMap(), +) + +val BodyPart.opposite: BodyPart? + get() = OPPOSITE_BODY_PARTS[this] + val BodyPart.side: Side? - get() = when (this) { - BodyPart.LEFT_UPPER_LEG, - BodyPart.LEFT_LOWER_LEG, - BodyPart.LEFT_FOOT, - BodyPart.LEFT_SHOULDER, - BodyPart.LEFT_UPPER_ARM, - BodyPart.LEFT_LOWER_ARM, - BodyPart.LEFT_HAND, - BodyPart.LEFT_THUMB_METACARPAL, - BodyPart.LEFT_THUMB_PROXIMAL, - BodyPart.LEFT_THUMB_DISTAL, - BodyPart.LEFT_INDEX_PROXIMAL, - BodyPart.LEFT_INDEX_INTERMEDIATE, - BodyPart.LEFT_INDEX_DISTAL, - BodyPart.LEFT_MIDDLE_PROXIMAL, - BodyPart.LEFT_MIDDLE_INTERMEDIATE, - BodyPart.LEFT_MIDDLE_DISTAL, - BodyPart.LEFT_RING_PROXIMAL, - BodyPart.LEFT_RING_INTERMEDIATE, - BodyPart.LEFT_RING_DISTAL, - BodyPart.LEFT_LITTLE_PROXIMAL, - BodyPart.LEFT_LITTLE_INTERMEDIATE, - BodyPart.LEFT_LITTLE_DISTAL, - BodyPart.LEFT_BIG_TOE, - BodyPart.LEFT_INDEX_TOE, - BodyPart.LEFT_MIDDLE_TOE, - BodyPart.LEFT_RING_TOE, - BodyPart.LEFT_LITTLE_TOE, - -> Side.LEFT - - BodyPart.RIGHT_UPPER_LEG, - BodyPart.RIGHT_LOWER_LEG, - BodyPart.RIGHT_FOOT, - BodyPart.RIGHT_SHOULDER, - BodyPart.RIGHT_UPPER_ARM, - BodyPart.RIGHT_LOWER_ARM, - BodyPart.RIGHT_HAND, - BodyPart.RIGHT_THUMB_METACARPAL, - BodyPart.RIGHT_THUMB_PROXIMAL, - BodyPart.RIGHT_THUMB_DISTAL, - BodyPart.RIGHT_INDEX_PROXIMAL, - BodyPart.RIGHT_INDEX_INTERMEDIATE, - BodyPart.RIGHT_INDEX_DISTAL, - BodyPart.RIGHT_MIDDLE_PROXIMAL, - BodyPart.RIGHT_MIDDLE_INTERMEDIATE, - BodyPart.RIGHT_MIDDLE_DISTAL, - BodyPart.RIGHT_RING_PROXIMAL, - BodyPart.RIGHT_RING_INTERMEDIATE, - BodyPart.RIGHT_RING_DISTAL, - BodyPart.RIGHT_LITTLE_PROXIMAL, - BodyPart.RIGHT_LITTLE_INTERMEDIATE, - BodyPart.RIGHT_LITTLE_DISTAL, - BodyPart.RIGHT_BIG_TOE, - BodyPart.RIGHT_INDEX_TOE, - BodyPart.RIGHT_MIDDLE_TOE, - BodyPart.RIGHT_RING_TOE, - BodyPart.RIGHT_LITTLE_TOE, - -> Side.RIGHT - - else -> null - } + get() = BODY_PART_SIDES[this] 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 7ae6040f7..18a3449f8 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/mapping.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/mapping.kt @@ -3,6 +3,7 @@ package dev.slimevr.vmc import com.jme3.math.FastMath import dev.slimevr.resets.ResetBodyParts import dev.slimevr.skeleton.BodyPartMap +import dev.slimevr.util.opposite import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart @@ -163,6 +164,22 @@ private class VmcBoneTree(hierarchy: BodyPartMap>) { val VMC_OUTPUT_BONE_PARENTS: BodyPartMap = VmcBoneTree(VMC_HIERARCHY_MAP).parents +// Bones between the hips and the neck, hips first, as VMC's hierarchy links them. +val VMC_HIP_TO_NECK_CHAIN: List = generateSequence(BodyPart.NECK) { VMC_OUTPUT_BONE_PARENTS[it] } + .takeWhile { it != BodyPart.HIP } + .toList() + .asReversed() + +// Spine bones between the hips and the neck in the actual skeleton hierarchy +// Used to derive a rest hip height above the floor when anchored with no VRM loaded. +val SPINE_CHAIN_ABOVE_HIP: List = listOf( + BodyPart.NECK, + BodyPart.UPPER_CHEST, + BodyPart.LOWER_CHEST, + BodyPart.UPPER_WAIST, + BodyPart.LOWER_WAIST, +) + val VMC_INPUT_HIERARCHY_MAP: BodyPartMap> = BodyPartMap( VMC_HIERARCHY_MAP + mapOf( @@ -178,44 +195,7 @@ val UNITY_BONE_TO_BODY_PART: Map = BODY_PART_TO_UNITY_BONE.ent .flatMap { (bodyPart, names) -> names.map { name -> name.lowercase() to bodyPart } } .toMap() -val VMC_MIRROR_BONE_PAIRS: List> = listOf( - BodyPart.LEFT_SHOULDER to BodyPart.RIGHT_SHOULDER, - BodyPart.LEFT_UPPER_ARM to BodyPart.RIGHT_UPPER_ARM, - BodyPart.LEFT_LOWER_ARM to BodyPart.RIGHT_LOWER_ARM, - BodyPart.LEFT_HAND to BodyPart.RIGHT_HAND, - BodyPart.LEFT_UPPER_LEG to BodyPart.RIGHT_UPPER_LEG, - BodyPart.LEFT_LOWER_LEG to BodyPart.RIGHT_LOWER_LEG, - BodyPart.LEFT_FOOT to BodyPart.RIGHT_FOOT, - BodyPart.LEFT_THUMB_METACARPAL to BodyPart.RIGHT_THUMB_METACARPAL, - BodyPart.LEFT_THUMB_PROXIMAL to BodyPart.RIGHT_THUMB_PROXIMAL, - BodyPart.LEFT_THUMB_DISTAL to BodyPart.RIGHT_THUMB_DISTAL, - BodyPart.LEFT_INDEX_PROXIMAL to BodyPart.RIGHT_INDEX_PROXIMAL, - BodyPart.LEFT_INDEX_INTERMEDIATE to BodyPart.RIGHT_INDEX_INTERMEDIATE, - BodyPart.LEFT_INDEX_DISTAL to BodyPart.RIGHT_INDEX_DISTAL, - BodyPart.LEFT_MIDDLE_PROXIMAL to BodyPart.RIGHT_MIDDLE_PROXIMAL, - BodyPart.LEFT_MIDDLE_INTERMEDIATE to BodyPart.RIGHT_MIDDLE_INTERMEDIATE, - BodyPart.LEFT_MIDDLE_DISTAL to BodyPart.RIGHT_MIDDLE_DISTAL, - BodyPart.LEFT_RING_PROXIMAL to BodyPart.RIGHT_RING_PROXIMAL, - BodyPart.LEFT_RING_INTERMEDIATE to BodyPart.RIGHT_RING_INTERMEDIATE, - BodyPart.LEFT_RING_DISTAL to BodyPart.RIGHT_RING_DISTAL, - BodyPart.LEFT_LITTLE_PROXIMAL to BodyPart.RIGHT_LITTLE_PROXIMAL, - BodyPart.LEFT_LITTLE_INTERMEDIATE to BodyPart.RIGHT_LITTLE_INTERMEDIATE, - BodyPart.LEFT_LITTLE_DISTAL to BodyPart.RIGHT_LITTLE_DISTAL, - BodyPart.LEFT_BIG_TOE to BodyPart.RIGHT_BIG_TOE, - BodyPart.LEFT_BIG_TOE to BodyPart.RIGHT_BIG_TOE, - BodyPart.LEFT_INDEX_TOE to BodyPart.RIGHT_INDEX_TOE, - BodyPart.LEFT_MIDDLE_TOE to BodyPart.RIGHT_MIDDLE_TOE, - BodyPart.LEFT_RING_TOE to BodyPart.RIGHT_RING_TOE, - BodyPart.LEFT_LITTLE_TOE to BodyPart.RIGHT_LITTLE_TOE, -) - -val VMC_MIRROR_BONES: BodyPartMap = BodyPartMap( - VMC_MIRROR_BONE_PAIRS - .flatMap { (left, right) -> listOf(left to right, right to left) } - .toMap(), -) - -fun vmcMirrorSource(bodyPart: BodyPart): BodyPart = VMC_MIRROR_BONES[bodyPart] ?: bodyPart +fun vmcMirrorSource(bodyPart: BodyPart): BodyPart = bodyPart.opposite ?: bodyPart // Per-bone rest offset, subtracted from the live world rotation before computing the VMC local. // Arms remap our hanging rest (NEG_Y) to the VRM rig's T-pose rest direction so the avatar isn't stuck at T regardless of our pose. diff --git a/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt b/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt index 97d4ecd29..2d8a6c860 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt @@ -17,6 +17,8 @@ internal fun buildOutgoingBundle( routedBones: Set, config: VMCConfig, vrm: VrmGeometry?, + skeletonHeight: Float, + floorLevel: Float, elapsed: Duration, ): OscBundle { val contents = buildList { @@ -30,29 +32,20 @@ internal fun buildOutgoingBundle( if (targetBodyPart !in routedBones) continue val targetParentBodyPart = VMC_OUTPUT_BONE_PARENTS[targetBodyPart] - val trackingBodyPart = if (config.mirrorTracking) vmcMirrorSource(targetBodyPart) else targetBodyPart + val trackingBodyPart = trackingBodyPart(targetBodyPart, config.mirrorTracking) val trackingBone = bones[trackingBodyPart] ?: continue if (targetParentBodyPart == null) { - // TODO anchorHip https://github.com/SlimeVR/SlimeVR-Server/blob/main/server/core/src/main/java/dev/slimevr/osc/VMCHandler.kt#L371 - val pos = vrm?.hipLocalPosition ?: Vector3.ZERO + val pos = vmcHipPosition(bones, vrm, config, skeletonHeight, floorLevel) val rot = vmcLocalRotation(trackingBone, null, targetBodyPart, null, config.mirrorTracking) add(OscContent.Message(transformMessage("/VMC/Ext/Bone/Pos", unityNames.first(), pos, rot))) continue } - val trackingParentBodyPart = if (config.mirrorTracking) { - vmcMirrorSource(targetParentBodyPart) - } else { - targetParentBodyPart - } + val trackingParentBodyPart = trackingBodyPart(targetParentBodyPart, config.mirrorTracking) val trackingParent = bones[trackingParentBodyPart] ?: continue - val pos = if (vrm != null) { - vrm.bindOffsets[targetBodyPart] ?: Vector3.ZERO - } else { - vmcLocalPosition(trackingBone, trackingParent, targetParentBodyPart, config.mirrorTracking) - } + val pos = emittedLocalPosition(targetBodyPart, targetParentBodyPart, trackingBone, trackingParent, vrm, config.mirrorTracking) val rot = vmcLocalRotation( trackingBone, trackingParent, @@ -71,6 +64,9 @@ internal fun buildOutgoingBundle( internal fun buildInitRequestMessage(): OscMessage = OscMessage("/VMC/Ext/Req", emptyList()) +private fun trackingBodyPart(targetBodyPart: BodyPart, mirror: Boolean): BodyPart = + if (mirror) vmcMirrorSource(targetBodyPart) else targetBodyPart + private fun restAdjustedWorld( bone: BoneState, restBodyPart: BodyPart = bone.bodyPart, @@ -104,6 +100,56 @@ internal fun vmcLocalPosition( return parentAdjusted.inv().sandwich(if (mirror) vmcMirrorPosition(localPosition) else localPosition) } +private fun emittedLocalPosition( + targetBodyPart: BodyPart, + targetParentBodyPart: BodyPart, + trackingBone: BoneState, + trackingParent: BoneState, + vrm: VrmGeometry?, + mirror: Boolean, +): Vector3 = if (vrm != null) { + vrm.bindOffsets[targetBodyPart] ?: Vector3.ZERO +} else { + vmcLocalPosition(trackingBone, trackingParent, targetParentBodyPart, mirror) +} + +private fun hipToNeckOffset(bones: ComputedSkeleton, vrm: VrmGeometry?, mirror: Boolean): Vector3? { + var parentBodyPart = BodyPart.HIP + var offset = Vector3.ZERO + for (bodyPart in VMC_HIP_TO_NECK_CHAIN) { + val trackingBone = bones[trackingBodyPart(bodyPart, mirror)] ?: return null + val trackingParent = bones[trackingBodyPart(parentBodyPart, mirror)] ?: return null + val localPosition = emittedLocalPosition(bodyPart, parentBodyPart, trackingBone, trackingParent, vrm, mirror) + val parentWorldRotation = restAdjustedWorld(trackingParent, parentBodyPart, mirror) + offset += parentWorldRotation.sandwich(localPosition) + parentBodyPart = bodyPart + } + return offset +} + +// Hip height above the floor at rest, used when anchored with no VRM loaded +private fun restHipHeight(bones: ComputedSkeleton, skeletonHeight: Float): Float = skeletonHeight + + SPINE_CHAIN_ABOVE_HIP.sumOf { (bones[it]?.offset?.y ?: 0f).toDouble() }.toFloat() + +private fun vmcHipPosition( + bones: ComputedSkeleton, + vrm: VrmGeometry?, + config: VMCConfig, + skeletonHeight: Float, + floorLevel: Float, +): Vector3 { + val anchoredHipPosition = vrm?.hipLocalPosition ?: Vector3(0f, restHipHeight(bones, skeletonHeight), 0f) + if (config.anchorAtHips) return anchoredHipPosition + + val neck = bones[BodyPart.NECK] ?: return anchoredHipPosition + val neckOffset = hipToNeckOffset(bones, vrm, config.mirrorTracking) ?: return anchoredHipPosition + + val restHeight = vrm?.outputRestHeight ?: skeletonHeight + val scale = if (skeletonHeight != 0f) restHeight / skeletonHeight else 1f + val floorRelativeNeck = (neck.headPosition - Vector3(0f, floorLevel, 0f)) * scale + return floorRelativeNeck - neckOffset +} + private fun transformMessage(address: String, name: String, pos: Vector3, rot: Quaternion): OscMessage = OscMessage( address, listOf( diff --git a/server/core/src/main/java/dev/slimevr/vmc/output.kt b/server/core/src/main/java/dev/slimevr/vmc/output.kt index b10468d2d..f16661623 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/output.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/output.kt @@ -65,13 +65,27 @@ class VMCOutputBehaviour( .map { it.data.vmcConfig } .distinctUntilChanged() - combine(skeleton.computed, routedBones, config, ::Triple) - .onEach { (computedSkeleton, bones, vmcConfig) -> - sendFrame(receiver, runtime, computedSkeleton, bones, vmcConfig, startedAt) + val floorReference = skeleton.context.state + .map { it.skeletonHeight to it.floorLevel } + .distinctUntilChanged() + + combine(skeleton.computed, routedBones, config, floorReference) { computedSkeleton, routedBoneSet, vmcConfig, floor -> + FrameInputs(computedSkeleton, routedBoneSet, vmcConfig, skeletonHeight = floor.first, floorLevel = floor.second) + } + .onEach { (computedSkeleton, routedBoneSet, vmcConfig, skeletonHeight, floorLevel) -> + sendFrame(receiver, runtime, computedSkeleton, routedBoneSet, vmcConfig, skeletonHeight, floorLevel, startedAt) } .launchIn(receiver.context.scope) } + private data class FrameInputs( + val computedSkeleton: ComputedSkeleton, + val routedBones: Set, + val config: VMCConfig, + val skeletonHeight: Float, + val floorLevel: Float, + ) + private suspend fun applyTarget( receiver: VMCManager, runtime: OutputRuntime, @@ -133,13 +147,23 @@ class VMCOutputBehaviour( bones: ComputedSkeleton, routedBones: Set, config: VMCConfig, + skeletonHeight: Float, + floorLevel: Float, startedAt: MonotonicValueTimeMark, ) { val sender = runtime.sender ?: return val status = receiver.context.state.value.status if (runtime.sendFailing && runtime.nextFrameRetryAt?.hasPassedNow() == false) return - val bundle = buildOutgoingBundle(bones, routedBones, config, receiver.context.state.value.vrm, startedAt.elapsedNow()) + val bundle = buildOutgoingBundle( + bones, + routedBones, + config, + receiver.context.state.value.vrm, + skeletonHeight, + floorLevel, + startedAt.elapsedNow(), + ) try { sender.send(bundle) 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 e5f9a96a1..82f8d32f8 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/vrm.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/vrm.kt @@ -97,6 +97,8 @@ data class VrmGeometry( val hipLocalPosition: Vector3, /** Floor-to-neck height, on the same basis as Skeleton.skeletonHeight. Used to scale VMC input positions. */ val vrmHeight: Float, + /** Floor-to-neck height along the VMC output hierarchy */ + val outputRestHeight: Float, ) fun buildVrmGeometry(reader: VrmReader): VrmGeometry { @@ -115,10 +117,13 @@ fun buildVrmGeometry(reader: VrmReader): VrmGeometry { offset(BodyPart.NECK) ).y + val outputRestHeight = hipLocalPosition.y + VMC_HIP_TO_NECK_CHAIN.sumOf { offset(it).y.toDouble() }.toFloat() + return VrmGeometry( bindOffsets = bindOffsets, hipLocalPosition = hipLocalPosition, vrmHeight = vrmHeight, + outputRestHeight = outputRestHeight, ) } 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 f155f1041..d6a13969b 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt @@ -139,7 +139,8 @@ class InputDecoderTest { ), ) - val bundle = buildOutgoingBundle(bones, bones.keys, VMCConfig(), vrm = null, elapsed = 0.seconds) + val config = VMCConfig(anchorAtHips = false) + val bundle = buildOutgoingBundle(bones, bones.keys, config, vrm = null, skeletonHeight = 1.7f, floorLevel = 0f, elapsed = 0.seconds) val frame = decodeVmcBundle(bundle, emptyVmcInputFrame()) val worldTransforms = vmcWorldTransforms( locals = frame.boneLocalRotations, diff --git a/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt b/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt index f78e58dda..1adb9c33c 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt @@ -9,6 +9,7 @@ import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.BoneState import dev.slimevr.skeleton.Velocity import dev.slimevr.skeleton.bodyPartMap +import dev.slimevr.vectorAssertEquals import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import solarxr_protocol.datatypes.BodyPart @@ -19,15 +20,15 @@ import kotlin.test.assertNull import kotlin.test.assertTrue import kotlin.time.Duration.Companion.seconds -private fun bone(bodyPart: BodyPart, rotation: Quaternion = Quaternion.IDENTITY) = BoneState( +private fun bone(bodyPart: BodyPart, rotation: Quaternion = Quaternion.IDENTITY, headPosition: Vector3 = Vector3(0f, 1f, 0f)) = BoneState( parentBone = null, bodyPart = bodyPart, headOffset = Vector3.ZERO, offset = Vector3(0f, -0.1f, 0f), rotation = rotation, acceleration = Vector3.ZERO, - headPosition = Vector3(0f, 1f, 0f), - tailPosition = Vector3(0f, 0.9f, 0f), + headPosition = headPosition, + tailPosition = headPosition + Vector3(0f, -0.1f, 0f), velocity = Velocity(Vector3.ZERO, Vector3.ZERO), ) @@ -36,6 +37,13 @@ private fun messages(bundle: OscBundle): List = bundle.contents.map private fun boneMessage(bundle: OscBundle, unityName: String): OscMessage? = messages(bundle) .firstOrNull { it.address == "/VMC/Ext/Bone/Pos" && (it.args.firstOrNull() as? OscArg.String)?.value == unityName } +// The encoder negates z on the wire, so undo that to recover the emitted position. +private fun messagePosition(msg: OscMessage): Vector3 = Vector3( + (msg.args[1] as OscArg.Float).value, + (msg.args[2] as OscArg.Float).value, + -(msg.args[3] as OscArg.Float).value, +) + class OutputEncoderTest { private val defaultConfig = VMCConfig() @@ -46,6 +54,8 @@ class OutputEncoderTest { routedBones = emptySet(), config = defaultConfig, vrm = null, + skeletonHeight = 1.7f, + floorLevel = 0f, elapsed = 2.seconds, ) @@ -69,6 +79,8 @@ class OutputEncoderTest { routedBones = setOf(BodyPart.HIP), config = defaultConfig, vrm = null, + skeletonHeight = 1.7f, + floorLevel = 0f, elapsed = 0.seconds, ) @@ -83,6 +95,8 @@ class OutputEncoderTest { routedBones = setOf(BodyPart.HIP, BodyPart.LOWER_WAIST), config = defaultConfig, vrm = null, + skeletonHeight = 1.7f, + floorLevel = 0f, elapsed = 0.seconds, ) @@ -102,12 +116,14 @@ class OutputEncoderTest { ) val routed = setOf(BodyPart.HIP, BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG) - val plain = buildOutgoingBundle(bones, routed, defaultConfig, null, 0.seconds) + val plain = buildOutgoingBundle(bones, routed, defaultConfig, null, 1.7f, 0f, 0.seconds) val mirrored = buildOutgoingBundle( bones, routed, defaultConfig.copy(mirrorTracking = true), null, + 1.7f, + 0f, 0.seconds, ) @@ -128,11 +144,106 @@ class OutputEncoderTest { val routed = setOf(BodyPart.HIP, BodyPart.LOWER_WAIST) val vrm = buildVrmGeometry(VrmReader(VRM_JSON)) - val bundle = buildOutgoingBundle(bones, routed, defaultConfig, vrm, 0.seconds) + val bundle = buildOutgoingBundle(bones, routed, defaultConfig, vrm, 1.7f, 0f, 0.seconds) val hips = assertNotNull(boneMessage(bundle, "Hips")) assertEquals(0.9f, (hips.args[2] as OscArg.Float).value) } + + @Test + fun testAnchoredHipsWithVrmIgnoreLiveSkeletonPosition() { + val vrm = buildVrmGeometry(VrmReader(VRM_JSON)) + // The hip bone itself is far from where the anchored output should place it, to prove + // the live position isn't read at all. + val bones = BodyPartMap(mapOf(BodyPart.HIP to bone(BodyPart.HIP, headPosition = Vector3(3f, 5f, -2f)))) + val config = defaultConfig.copy(anchorAtHips = true) + + val bundle = buildOutgoingBundle(bones, setOf(BodyPart.HIP), config, vrm, 1.7f, 0.3f, 0.seconds) + + val hips = assertNotNull(boneMessage(bundle, "Hips")) + vectorAssertEquals(vrm.hipLocalPosition, messagePosition(hips)) + } + + @Test + fun testAnchoredHipsWithoutVrmUseRestHeightAboveFloor() { + val bones = BodyPartMap( + mapOf( + BodyPart.HIP to bone(BodyPart.HIP, headPosition = Vector3(3f, 5f, -2f)), + BodyPart.NECK to bone(BodyPart.NECK), + BodyPart.LOWER_CHEST to bone(BodyPart.LOWER_CHEST), + ), + ) + val config = defaultConfig.copy(anchorAtHips = true) + + val bundle = buildOutgoingBundle(bones, setOf(BodyPart.HIP), config, null, 1.7f, 0f, 0.seconds) + + val hips = assertNotNull(boneMessage(bundle, "Hips")) + // skeletonHeight (1.7) minus the two present bones' rest lengths (0.1 each) + vectorAssertEquals(Vector3(0f, 1.5f, 0f), messagePosition(hips)) + } + + @Test + fun testFreeAnchorWithoutVrmPlacesHipsFromNeckMinusFloor() { + val hipHead = Vector3(0f, 0.9f, 0f) + val bones = BodyPartMap( + mapOf( + BodyPart.HIP to bone(BodyPart.HIP, headPosition = hipHead), + BodyPart.UPPER_WAIST to bone(BodyPart.UPPER_WAIST, headPosition = Vector3(0f, 1.1f, 0f)), + BodyPart.LOWER_CHEST to bone(BodyPart.LOWER_CHEST, headPosition = Vector3(0f, 1.3f, 0f)), + BodyPart.NECK to bone(BodyPart.NECK, headPosition = Vector3(0f, 1.5f, 0f)), + ), + ) + val config = defaultConfig.copy(anchorAtHips = false) + val skeletonHeight = 1.5f + + val atFloorZero = buildOutgoingBundle(bones, setOf(BodyPart.HIP), config, null, skeletonHeight, 0f, 0.seconds) + vectorAssertEquals(hipHead, messagePosition(assertNotNull(boneMessage(atFloorZero, "Hips")))) + + val atRaisedFloor = buildOutgoingBundle(bones, setOf(BodyPart.HIP), config, null, skeletonHeight, 0.2f, 0.seconds) + vectorAssertEquals(hipHead - Vector3(0f, 0.2f, 0f), messagePosition(assertNotNull(boneMessage(atRaisedFloor, "Hips")))) + } + + @Test + fun testFreeAnchorTranslatesWithWholeSkeleton() { + val delta = Vector3(0.5f, 0.3f, -0.2f) + fun bones(shift: Vector3) = BodyPartMap( + mapOf( + BodyPart.HIP to bone(BodyPart.HIP, headPosition = Vector3(0f, 0.9f, 0f) + shift), + BodyPart.UPPER_WAIST to bone(BodyPart.UPPER_WAIST, headPosition = Vector3(0f, 1.1f, 0f) + shift), + BodyPart.LOWER_CHEST to bone(BodyPart.LOWER_CHEST, headPosition = Vector3(0f, 1.3f, 0f) + shift), + BodyPart.NECK to bone(BodyPart.NECK, headPosition = Vector3(0f, 1.5f, 0f) + shift), + ), + ) + val config = defaultConfig.copy(anchorAtHips = false) + val skeletonHeight = 1.5f + + val base = buildOutgoingBundle(bones(Vector3.ZERO), setOf(BodyPart.HIP), config, null, skeletonHeight, 0f, 0.seconds) + val shifted = buildOutgoingBundle(bones(delta), setOf(BodyPart.HIP), config, null, skeletonHeight, 0f, 0.seconds) + + val baseHips = messagePosition(assertNotNull(boneMessage(base, "Hips"))) + val shiftedHips = messagePosition(assertNotNull(boneMessage(shifted, "Hips"))) + + vectorAssertEquals(baseHips + delta, shiftedHips) + } + + @Test + fun testFreeAnchorWithVrmAtRestMatchesHipLocalPosition() { + val vrm = buildVrmGeometry(VrmReader(VRM_JSON)) + val bones = BodyPartMap( + mapOf( + BodyPart.HIP to bone(BodyPart.HIP), + BodyPart.UPPER_WAIST to bone(BodyPart.UPPER_WAIST), + BodyPart.LOWER_CHEST to bone(BodyPart.LOWER_CHEST), + BodyPart.NECK to bone(BodyPart.NECK, headPosition = Vector3(0f, vrm.outputRestHeight, 0f)), + ), + ) + val config = defaultConfig.copy(anchorAtHips = false) + + val bundle = buildOutgoingBundle(bones, setOf(BodyPart.HIP), config, vrm, vrm.outputRestHeight, 0f, 0.seconds) + + val hips = assertNotNull(boneMessage(bundle, "Hips")) + vectorAssertEquals(vrm.hipLocalPosition, messagePosition(hips)) + } } // Minimal VRM 1.0 document: a hips node at y = 0.9 and a spine node above it. diff --git a/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt b/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt index e07f82923..cbe7af101 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt @@ -117,7 +117,7 @@ class VMCInputBehaviourTest { harness.receiver.context.dispatch( VMCActions.SetVrm( state = VMCOSCVrmState.LOADED, - vrm = VrmGeometry(bindOffsets = bodyPartMap(), hipLocalPosition = Vector3.ZERO, vrmHeight = skeletonHeight / 2f), + vrm = VrmGeometry(bindOffsets = bodyPartMap(), hipLocalPosition = Vector3.ZERO, vrmHeight = skeletonHeight / 2f, outputRestHeight = skeletonHeight / 2f), ), )