mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
Performance optimizations on skeleton
This commit is contained in:
1 parent
3d1bb1629b
commit
5e689f4f28
9 files changed
+42
-25
No files matched your search
@@ -187,10 +187,11 @@ class ComputedSkeletonBehaviour(
|
||||
val waiter: PreciseWaiter,
|
||||
) : SkeletonBehaviour {
|
||||
private val ALWAYS_ACTIVE_PARTS = arrayOf(BodyPart.HEAD, BodyPart.NECK)
|
||||
private val IK_THRESHOLD = 0.005f // 0.002 would be ideal for accuracy, but we need performance
|
||||
private val IK_THRESHOLD = 0.005f // 0.002 would be ideal for accuracy, but we want performance
|
||||
private val IK_MAX_ITERATIONS = 100
|
||||
|
||||
private val intervalDuration = (1.0 / hz).seconds
|
||||
|
||||
/** Shortest gap between two tick starts before the later one is pushed to the next slot */
|
||||
private val minimumGap = intervalDuration * 0.75
|
||||
|
||||
|
||||
@@ -101,11 +101,17 @@ inline fun BodyPart.findFirstParent(predicate: (BodyPart) -> Boolean): BodyPart?
|
||||
val headPartSet = setOf(BodyPart.HEAD)
|
||||
fun highestBodyParts(bodyParts: Set<BodyPart>): Set<BodyPart> {
|
||||
if (BodyPart.HEAD in bodyParts) return headPartSet
|
||||
|
||||
// Construct boolean array once
|
||||
val bodyPartsPresent = BooleanArray(BodyPart.entries.size)
|
||||
for (bodyPart in bodyParts) bodyPartsPresent[bodyPart.ordinal] = true
|
||||
|
||||
val result = bodyParts.toMutableSet()
|
||||
for (bodyPart in bodyParts) {
|
||||
var parent = parentOf(bodyPart)
|
||||
while (parent != null) {
|
||||
if (parent in bodyParts) {
|
||||
// Use the array here for performance
|
||||
if (bodyPartsPresent[parent.ordinal]) {
|
||||
result.remove(bodyPart)
|
||||
break
|
||||
}
|
||||
@@ -115,7 +121,7 @@ fun highestBodyParts(bodyParts: Set<BodyPart>): Set<BodyPart> {
|
||||
return result
|
||||
}
|
||||
|
||||
private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List<Pair<BodyPart?, BodyPart>> {
|
||||
private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): Array<Pair<BodyPart?, BodyPart>> {
|
||||
val result = mutableListOf<Pair<BodyPart?, BodyPart>>()
|
||||
fun visit(parentBone: BodyPart?, bone: BodyPart, skipSelf: Boolean) {
|
||||
if (!skipSelf) result += parentBone to bone
|
||||
@@ -123,16 +129,16 @@ private fun buildHierarchy(root: BodyPart, onlyChildren: Boolean): List<Pair<Bod
|
||||
for (child in children) visit(bone, child, false)
|
||||
}
|
||||
visit(null, root, onlyChildren)
|
||||
return result
|
||||
return result.toTypedArray()
|
||||
}
|
||||
|
||||
// The hierarchy is constant, so every traversal of it is too. Derived once for the same reason
|
||||
// BODY_PART_PARENTS is: walking it lazily per call cost a continuation and a Pair per bone, and
|
||||
// BoneYawFallbackProcessor asks for one per active bone per frame.
|
||||
private val BODY_PART_HIERARCHIES: Array<List<Pair<BodyPart?, BodyPart>>> =
|
||||
// FallbackProcessor asks for one per active bone per frame.
|
||||
private val BODY_PART_HIERARCHIES: Array<Array<Pair<BodyPart?, BodyPart>>> =
|
||||
Array(ALL_BODY_PARTS.size * 2) { index -> buildHierarchy(ALL_BODY_PARTS[index / 2], index % 2 == 1) }
|
||||
|
||||
fun iterateBodyPartHierarchy(
|
||||
root: BodyPart = BodyPart.HEAD,
|
||||
onlyChildren: Boolean = false,
|
||||
): List<Pair<BodyPart?, BodyPart>> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0]
|
||||
): Array<Pair<BodyPart?, BodyPart>> = BODY_PART_HIERARCHIES[root.ordinal * 2 + if (onlyChildren) 1 else 0]
|
||||
@@ -61,8 +61,8 @@ val CHILD_FALLBACKS = mapOf(
|
||||
*/
|
||||
class FallbackInputProcessor : SkeletonInputProcessor {
|
||||
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
|
||||
mutableInputSkeleton.forEachBone { bodyPart, bone ->
|
||||
if (bone.isRotationActive && bone.isAccelerationActive) return@forEachBone
|
||||
mutableInputSkeleton.forEach { (bodyPart, bone) ->
|
||||
if (bone.isRotationActive && bone.isAccelerationActive) return@forEach
|
||||
val newRotation = if (!bone.isRotationActive) {
|
||||
val fallbackPart = bodyPart.findFirstParent { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
?: CHILD_FALLBACKS[bodyPart]?.firstOrNull { mutableInputSkeleton[it]?.isRotationActive == true }
|
||||
|
||||
@@ -10,7 +10,7 @@ import io.github.axisangles.ktmath.Vector3
|
||||
class TrackerOffsetInputProcessor : SkeletonInputProcessor {
|
||||
override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) {
|
||||
mutableInputSkeleton.forEach { (part, input) ->
|
||||
if (input.trackerOffset == Vector3.ZERO) return@forEach
|
||||
if (input.trackerOffset.isZero()) return@forEach
|
||||
val localOffset = input.rotation.sandwich(input.trackerOffset)
|
||||
mutableInputSkeleton[part] = input.copy(position = input.position?.plus(localOffset))
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ data class BoneState(
|
||||
// computed on demand rather than per bone
|
||||
private val orientationOffset: Quaternion
|
||||
get() = when {
|
||||
offset.len() == 0f -> Quaternion.IDENTITY
|
||||
offset.isZero() -> Quaternion.IDENTITY
|
||||
offset.unit().y == 1f -> Quaternion.I
|
||||
else -> Quaternion.fromTo(Vector3.NEG_Y, offset)
|
||||
}
|
||||
@@ -141,9 +141,14 @@ val DEFAULT_SKELETON_STATE = run {
|
||||
}
|
||||
|
||||
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?.let { it.tailPosition + it.rotation.sandwich(bone.headOffset) }
|
||||
?: bone.position ?: Vector3.ZERO
|
||||
// Position of the bone input is used for the root since it has no parent
|
||||
val headPosition = parentBone?.let {
|
||||
if (bone.headOffset.isZero()) {
|
||||
it.tailPosition
|
||||
} else {
|
||||
it.tailPosition + it.rotation.sandwich(bone.headOffset)
|
||||
}
|
||||
} ?: bone.position ?: Vector3.ZERO
|
||||
return BoneState(
|
||||
parentBone = parentBone,
|
||||
bodyPart = bone.bodyPart,
|
||||
@@ -154,6 +159,7 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO
|
||||
acceleration = bone.acceleration,
|
||||
headPosition = headPosition,
|
||||
tailPosition = headPosition + bone.rotation.sandwich(bone.offset),
|
||||
// Velocity is written directly during the skeleton loop computed bones; keep it.
|
||||
velocity = velocity,
|
||||
)
|
||||
}
|
||||
@@ -163,10 +169,9 @@ fun buildBone(bone: BoneInput, parentBone: BoneState?, velocity: Velocity = ZERO
|
||||
*/
|
||||
fun buildBones(mutableBones: BodyPartMap<BoneState>, boneInputs: InputSkeleton, changedParts: Set<BodyPart> = headPartSet) {
|
||||
for (bodyPart in highestBodyParts(changedParts)) {
|
||||
for ((parentPart, childPart) in iterateBodyPartHierarchy(parentOf(bodyPart) ?: bodyPart, bodyPart != BodyPart.HEAD)) {
|
||||
for ((parentPart, childPart) in iterateBodyPartHierarchy(bodyPart, false)) {
|
||||
val rawBone = boneInputs[childPart] ?: continue
|
||||
val parentBone = parentPart?.let { mutableBones[it] }
|
||||
// Velocity is written directly during the skeleton loop computed bones; keep it.
|
||||
val parentBone = (parentPart ?: parentOf(childPart))?.let { mutableBones[it] }
|
||||
mutableBones[childPart] = buildBone(rawBone, parentBone, mutableBones[childPart]?.velocity ?: ZERO_VELOCITY)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,9 +32,11 @@ class FloorClipTargetProcessor(
|
||||
if (target.y < lowestTargetY) lowestTargetY = target.y
|
||||
}
|
||||
|
||||
// Snap the parent up
|
||||
val offsetY = (floorLevel - lowestTargetY).coerceAtLeast(0f)
|
||||
mutableIkTargets[parentPart] = Vector3(parentTarget.x, parentTarget.y + offsetY, parentTarget.z)
|
||||
val offsetY = floorLevel - lowestTargetY
|
||||
if (offsetY > 0f) {
|
||||
// Snap the parent up
|
||||
mutableIkTargets[parentPart] = Vector3(parentTarget.x, parentTarget.y + offsetY, parentTarget.z)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -37,8 +37,8 @@ import dev.slimevr.solarxr.rpc.SkeletonSettingsBehaviour
|
||||
import dev.slimevr.solarxr.rpc.StayAlignedBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TapDetectionBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TelemetryBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TrackerImuTelemetryBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TimeoutSettingsBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TrackerImuTelemetryBehaviour
|
||||
import dev.slimevr.solarxr.rpc.TrackingChecklistBehaviour
|
||||
import dev.slimevr.solarxr.rpc.UpdateTrackerBehaviour
|
||||
import dev.slimevr.solarxr.rpc.VmcBehaviour
|
||||
|
||||
@@ -75,6 +75,8 @@ class Vector3(val x: Float, val y: Float, val z: Float) {
|
||||
**/
|
||||
fun len() = sqrt(x * x + y * y + z * z)
|
||||
|
||||
fun isZero() = x == 0f && y == 0f && z == 0f
|
||||
|
||||
/**
|
||||
* @return the normalized vector
|
||||
**/
|
||||
|
||||
Reference in new issue
Block a user