mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
improved finger tracking
This commit is contained in:
1 parent
178f40226b
commit
f1578ed3fe
3 files changed
+115
-56
No files matched your search
@@ -14,7 +14,7 @@ private val METACARPAL_THUMB_CONSTRAINT = LooseHingeConstraint(
|
||||
private val THUMB_CONSTRAINT = LooseHingeConstraint(
|
||||
-5f * FastMath.DEG_TO_RAD,
|
||||
100f * FastMath.DEG_TO_RAD,
|
||||
5f * FastMath.DEG_TO_RAD,
|
||||
3f * FastMath.DEG_TO_RAD,
|
||||
Vector3.NEG_X,
|
||||
)
|
||||
|
||||
@@ -27,7 +27,7 @@ private val LEFT_PROXIMAL_FINGER_CONSTRAINT = LooseHingeConstraint(
|
||||
private val LEFT_FINGER_CONSTRAINT = LooseHingeConstraint(
|
||||
-5f * FastMath.DEG_TO_RAD,
|
||||
110f * FastMath.DEG_TO_RAD,
|
||||
0f,
|
||||
3f,
|
||||
Vector3.POS_Z,
|
||||
)
|
||||
private val RIGHT_PROXIMAL_FINGER_CONSTRAINT = LEFT_PROXIMAL_FINGER_CONSTRAINT.copy(hingeAxis = Vector3.NEG_Z)
|
||||
|
||||
@@ -6,36 +6,52 @@ import dev.slimevr.skeleton.SkeletonInputProcessor
|
||||
import dev.slimevr.util.Side
|
||||
import dev.slimevr.util.side
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
|
||||
// TODO only use this for forward/curling. Everything else it should just copy
|
||||
private const val INTERMEDIATE_FROM_PROXIMAL = 2.12f
|
||||
private const val DISTAL_FROM_PROXIMAL = 3.03f
|
||||
private const val INTERMEDIATE_FROM_PROXIMAL_CURL = 2.12f
|
||||
private const val DISTAL_FROM_PROXIMAL_CURL = 3.03f
|
||||
|
||||
private val PROXIMAL_REST_ROTATION = Quaternion.rotationAroundZAxis(0.15f)
|
||||
private val PROXIMAL_THUMB_REST_ROTATION = Quaternion.rotationAroundXAxis(-0.15f)
|
||||
private val THUMB_PARTS = arrayOf(
|
||||
BodyPart.LEFT_THUMB_METACARPAL,
|
||||
BodyPart.LEFT_THUMB_PROXIMAL,
|
||||
BodyPart.LEFT_THUMB_DISTAL,
|
||||
BodyPart.RIGHT_THUMB_METACARPAL,
|
||||
BodyPart.RIGHT_THUMB_PROXIMAL,
|
||||
BodyPart.RIGHT_THUMB_DISTAL,
|
||||
)
|
||||
private const val INTERMEDIATE_FROM_PROXIMAL = 1.04f
|
||||
private const val DISTAL_FROM_PROXIMAL = 1.10f
|
||||
|
||||
private fun getRestRotation(hand: BoneInput, finger: BoneInput, multiplier: Float): Quaternion {
|
||||
val isThumb = finger.bodyPart in THUMB_PARTS
|
||||
val restRotation = if (isThumb) {
|
||||
PROXIMAL_THUMB_REST_ROTATION
|
||||
private const val REST_ANGLE = 0.15f
|
||||
|
||||
private fun getRestRotation(handRotation: Quaternion, axis: Vector3, multiplier: Float): Quaternion {
|
||||
val restRotation = Quaternion.fromRotationVector(axis * REST_ANGLE)
|
||||
return handRotation * restRotation.pow(multiplier)
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpolate the forward, back and side rotation with different ratios: one ratio for forward and one ratio for back and side.
|
||||
*/
|
||||
private fun interpolateForwardBackSide(hand: Quaternion, finger: Quaternion, forwardRatio: Float, backSideRatio: Float, axis: Vector3): Quaternion {
|
||||
// Work with the local rotation to avoid axes being ambiguous
|
||||
val local = finger.localAbout(hand)
|
||||
|
||||
// If forward is going back, actually use the backSideRatio.
|
||||
val actualForwardRatio = if (local.angleAboutR(axis) < 0f) {
|
||||
backSideRatio
|
||||
} else {
|
||||
val side = hand.bodyPart.side
|
||||
if (side == Side.RIGHT) {
|
||||
PROXIMAL_REST_ROTATION.inv()
|
||||
} else {
|
||||
PROXIMAL_REST_ROTATION
|
||||
}
|
||||
forwardRatio
|
||||
}
|
||||
return hand.rotation * restRotation.pow(multiplier)
|
||||
|
||||
// Early return for optimization
|
||||
if (actualForwardRatio == backSideRatio) return hand.interpQ(finger, backSideRatio)
|
||||
|
||||
// Decompose the local rotation into its twist (forward) and its swing (side) along the axis
|
||||
val forward = local.project(axis).unit()
|
||||
val side = local / forward
|
||||
|
||||
// Scale each axis of the local rotation with our ratios
|
||||
val scaledForward = forward.pow(actualForwardRatio)
|
||||
val scaledSide = side.pow(backSideRatio)
|
||||
|
||||
// Recompose the local rotation. We removed forward from local rotation to get side, so we must add forward to side back.
|
||||
val scaledLocal = scaledSide * scaledForward
|
||||
|
||||
// Return scaled local rotation applied to From rotation
|
||||
return scaledLocal.globalAbout(hand)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,59 +63,102 @@ class FingersInputProcessor : SkeletonInputProcessor {
|
||||
val proximal: BodyPart,
|
||||
val intermediate: BodyPart,
|
||||
val distal: BodyPart,
|
||||
val axis: Vector3,
|
||||
val isThumb: Boolean = false,
|
||||
)
|
||||
|
||||
private val fingers = arrayOf(
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_THUMB_METACARPAL, BodyPart.LEFT_THUMB_PROXIMAL, BodyPart.LEFT_THUMB_DISTAL),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_INDEX_PROXIMAL, BodyPart.LEFT_INDEX_INTERMEDIATE, BodyPart.LEFT_INDEX_DISTAL),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_MIDDLE_PROXIMAL, BodyPart.LEFT_MIDDLE_INTERMEDIATE, BodyPart.LEFT_MIDDLE_DISTAL),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_RING_PROXIMAL, BodyPart.LEFT_RING_INTERMEDIATE, BodyPart.LEFT_RING_DISTAL),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_LITTLE_PROXIMAL, BodyPart.LEFT_LITTLE_INTERMEDIATE, BodyPart.LEFT_LITTLE_DISTAL),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_THUMB_METACARPAL, BodyPart.LEFT_THUMB_PROXIMAL, BodyPart.LEFT_THUMB_DISTAL, Vector3.NEG_X, true),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_INDEX_PROXIMAL, BodyPart.LEFT_INDEX_INTERMEDIATE, BodyPart.LEFT_INDEX_DISTAL, Vector3.POS_Z),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_MIDDLE_PROXIMAL, BodyPart.LEFT_MIDDLE_INTERMEDIATE, BodyPart.LEFT_MIDDLE_DISTAL, Vector3.POS_Z),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_RING_PROXIMAL, BodyPart.LEFT_RING_INTERMEDIATE, BodyPart.LEFT_RING_DISTAL, Vector3.POS_Z),
|
||||
Finger(BodyPart.LEFT_HAND, BodyPart.LEFT_LITTLE_PROXIMAL, BodyPart.LEFT_LITTLE_INTERMEDIATE, BodyPart.LEFT_LITTLE_DISTAL, Vector3.POS_Z),
|
||||
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_THUMB_METACARPAL, BodyPart.RIGHT_THUMB_PROXIMAL, BodyPart.RIGHT_THUMB_DISTAL),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_INDEX_PROXIMAL, BodyPart.RIGHT_INDEX_INTERMEDIATE, BodyPart.RIGHT_INDEX_DISTAL),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_MIDDLE_PROXIMAL, BodyPart.RIGHT_MIDDLE_INTERMEDIATE, BodyPart.RIGHT_MIDDLE_DISTAL),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_RING_PROXIMAL, BodyPart.RIGHT_RING_INTERMEDIATE, BodyPart.RIGHT_RING_DISTAL),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_LITTLE_PROXIMAL, BodyPart.RIGHT_LITTLE_INTERMEDIATE, BodyPart.RIGHT_LITTLE_DISTAL),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_THUMB_METACARPAL, BodyPart.RIGHT_THUMB_PROXIMAL, BodyPart.RIGHT_THUMB_DISTAL, Vector3.NEG_X, true),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_INDEX_PROXIMAL, BodyPart.RIGHT_INDEX_INTERMEDIATE, BodyPart.RIGHT_INDEX_DISTAL, Vector3.NEG_Z),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_MIDDLE_PROXIMAL, BodyPart.RIGHT_MIDDLE_INTERMEDIATE, BodyPart.RIGHT_MIDDLE_DISTAL, Vector3.NEG_Z),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_RING_PROXIMAL, BodyPart.RIGHT_RING_INTERMEDIATE, BodyPart.RIGHT_RING_DISTAL, Vector3.NEG_Z),
|
||||
Finger(BodyPart.RIGHT_HAND, BodyPart.RIGHT_LITTLE_PROXIMAL, BodyPart.RIGHT_LITTLE_INTERMEDIATE, BodyPart.RIGHT_LITTLE_DISTAL, Vector3.NEG_Z),
|
||||
)
|
||||
|
||||
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
|
||||
for (finger in fingers) {
|
||||
val hand = mutableInputSkeleton[finger.hand] ?: continue
|
||||
val handRotation = mutableInputSkeleton[finger.hand]?.rotation ?: continue
|
||||
val proximal = mutableInputSkeleton[finger.proximal] ?: continue
|
||||
val intermediate = mutableInputSkeleton[finger.intermediate] ?: continue
|
||||
val distal = mutableInputSkeleton[finger.distal] ?: continue
|
||||
|
||||
// Proximal
|
||||
if (!proximal.isRotationActive) {
|
||||
// Missing proximal bone. Impute it from intermediate > distal > hand
|
||||
val newRotation = if (intermediate.isRotationActive) {
|
||||
hand.rotation.interpQ(intermediate.rotation, 1f / INTERMEDIATE_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
intermediate.rotation,
|
||||
1f / INTERMEDIATE_FROM_PROXIMAL_CURL,
|
||||
1f / INTERMEDIATE_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else if (distal.isRotationActive) {
|
||||
hand.rotation.interpQ(distal.rotation, 1f / DISTAL_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
distal.rotation,
|
||||
1f / DISTAL_FROM_PROXIMAL_CURL,
|
||||
1f / DISTAL_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else {
|
||||
getRestRotation(hand, proximal, 1f)
|
||||
getRestRotation(handRotation, finger.axis, 1f)
|
||||
}
|
||||
mutableInputSkeleton[finger.proximal] = proximal.copy(rotation = newRotation)
|
||||
}
|
||||
|
||||
// Intermediate
|
||||
if (!intermediate.isRotationActive) {
|
||||
// Missing intermediate bone. Impute it from distal > proximal > hand
|
||||
val newRotation = if (distal.isRotationActive) {
|
||||
hand.rotation.interpQ(distal.rotation, INTERMEDIATE_FROM_PROXIMAL / DISTAL_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
distal.rotation,
|
||||
INTERMEDIATE_FROM_PROXIMAL_CURL / DISTAL_FROM_PROXIMAL_CURL,
|
||||
INTERMEDIATE_FROM_PROXIMAL / DISTAL_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else if (proximal.isRotationActive) {
|
||||
hand.rotation.interpQ(proximal.rotation, INTERMEDIATE_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
proximal.rotation,
|
||||
INTERMEDIATE_FROM_PROXIMAL_CURL,
|
||||
INTERMEDIATE_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else {
|
||||
getRestRotation(hand, proximal, INTERMEDIATE_FROM_PROXIMAL)
|
||||
getRestRotation(handRotation, finger.axis, INTERMEDIATE_FROM_PROXIMAL_CURL)
|
||||
}
|
||||
mutableInputSkeleton[finger.intermediate] = intermediate.copy(rotation = newRotation)
|
||||
}
|
||||
|
||||
// Distal
|
||||
if (!distal.isRotationActive) {
|
||||
// Missing distal bone. Impute it from intermediate > proximal > hand
|
||||
val newRotation = if (intermediate.isRotationActive) {
|
||||
hand.rotation.interpQ(intermediate.rotation, DISTAL_FROM_PROXIMAL / INTERMEDIATE_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
intermediate.rotation,
|
||||
DISTAL_FROM_PROXIMAL_CURL / INTERMEDIATE_FROM_PROXIMAL_CURL,
|
||||
DISTAL_FROM_PROXIMAL / INTERMEDIATE_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else if (proximal.isRotationActive) {
|
||||
hand.rotation.interpQ(proximal.rotation, DISTAL_FROM_PROXIMAL)
|
||||
interpolateForwardBackSide(
|
||||
handRotation,
|
||||
proximal.rotation,
|
||||
DISTAL_FROM_PROXIMAL_CURL,
|
||||
DISTAL_FROM_PROXIMAL,
|
||||
finger.axis,
|
||||
)
|
||||
} else {
|
||||
getRestRotation(hand, proximal, DISTAL_FROM_PROXIMAL)
|
||||
getRestRotation(handRotation, finger.axis, DISTAL_FROM_PROXIMAL_CURL)
|
||||
}
|
||||
mutableInputSkeleton[finger.distal] = distal.copy(rotation = newRotation)
|
||||
}
|
||||
|
||||
@@ -239,22 +239,22 @@ private fun interpolateRatio(fromUpperToLower: Float, curvature: Float, fromReli
|
||||
private fun interpolateForwardSide(fromRotation: Quaternion, toRotation: Quaternion, forwardRatio: Float, sideRatio: Float?): Quaternion {
|
||||
if (sideRatio == null) return fromRotation.interpQ(toRotation, forwardRatio)
|
||||
|
||||
// Work with the rotation delta to avoid axes being ambiguous
|
||||
val delta = fromRotation.inv() * toRotation
|
||||
// Work with the local rotation to avoid axes being ambiguous
|
||||
val local = toRotation.localAbout(fromRotation)
|
||||
|
||||
// Decompose the delta into its twist (forward) and swing (remaining/sides) along the x-axis
|
||||
val forward = delta.project(Vector3.POS_X).unit()
|
||||
val side = delta / forward
|
||||
// Decompose the local rotation into its twist (forward) and swing (remaining/sides) along the x-axis
|
||||
val forward = local.project(Vector3.POS_X).unit()
|
||||
val side = local / forward
|
||||
|
||||
// Scale each axis of the delta with our ratios
|
||||
// Scale each axis of the local rotation with our ratios
|
||||
val scaledForward = forward.pow(forwardRatio)
|
||||
val scaledSide = side.pow(sideRatio)
|
||||
|
||||
// Recompose the delta. We removed formward from delta to get side, so we must add forward to side back.
|
||||
val scaledDelta = scaledSide * scaledForward
|
||||
// Recompose the local rotation. We removed forward from local rotation to get side, so we must add forward to side back.
|
||||
val scaledLocal = scaledSide * scaledForward
|
||||
|
||||
// Return scaled delta applied to From rotation
|
||||
return fromRotation * scaledDelta
|
||||
// Return scaled local rotation applied to From rotation
|
||||
return scaledLocal.globalAbout(fromRotation)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user