diff --git a/gui/app/src/components/commons/BodyPartIcon.tsx b/gui/app/src/components/commons/BodyPartIcon.tsx index 219e68e72..ff86eaef8 100644 --- a/gui/app/src/components/commons/BodyPartIcon.tsx +++ b/gui/app/src/components/commons/BodyPartIcon.tsx @@ -75,8 +75,6 @@ export const mapPart: Record< [BodyPart.CHEST]: ({ width }) => , [BodyPart.HEAD]: ({ width }) => , [BodyPart.HIP]: ({ width }) => , - [BodyPart.LEFT_HIP]: ({ width }) => , // Unused - [BodyPart.RIGHT_HIP]: ({ width }) => , // Unused [BodyPart.LEFT_FOOT]: ({ width, currentLocales }) => currentLocales.includes('en-x-owo') ? ( diff --git a/gui/app/src/hooks/body-parts.ts b/gui/app/src/hooks/body-parts.ts index 7c391de7f..93add9328 100644 --- a/gui/app/src/hooks/body-parts.ts +++ b/gui/app/src/hooks/body-parts.ts @@ -84,7 +84,5 @@ export const ALL_BODY_PARTS = [ ...SPINE_BODY_PARTS, ...LEG_BODY_PARTS, ...ARM_BODY_PARTS, - BodyPart.LEFT_HIP, - BodyPart.RIGHT_HIP, ...FINGER_BODY_PARTS, ]; diff --git a/gui/app/src/utils/skeletonHelper.ts b/gui/app/src/utils/skeletonHelper.ts index cfdc24355..072dc5180 100644 --- a/gui/app/src/utils/skeletonHelper.ts +++ b/gui/app/src/utils/skeletonHelper.ts @@ -39,8 +39,6 @@ const BONE_COLOR_GROUPS: [BodyPart[], string][] = [ export function getBoneColor(bodyPart: BodyPart) { if ( - bodyPart === BodyPart.LEFT_HIP || - bodyPart === BodyPart.RIGHT_HIP || FINGER_BODY_PARTS.includes(bodyPart) || TOE_BODY_PARTS.includes(bodyPart) ) { diff --git a/gui/app/src/utils/skeletonParts.ts b/gui/app/src/utils/skeletonParts.ts index 91871546a..09890656f 100644 --- a/gui/app/src/utils/skeletonParts.ts +++ b/gui/app/src/utils/skeletonParts.ts @@ -278,19 +278,6 @@ export const SKELETON_PART_PRESETS: Record = { }), { trackerOffset: HIP_TRACKER_OFFSET } ), - [BodyPart.LEFT_HIP]: part( - model('hip_ball', { - scale: spanBone({ girthFrom: 'hips', length: 0.6 }), - offset: inBoneLengths({ depth: 0.8, length: -0.8 }), - }) - ), - [BodyPart.RIGHT_HIP]: part( - model('hip_ball', { - scale: spanBone({ girthFrom: 'hips', length: 0.6 }), - offset: inBoneLengths({ depth: 0.8, length: -0.8 }), - }) - ), - [BodyPart.LEFT_SHOULDER]: part( model('shoulder', { scale: shoulderScale, diff --git a/gui/app/src/utils/skeletonProportions.ts b/gui/app/src/utils/skeletonProportions.ts index e19d51361..2790718b1 100644 --- a/gui/app/src/utils/skeletonProportions.ts +++ b/gui/app/src/utils/skeletonProportions.ts @@ -59,8 +59,8 @@ export function computeHeadYOffset(bones: Map) { /** * Derives the few body measurements the mesh preview needs from the skeleton. * - * `BoneT` only carries lengths, so girths have to come from the bones that - * happen to run sideways: the shoulder and hip bones. + * Shoulder girth comes from the sideways-running shoulder bones. Hip girth is the + * gap between the two upper-leg heads, which the hip width spreads apart. */ export function deriveSkeletonProportions( bones: Map @@ -70,7 +70,17 @@ export function deriveSkeletonProportions( const shoulderWidth = sumBoneLengths(bones, [BodyPart.LEFT_SHOULDER, BodyPart.RIGHT_SHOULDER]) ?? 0; - const hipWidth = sumBoneLengths(bones, [BodyPart.LEFT_HIP, BodyPart.RIGHT_HIP]) ?? 0; + + const leftLegHead = bones.get(BodyPart.LEFT_UPPER_LEG)?.headPosition; + const rightLegHead = bones.get(BodyPart.RIGHT_UPPER_LEG)?.headPosition; + const hipWidth = + leftLegHead && rightLegHead + ? Math.hypot( + leftLegHead.x - rightLegHead.x, + leftLegHead.y - rightLegHead.y, + leftLegHead.z - rightLegHead.z + ) + : 0; return { bodyScale, diff --git a/server/core/src/main/java/dev/slimevr/bvh/stream.kt b/server/core/src/main/java/dev/slimevr/bvh/stream.kt index 9f3c79fc1..5c946c970 100644 --- a/server/core/src/main/java/dev/slimevr/bvh/stream.kt +++ b/server/core/src/main/java/dev/slimevr/bvh/stream.kt @@ -95,9 +95,12 @@ class BvhStream( return intervalString.padEnd(9) } - private fun getBvhOffset(parentPart: BodyPart, bones: ComputedSkeleton): Vector3 { - val offset = bones[parentPart]?.offset ?: return Vector3.ZERO - return offset * OFFSET_SCALE + // A joint's BVH OFFSET is the vector from the parent joint's origin to this joint's, in the + // parent's frame: the parent's own head->tail offset plus this bone's head displacement. + private fun getBvhOffset(childPart: BodyPart, parentPart: BodyPart, bones: ComputedSkeleton): Vector3 { + val parentOffset = bones[parentPart]?.offset ?: Vector3.ZERO + val childHeadOffset = bones[childPart]?.headOffset ?: Vector3.ZERO + return (parentOffset + childHeadOffset) * OFFSET_SCALE } private suspend fun writeRotations(part: BodyPart, bones: ComputedSkeleton) { @@ -118,7 +121,7 @@ class BvhStream( file.write("${childIndent}OFFSET 0.0 0.0 0.0\n") file.write("${childIndent}CHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation\n") } else { - val offset = getBvhOffset(parent, bones) + val offset = getBvhOffset(part, parent, bones) file.write("${childIndent}OFFSET ${offset.x} ${offset.y} ${offset.z}\n") file.write("${childIndent}CHANNELS 3 Zrotation Xrotation Yrotation\n") } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt index 69a2e1a38..ac63d5354 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import solarxr_protocol.datatypes.BodyPart +import solarxr_protocol.rpc.ResetType import java.util.EnumMap import java.util.concurrent.Executors import kotlin.math.cos @@ -36,6 +37,7 @@ class ProportionsBehaviour(private val userConfig: UserConfig) : SkeletonBehavio .onEach { proportions -> if (proportions.isNotEmpty()) { receiver.context.dispatch(SkeletonActions.SetProportions(configToBoneValues(proportions))) + receiver.resetProcessors(ResetType.FULL) } } .launchIn(receiver.context.scope) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt index 84be9fe03..60df2ed16 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt @@ -60,10 +60,6 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( 120f * FastMath.DEG_TO_RAD, ), - BodyPart.LEFT_HIP to TwistSwingConstraint( - 0f * FastMath.DEG_TO_RAD, - 15f * FastMath.DEG_TO_RAD, - ), BodyPart.LEFT_UPPER_LEG to TwistSwingConstraint( 120f * FastMath.DEG_TO_RAD, 180f * FastMath.DEG_TO_RAD, @@ -78,10 +74,6 @@ val BODY_PART_CONSTRAINT_MAP: BodyPartMap = BodyPartMap( 60f * FastMath.DEG_TO_RAD, ), - BodyPart.RIGHT_HIP to TwistSwingConstraint( - 0f * FastMath.DEG_TO_RAD, - 15f * FastMath.DEG_TO_RAD, - ), BodyPart.RIGHT_UPPER_LEG to TwistSwingConstraint( 120f * FastMath.DEG_TO_RAD, 180f * FastMath.DEG_TO_RAD, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt index 01c154acd..c812fec5d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-structure.kt @@ -54,13 +54,11 @@ val BODY_PART_HIERARCHY_MAP: BodyPartMap> = BodyPartMap( BodyPart.UPPER_CHEST to arrayOf(BodyPart.CHEST), BodyPart.CHEST to arrayOf(BodyPart.WAIST), BodyPart.WAIST to arrayOf(BodyPart.HIP), - BodyPart.HIP to arrayOf(BodyPart.LEFT_HIP, BodyPart.RIGHT_HIP), + BodyPart.HIP to arrayOf(BodyPart.LEFT_UPPER_LEG, BodyPart.RIGHT_UPPER_LEG), - BodyPart.LEFT_HIP to arrayOf(BodyPart.LEFT_UPPER_LEG), BodyPart.LEFT_UPPER_LEG to arrayOf(BodyPart.LEFT_LOWER_LEG), BodyPart.LEFT_LOWER_LEG to arrayOf(BodyPart.LEFT_FOOT), - BodyPart.RIGHT_HIP to arrayOf(BodyPart.RIGHT_UPPER_LEG), BodyPart.RIGHT_UPPER_LEG to arrayOf(BodyPart.RIGHT_LOWER_LEG), BodyPart.RIGHT_LOWER_LEG to arrayOf(BodyPart.RIGHT_FOOT), diff --git a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt index 104714215..38b7a1b9f 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/ik.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/ik.kt @@ -21,20 +21,6 @@ fun chainDistanceFromTarget( return (target - chainTail).len() } -fun chainCanReach( - bones: ComputedSkeleton, - chain: IKChain, - target: Vector3, -): Boolean { - val chainHead = requireBone(bones, chain.first()).headPosition - - val chainLength = chain.fold(0f) { acc, bodyPart -> - val boneLength = requireBone(bones, bodyPart).offset.len() - acc + boneLength - } - return (target - chainHead).len() <= chainLength -} - private val oppositeRotation = Quaternion.rotationAroundZAxis(FastMath.PI) fun fromChainToTarget( bodyPart: BodyPart, 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/bone-direct-link.kt index 250958c1c..1554036e1 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/bone-direct-link.kt @@ -17,9 +17,6 @@ class BoneDirectLinkInputProcessor : SkeletonInputProcessor { BodyPart.HEAD to BodyPart.NECK, BodyPart.NECK to BodyPart.HEAD, - BodyPart.LEFT_HIP to BodyPart.HIP, - BodyPart.RIGHT_HIP to BodyPart.HIP, - BodyPart.LEFT_FOOT to BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_FOOT to BodyPart.RIGHT_LOWER_LEG, 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 bf600aa86..dd285b19a 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -41,6 +41,7 @@ val ZERO_VELOCITY = Velocity(Vector3.ZERO, Vector3.ZERO) /** Pre-FK */ data class BoneInput( val bodyPart: BodyPart, + val headOffset: Vector3, val offset: Vector3, val rotation: Quaternion, val acceleration: Vector3, @@ -54,6 +55,7 @@ data class BoneInput( data class BoneState( val parentBone: BoneState?, val bodyPart: BodyPart, + val headOffset: Vector3, val offset: Vector3, val rotation: Quaternion, val acceleration: Vector3, @@ -93,6 +95,7 @@ data class SkeletonState( val DEFAULT_BONE_INPUT = BoneInput( bodyPart = BodyPart.NONE, + headOffset = Vector3.ZERO, offset = Vector3.ZERO, rotation = Quaternion.IDENTITY, acceleration = Vector3.ZERO, @@ -102,25 +105,31 @@ val DEFAULT_BONE_INPUT = BoneInput( isPositionActive = false, ) -val DEFAULT_SKELETON_STATE = SkeletonState( - boneInputs = DEFAULT_PROPORTIONS.toBoneOffsets().mapValues { bodyPart, tailOffset -> - DEFAULT_BONE_INPUT.copy( - bodyPart = bodyPart, - offset = tailOffset, - ) - }, - skeletonHeight = DEFAULT_HEIGHT, - floorLevel = 0f, - paused = false, - pausedProcessedBoneInputs = null, -) +val DEFAULT_SKELETON_STATE = run { + val offsets = toBoneOffsets(DEFAULT_PROPORTIONS) + SkeletonState( + boneInputs = offsets.tail.mapValues { bodyPart, tailOffset -> + DEFAULT_BONE_INPUT.copy( + bodyPart = bodyPart, + headOffset = offsets.head[bodyPart] ?: Vector3.ZERO, + offset = tailOffset, + ) + }, + skeletonHeight = DEFAULT_HEIGHT, + floorLevel = 0f, + paused = false, + pausedProcessedBoneInputs = null, + ) +} fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO_VELOCITY): BoneState { // Raw position of the bone input is used for BodyPart.HEAD since it has no parent - val headPosition = parentBone?.tailPosition ?: bone.position ?: Vector3.ZERO + val headPosition = parentBone?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) } + ?: bone.position ?: Vector3.ZERO return BoneState( parentBone = parentBone, bodyPart = bone.bodyPart, + headOffset = bone.headOffset, offset = bone.offset, rotation = bone.rotation, acceleration = bone.acceleration, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt index bf8bb0e29..fae7f06de 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt @@ -48,7 +48,6 @@ private val BONE_VALUE_TO_OFFSETS: Map> = map SkeletonBone.CHEST to BodyPartMap(mapOf(BodyPart.CHEST to Vector3.NEG_Y)), SkeletonBone.WAIST to BodyPartMap(mapOf(BodyPart.WAIST to Vector3.NEG_Y)), SkeletonBone.HIP to BodyPartMap(mapOf(BodyPart.HIP to Vector3.NEG_Y)), - SkeletonBone.HIPS_WIDTH to BodyPartMap(mapOf(BodyPart.LEFT_HIP to Vector3.NEG_X / 2f, BodyPart.RIGHT_HIP to Vector3.POS_X / 2f)), SkeletonBone.UPPER_LEG to BodyPartMap(mapOf(BodyPart.LEFT_UPPER_LEG to Vector3.NEG_Y, BodyPart.RIGHT_UPPER_LEG to Vector3.NEG_Y)), SkeletonBone.LOWER_LEG to BodyPartMap(mapOf(BodyPart.LEFT_LOWER_LEG to Vector3.NEG_Y, BodyPart.RIGHT_LOWER_LEG to Vector3.NEG_Y)), SkeletonBone.FOOT_LENGTH to BodyPartMap(mapOf(BodyPart.LEFT_FOOT to Vector3.NEG_Z, BodyPart.RIGHT_FOOT to Vector3.NEG_Z)), @@ -61,22 +60,21 @@ private val BONE_VALUE_TO_OFFSETS: Map> = map SkeletonBone.HAND_Z to BodyPartMap(mapOf(BodyPart.LEFT_HAND to Vector3.NEG_Z, BodyPart.RIGHT_HAND to Vector3.NEG_Z)), ) -private val BONE_OFFSET_TO_VALUES: BodyPartMap> = BodyPartMap( - BONE_VALUE_TO_OFFSETS - .flatMap { (cfg, bones) -> - // Invert map, splitting entries [ List>> ] - // Vector also needs to be inverted ((vec/len)/len)==(1/vec) - bones.map { (bone, vec) -> bone to (cfg to vec / vec.lenSq()) } - } - // Merge entries, creating a map again [ Map>> ] +private val BONE_VALUE_TO_HEAD_OFFSETS: Map> = mapOf( + SkeletonBone.HIPS_WIDTH to BodyPartMap(mapOf(BodyPart.LEFT_UPPER_LEG to Vector3.NEG_X / 2f, BodyPart.RIGHT_UPPER_LEG to Vector3.POS_X / 2f)), +) + +// Inverts an offset table so a resolved offset vector can be turned back into a SkeletonBone value. +// The vector is inverted too ((vec/len)/len == 1/vec) so hadamard + len recovers the scalar. +private fun invertOffsetTable(table: Map>): BodyPartMap> = BodyPartMap( + table + .flatMap { (cfg, bones) -> bones.map { (bone, vec) -> bone to (cfg to vec / vec.lenSq()) } } .groupBy({ it.first }, { it.second }) - // Transform the values into maps [ Map> ] .mapValues { it.value.toMap() }, ) -// Maps each SolarXR SkeletonBone to the BodyPart(s) it controls in the skeleton. -// Symmetric bones (legs, arms) map to both left and right sides. -val SKELETON_BONE_TO_BODY_PARTS: Map> = BONE_VALUE_TO_OFFSETS.mapValues { it.value.keys } +private val BONE_OFFSET_TO_VALUES: BodyPartMap> = invertOffsetTable(BONE_VALUE_TO_OFFSETS) +private val BONE_HEAD_OFFSET_TO_VALUES: BodyPartMap> = invertOffsetTable(BONE_VALUE_TO_HEAD_OFFSETS) // Sum of default bone lengths for height-contributing bones // Used to normalize HEIGHT_SCALED_BONE_RATIOS. @@ -107,110 +105,139 @@ fun computeAllDefaultProportionsByBone(height: Float): Map { return nonScaled + heightScaled } -fun Map.toBoneOffsets(): BodyPartMap { - val offsets = bodyPartMap() - // Normal bones - for ((cfg, length) in this) { - val boneOffsets = BONE_VALUE_TO_OFFSETS[cfg] ?: continue - for ((bone, vec) in boneOffsets) { - offsets[bone] = (offsets[bone] ?: Vector3.ZERO) + length * vec - } +// Resolved bone geometry for a set of proportions. tail is the head->tail vector in the bone's own +// frame; head is the parent-tail->head vector in the parent's frame (Vector3.ZERO for most bones). +data class BoneOffsets( + val tail: BodyPartMap, + val head: BodyPartMap, +) + +fun toBoneOffsets(lengths: Map): BoneOffsets { + val tail = bodyPartMap() + val head = bodyPartMap() + for ((cfg, length) in lengths) { + BONE_VALUE_TO_OFFSETS[cfg]?.let { for ((bone, vec) in it) tail[bone] = (tail[bone] ?: Vector3.ZERO) + length * vec } + BONE_VALUE_TO_HEAD_OFFSETS[cfg]?.let { for ((bone, vec) in it) head[bone] = (head[bone] ?: Vector3.ZERO) + length * vec } } - // Fingers - this[SkeletonBone.HAND_Y]?.let { offsets.putAll(getFingerOffsets(it)) } - // Toes - this[SkeletonBone.FOOT_LENGTH]?.let { offsets.putAll(getToeOffsets(it)) } - return offsets + lengths[SkeletonBone.HAND_Y]?.let { + tail.putAll(getFingerOffsets(it)) + head.putAll(getFingerHeadOffsets(it)) + } + lengths[SkeletonBone.FOOT_LENGTH]?.let { + tail.putAll(getToeOffsets(it)) + head.putAll(getToeHeadOffsets(it)) + } + return BoneOffsets(tail, head) } -fun BodyPartMap.toBoneValues(): Map = this - .flatMap { (bone, vec) -> - BONE_OFFSET_TO_VALUES[bone]?.map { (cfg, cfgVec) -> cfg to vec.hadamard(cfgVec).len() } ?: emptyList() +fun toBoneValues(tailOffsets: BodyPartMap, headOffsets: BodyPartMap): Map { + fun invert(offsets: BodyPartMap, table: BodyPartMap>) = offsets.flatMap { (bone, vec) -> + table[bone]?.map { (cfg, cfgVec) -> cfg to vec.hadamard(cfgVec).len() } ?: emptyList() } - .groupBy({ it.first }, { it.second }) - .mapValues { it.value.first() } -// TODO: ?? ^ I don't really know what's going on here, or understand the original intent behind the code, -// but the doubling of proportions was caused by this. It used to be .mapValues { it.value.sum() }, but for `SkeletonBone`s -// that contribute to the offsets of multiple `BodyPart`s (e.g. HIP_WIDTH contributes to LEFT_HIP and RIGHT_HIP, -// UPPER_ARM contributes to LEFT_UPPER_ARM and RIGHT_UPPER_ARM) the offsets are in the values twice. + + return (invert(tailOffsets, BONE_OFFSET_TO_VALUES) + invert(headOffsets, BONE_HEAD_OFFSET_TO_VALUES)) + .groupBy({ it.first }, { it.second }) + .mapValues { it.value.first() } +} fun configToBoneValues(proportions: Map): Map = proportions.mapKeys { SkeletonBone.entries.firstOrNull { cfg -> cfg.name == it.key } ?: SkeletonBone.NONE } +// Fraction of a finger's length taken by each phalanx, hand-ward to tip-ward. Sums to 1. +private val PHALANX_RATIOS = floatArrayOf(0.5f, 0.283f, 0.217f) + /** - * Returns the offsets for the finger bones scaled from the handLength. + * A finger in the hand's rest frame. The hand hangs with the palm toward the thigh, so for the left + * hand +X points to the palm (medial), -Y toward the fingertips, -Z forward; the right hand mirrors X. + * The knuckles fan front-to-back: the index sits forward near the thumb, the little finger back. + * + * @param segments the three bones hand-ward to tip-ward + * @param lengthFraction the whole finger's length as a fraction of handLength + * @param knuckle the first bone's head vs the hand's tail, fractions of handLength + * @param lean how the finger drifts as it extends, per unit of segment length (the -Y fall is on top) */ -private fun getFingerOffsets(handLength: Float) = ( - iterateBodyPartHierarchy(BodyPart.LEFT_HAND, true) + - iterateBodyPartHierarchy(BodyPart.RIGHT_HAND, true) - ).map { it.second }.associateWith { - when (it) { - BodyPart.LEFT_THUMB_METACARPAL, BodyPart.RIGHT_THUMB_METACARPAL, - -> { - val length = handLength * 0.72f * PROXIMAL_RATIO - Vector3(0f, -length, -length * 0.5f) +private class Finger( + val segments: List>, + val lengthFraction: Float, + val knuckle: Vector3, + val lean: Vector3, +) + +private val FINGERS = listOf( + Finger( + listOf( + 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, + ), + lengthFraction = 0.72f, knuckle = Vector3(0.16f, 0.3f, -0.28f), lean = Vector3(0.05f, 0f, -0.7f), + ), + Finger( + listOf( + 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, + ), + lengthFraction = 0.805f, knuckle = Vector3(0.03f, 0.05f, -0.34f), lean = Vector3(0f, 0f, -0.12f), + ), + Finger( + listOf( + 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, + ), + lengthFraction = 0.92f, knuckle = Vector3(0.04f, 0f, -0.11f), lean = Vector3(0f, 0f, -0.02f), + ), + Finger( + listOf( + 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, + ), + lengthFraction = 0.805f, knuckle = Vector3(0.03f, 0.03f, 0.11f), lean = Vector3(0f, 0f, 0.05f), + ), + Finger( + listOf( + 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, + ), + lengthFraction = 0.69f, knuckle = Vector3(0f, 0.1f, 0.31f), lean = Vector3(0f, 0f, 0.14f), + ), +) + +/** + * head to tail vectors for every finger bone, scaled from handLength. The three phalanges of a finger + * share a direction and split the finger's length by [PHALANX_RATIOS]. + */ +private fun getFingerOffsets(handLength: Float): Map = buildMap { + for (finger in FINGERS) { + val fingerLength = handLength * finger.lengthFraction + val dir = Vector3(finger.lean.x, -1f + finger.lean.y, finger.lean.z).unit() + finger.segments.forEachIndexed { i, (left, right) -> + val segment = fingerLength * PHALANX_RATIOS[i] + put(left, dir * segment) + put(right, Vector3(-dir.x, dir.y, dir.z) * segment) } - - BodyPart.LEFT_THUMB_PROXIMAL, BodyPart.RIGHT_THUMB_PROXIMAL, - -> { - val length = handLength * 0.72f * INTERMEDIATE_RATIO - Vector3(0f, -length, -length * 0.5f) - } - - BodyPart.LEFT_THUMB_DISTAL, BodyPart.RIGHT_THUMB_DISTAL, - -> { - val length = handLength * 0.72f * DISTAL_RATIO - Vector3(0f, -length, -length * 0.5f) - } - - BodyPart.LEFT_INDEX_PROXIMAL, BodyPart.RIGHT_INDEX_PROXIMAL, - -> Vector3(0f, -handLength * 0.805f * PROXIMAL_RATIO, 0f) - - BodyPart.LEFT_INDEX_INTERMEDIATE, BodyPart.RIGHT_INDEX_INTERMEDIATE, - -> Vector3(0f, -handLength * 0.805f * INTERMEDIATE_RATIO, 0f) - - BodyPart.LEFT_INDEX_DISTAL, BodyPart.RIGHT_INDEX_DISTAL, - -> Vector3(0f, -handLength * 0.805f * DISTAL_RATIO, 0f) - - BodyPart.LEFT_MIDDLE_PROXIMAL, BodyPart.RIGHT_MIDDLE_PROXIMAL, - -> Vector3(0f, -handLength * 0.92f * PROXIMAL_RATIO, 0f) - - BodyPart.LEFT_MIDDLE_INTERMEDIATE, BodyPart.RIGHT_MIDDLE_INTERMEDIATE, - -> Vector3(0f, -handLength * 0.92f * INTERMEDIATE_RATIO, 0f) - - BodyPart.LEFT_MIDDLE_DISTAL, BodyPart.RIGHT_MIDDLE_DISTAL, - -> Vector3(0f, -handLength * 0.92f * DISTAL_RATIO, 0f) - - BodyPart.LEFT_RING_PROXIMAL, BodyPart.RIGHT_RING_PROXIMAL, - -> Vector3(0f, -handLength * 0.805f * PROXIMAL_RATIO, 0f) - - BodyPart.LEFT_RING_INTERMEDIATE, BodyPart.RIGHT_RING_INTERMEDIATE, - -> Vector3(0f, -handLength * 0.805f * INTERMEDIATE_RATIO, 0f) - - BodyPart.LEFT_RING_DISTAL, BodyPart.RIGHT_RING_DISTAL, - -> Vector3(0f, -handLength * 0.805f * DISTAL_RATIO, 0f) - - BodyPart.LEFT_LITTLE_PROXIMAL, BodyPart.RIGHT_LITTLE_PROXIMAL, - -> Vector3(0f, -handLength * 0.69f * PROXIMAL_RATIO, 0f) - - BodyPart.LEFT_LITTLE_INTERMEDIATE, BodyPart.RIGHT_LITTLE_INTERMEDIATE, - -> Vector3(0f, -handLength * 0.69f * INTERMEDIATE_RATIO, 0f) - - BodyPart.LEFT_LITTLE_DISTAL, BodyPart.RIGHT_LITTLE_DISTAL, - -> Vector3(0f, -handLength * 0.69f * DISTAL_RATIO, 0f) - - else -> error("$it is not expected as child of hands.") } } -// Ratios for finger lengths. They should sum up to 1. -private const val PROXIMAL_RATIO = 0.5f -private const val INTERMEDIATE_RATIO = 0.283f -private const val DISTAL_RATIO = 0.217f +/** + * head offset for each finger's first bone, fanning the knuckles across the palm. + * The other bones chain tail-to-head and are absent here. + */ +private fun getFingerHeadOffsets(handLength: Float): Map = buildMap { + for (finger in FINGERS) { + val (left, right) = finger.segments.first() + val k = finger.knuckle + put(left, Vector3(k.x * handLength, k.y * handLength, k.z * handLength)) + put(right, Vector3(-k.x * handLength, k.y * handLength, k.z * handLength)) + } +} /** - * Returns the offsets for the toe bones scaled from the handLength. + * Returns the offsets for the toe bones scaled from the footLength. */ private fun getToeOffsets(footLength: Float) = ( iterateBodyPartHierarchy(BodyPart.LEFT_FOOT, true) + @@ -218,3 +245,18 @@ private fun getToeOffsets(footLength: Float) = ( ).map { it.second }.associateWith { Vector3(0f, 0f, -footLength * 0.2f) } + +// Head offsets spread the toe roots across the forefoot. X is the foot's medial-lateral axis in +// foot-local space, positive toward the big toe of a left foot. Values are fractions of footLength. +private fun getToeHeadOffsets(footLength: Float): Map = mapOf( + BodyPart.LEFT_BIG_TOE to Vector3(footLength * 0.28f, 0f, 0f), + BodyPart.LEFT_INDEX_TOE to Vector3(footLength * 0.12f, 0f, 0f), + BodyPart.LEFT_MIDDLE_TOE to Vector3(0f, 0f, 0f), + BodyPart.LEFT_RING_TOE to Vector3(footLength * -0.12f, 0f, 0f), + BodyPart.LEFT_LITTLE_TOE to Vector3(footLength * -0.26f, 0f, 0f), + BodyPart.RIGHT_BIG_TOE to Vector3(footLength * -0.28f, 0f, 0f), + BodyPart.RIGHT_INDEX_TOE to Vector3(footLength * -0.12f, 0f, 0f), + BodyPart.RIGHT_MIDDLE_TOE to Vector3(0f, 0f, 0f), + BodyPart.RIGHT_RING_TOE to Vector3(footLength * 0.12f, 0f, 0f), + BodyPart.RIGHT_LITTLE_TOE to Vector3(footLength * 0.26f, 0f, 0f), +) diff --git a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt index 01aa91c8a..f0a825bf7 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt @@ -37,9 +37,12 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when } is SkeletonActions.SetProportions -> { - val bones = action.lengths.toBoneOffsets() + val offsets = toBoneOffsets(action.lengths) val newBones = state.boneInputs.mapValues { bodyPart, bone -> - bone.copy(offset = bones[bodyPart] ?: bone.offset) + bone.copy( + headOffset = offsets.head[bodyPart] ?: bone.headOffset, + offset = offsets.tail[bodyPart] ?: bone.offset, + ) } state.copy(boneInputs = newBones, skeletonHeight = action.lengths.height()) } diff --git a/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-proportions.kt b/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-proportions.kt index da3a6d712..159c501eb 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-proportions.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-proportions.kt @@ -32,8 +32,9 @@ class SkeletonProportionsBehaviour( private val skeleton: Skeleton, ) : SolarXRBridgeBehaviour { private fun buildConfigResponse(boneInputs: InputSkeleton): SkeletonProportionsResponse { - val boneOffsets = BodyPartMap(boneInputs.mapValues { it.value.offset }) - val boneValues = boneOffsets.toBoneValues() + val tailOffsets = BodyPartMap(boneInputs.mapValues { it.value.offset }) + val headOffsets = BodyPartMap(boneInputs.mapValues { it.value.headOffset }) + val boneValues = toBoneValues(tailOffsets, headOffsets) val skeletonParts = boneValues.map { (offset, bone) -> SkeletonPart(offset, bone) } return SkeletonProportionsResponse(skeletonParts = skeletonParts, skeletonHeight = boneValues.height()) } @@ -41,7 +42,11 @@ class SkeletonProportionsBehaviour( override fun observe(receiver: SolarXRBridge) { skeleton.context.state .map { it.boneInputs } - .distinctUntilChanged { old, new -> ALL_BODY_PARTS.all { part -> old[part]?.offset == new[part]?.offset } } + .distinctUntilChanged { old, new -> + ALL_BODY_PARTS.all { part -> + old[part]?.offset == new[part]?.offset && old[part]?.headOffset == new[part]?.headOffset + } + } .drop(1) .onEach { boneInputs -> val configResponse = buildConfigResponse(boneInputs) 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 6585b25b2..68f7d95c0 100644 --- a/server/core/src/main/java/dev/slimevr/util/side.kt +++ b/server/core/src/main/java/dev/slimevr/util/side.kt @@ -18,7 +18,6 @@ val Side.opposite: Side val BodyPart.side: Side? get() = when (this) { - BodyPart.LEFT_HIP, BodyPart.LEFT_UPPER_LEG, BodyPart.LEFT_LOWER_LEG, BodyPart.LEFT_FOOT, @@ -43,7 +42,6 @@ val BodyPart.side: Side? BodyPart.LEFT_LITTLE_DISTAL, -> Side.LEFT - BodyPart.RIGHT_HIP, BodyPart.RIGHT_UPPER_LEG, BodyPart.RIGHT_LOWER_LEG, BodyPart.RIGHT_FOOT, 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 ae8308e63..6d0926e73 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt @@ -35,6 +35,7 @@ private fun assertApprox(expected: Quaternion, actual: Quaternion, message: Stri private fun bone(bodyPart: BodyPart, rotation: Quaternion = Quaternion.IDENTITY, headPosition: Vector3 = Vector3.ZERO) = BoneState( parentBone = null, bodyPart = bodyPart, + headOffset = Vector3.ZERO, offset = Vector3(0f, -0.1f, 0f), rotation = rotation, acceleration = Vector3.ZERO, 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 8cb41ecf0..47bb5f5bd 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt @@ -22,6 +22,7 @@ import kotlin.time.Duration.Companion.seconds private fun bone(bodyPart: BodyPart, rotation: Quaternion = Quaternion.IDENTITY) = BoneState( parentBone = null, bodyPart = bodyPart, + headOffset = Vector3.ZERO, offset = Vector3(0f, -0.1f, 0f), rotation = rotation, acceleration = Vector3.ZERO, diff --git a/solarxr-protocol b/solarxr-protocol index 71e295950..509309fc9 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 71e2959508e022f23d4a0917c34a5ad44d27c7ee +Subproject commit 509309fc9dbe12d9e76bb9d16c9dc8d0875a8ae6