mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-26 11:56:27 -08:00
Rename RawBone to BoneInput
This commit is contained in:
6 files changed
+31
-32
No files matched your search
@@ -17,24 +17,24 @@ import kotlin.math.sin
|
||||
class BoneTransformBehaviour : SkeletonBehaviour {
|
||||
override fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when (action) {
|
||||
is SkeletonActions.SetBoneRotation -> {
|
||||
val bones = state.rawBones.toMutableMap()
|
||||
val bones = state.boneInputs.toMutableMap()
|
||||
val bone = bones[action.bodyPart] ?: return state
|
||||
bones[action.bodyPart] = bone.copy(rawRotation = action.rotation, isActive = true)
|
||||
state.copy(rawBones = bones)
|
||||
state.copy(boneInputs = bones)
|
||||
}
|
||||
|
||||
is SkeletonActions.SetBonePosition -> {
|
||||
val bones = state.rawBones.toMutableMap()
|
||||
val bones = state.boneInputs.toMutableMap()
|
||||
val bone = bones[action.bodyPart] ?: return state
|
||||
bones[action.bodyPart] = bone.copy(rawPosition = action.position, isActive = true)
|
||||
state.copy(rawBones = bones)
|
||||
state.copy(boneInputs = bones)
|
||||
}
|
||||
|
||||
is SkeletonActions.DisableBone -> {
|
||||
val bones = state.rawBones.toMutableMap()
|
||||
val bones = state.boneInputs.toMutableMap()
|
||||
val bone = bones[action.bodyPart] ?: return state
|
||||
bones[action.bodyPart] = bone.copy(isActive = false)
|
||||
state.copy(rawBones = bones)
|
||||
state.copy(boneInputs = bones)
|
||||
}
|
||||
|
||||
else -> state
|
||||
@@ -45,10 +45,10 @@ class ProportionsBehaviour(private val userConfig: UserConfig) : SkeletonBehavio
|
||||
override fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when (action) {
|
||||
is SkeletonActions.SetProportions -> {
|
||||
val bones = action.lengths.toBoneOffsets()
|
||||
val newBones = state.rawBones.mapValues { (bodyPart, bone) ->
|
||||
val newBones = state.boneInputs.mapValues { (bodyPart, bone) ->
|
||||
bone.copy(offset = bones[bodyPart] ?: bone.offset)
|
||||
}
|
||||
state.copy(rawBones = newBones, skeletonHeight = action.lengths.height())
|
||||
state.copy(boneInputs = newBones, skeletonHeight = action.lengths.height())
|
||||
}
|
||||
|
||||
else -> state
|
||||
|
||||
@@ -21,12 +21,12 @@ fun chainCanReach(bones: Map<BodyPart, BoneState>, chain: List<BodyPart>, target
|
||||
}
|
||||
|
||||
// Single loop over a chain
|
||||
fun ccdIk(rawBones: Map<BodyPart, RawBone>, bones: Map<BodyPart, BoneState>, chain: List<BodyPart>, target: Vector3): Map<BodyPart, BoneState> {
|
||||
val workingBones = rawBones.toMutableMap()
|
||||
fun ccdIk(boneInputs: Map<BodyPart, BoneInput>, bones: Map<BodyPart, BoneState>, chain: List<BodyPart>, target: Vector3): Map<BodyPart, BoneState> {
|
||||
val workingBones = boneInputs.toMutableMap()
|
||||
|
||||
// Reversed index, i increases backwards up the chain
|
||||
for ((i, bodyPart) in chain.reversed().withIndex()) {
|
||||
val currentRawBone = requireNotNull(rawBones[bodyPart])
|
||||
val currentRawBone = requireNotNull(boneInputs[bodyPart])
|
||||
val currentBone = requireNotNull(bones[bodyPart])
|
||||
|
||||
val localOffset = currentBone.localTailPosition
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package dev.slimevr.skeleton
|
||||
|
||||
import com.jme3.math.FastMath
|
||||
import dev.slimevr.Phase1ContextProvider
|
||||
import dev.slimevr.context.Behaviour
|
||||
import dev.slimevr.context.Context
|
||||
@@ -11,7 +10,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import solarxr_protocol.rpc.SkeletonBone
|
||||
|
||||
data class RawBone(
|
||||
data class BoneInput(
|
||||
val bodyPart: BodyPart,
|
||||
val offset: Vector3,
|
||||
val rawRotation: Quaternion,
|
||||
@@ -35,20 +34,20 @@ data class BoneState(
|
||||
get() = tailPosition - headPosition
|
||||
}
|
||||
|
||||
data class SkeletonState(val rawBones: Map<BodyPart, RawBone>, val skeletonHeight: Float, val paused: Boolean)
|
||||
data class SkeletonState(val boneInputs: Map<BodyPart, BoneInput>, val skeletonHeight: Float, val paused: Boolean)
|
||||
|
||||
val DEFAULT_SKELETON_STATE: SkeletonState = run {
|
||||
val bones = DEFAULT_BONE_OFFSETS.entries.associate { (bodyPart, tailOffset) ->
|
||||
bodyPart to BoneState(bodyPart = bodyPart, offset = tailOffset)
|
||||
}
|
||||
SkeletonState(
|
||||
rawBones = bones.mapValues { (_, bone) -> RawBone(rawRotation = bone.rotation, bodyPart = bone.bodyPart, offset = bone.offset, rawPosition = Vector3.NULL, isActive = false) },
|
||||
boneInputs = bones.mapValues { (_, bone) -> BoneInput(rawRotation = bone.rotation, bodyPart = bone.bodyPart, offset = bone.offset, rawPosition = Vector3.NULL, isActive = false) },
|
||||
skeletonHeight = DEFAULT_HEIGHT,
|
||||
paused = false,
|
||||
)
|
||||
}
|
||||
|
||||
fun buildBone(bone: RawBone, parentBone: BoneState?, originPosition: Vector3 = Vector3.NULL): BoneState {
|
||||
fun buildBone(bone: BoneInput, parentBone: BoneState?, originPosition: Vector3 = Vector3.NULL): BoneState {
|
||||
val head = parentBone?.tailPosition ?: originPosition
|
||||
return BoneState(
|
||||
bodyPart = bone.bodyPart,
|
||||
@@ -61,7 +60,7 @@ fun buildBone(bone: RawBone, parentBone: BoneState?, originPosition: Vector3 = V
|
||||
}
|
||||
|
||||
fun buildBones(
|
||||
state: Map<BodyPart, RawBone>,
|
||||
state: Map<BodyPart, BoneInput>,
|
||||
rootHead: Vector3 = Vector3.NULL,
|
||||
hierarchy: Sequence<Pair<BodyPart?, BodyPart>> = iterateBodyPartHierarchy(),
|
||||
): Map<BodyPart, BoneState> {
|
||||
@@ -78,7 +77,7 @@ fun buildBones(
|
||||
state: SkeletonState,
|
||||
rootHead: Vector3 = Vector3.NULL,
|
||||
hierarchy: Sequence<Pair<BodyPart?, BodyPart>> = iterateBodyPartHierarchy(),
|
||||
): Map<BodyPart, BoneState> = buildBones(state.rawBones, rootHead, hierarchy)
|
||||
): Map<BodyPart, BoneState> = buildBones(state.boneInputs, rootHead, hierarchy)
|
||||
|
||||
sealed interface SkeletonActions {
|
||||
data class SetBoneRotation(val bodyPart: BodyPart, val rotation: Quaternion) : SkeletonActions
|
||||
|
||||
@@ -101,10 +101,10 @@ class FallbackProcessor : SkeletonProcessor {
|
||||
)
|
||||
|
||||
override fun process(state: SkeletonState): SkeletonState {
|
||||
val rawBones = state.rawBones
|
||||
val rawBones = state.boneInputs
|
||||
|
||||
return state.copy(
|
||||
rawBones = rawBones.mapValues { (bodyPart, bone) ->
|
||||
boneInputs = rawBones.mapValues { (bodyPart, bone) ->
|
||||
if (bone.isActive) return@mapValues bone
|
||||
|
||||
bone.copy(rawRotation = computeFallbackRotation(rawBones, bodyPart))
|
||||
@@ -112,18 +112,18 @@ class FallbackProcessor : SkeletonProcessor {
|
||||
)
|
||||
}
|
||||
|
||||
private fun computeFallbackRotation(rawBones: Map<BodyPart, RawBone>, bodyPart: BodyPart): Quaternion {
|
||||
private fun computeFallbackRotation(boneInputs: Map<BodyPart, BoneInput>, bodyPart: BodyPart): Quaternion {
|
||||
// Fallback bone's rotation
|
||||
missingToFallbacks[bodyPart]
|
||||
?.firstNotNullOfOrNull { part ->
|
||||
rawBones[part]?.takeIf { it.isActive }
|
||||
boneInputs[part]?.takeIf { it.isActive }
|
||||
}
|
||||
?.let { return it.rawRotation }
|
||||
|
||||
// First active parent bone's yaw
|
||||
rawBones[
|
||||
boneInputs[
|
||||
findBodyPartParent(bodyPart) { part ->
|
||||
rawBones[part]?.isActive == true
|
||||
boneInputs[part]?.isActive == true
|
||||
},
|
||||
]?.let { return it.rawRotation.project(Vector3.POS_Y).unit() }
|
||||
|
||||
@@ -153,15 +153,15 @@ class SmoothingProcessor(val settings: Settings) : SkeletonProcessor {
|
||||
|
||||
val alpha = 1 - (SMOOTH_MIN + config.amount.coerceIn(0f, 1f) * (SMOOTH_MAX - SMOOTH_MIN))
|
||||
|
||||
smoothedRotations = state.rawBones.mapValues { (bodyPart, bone) ->
|
||||
smoothedRotations = state.boneInputs.mapValues { (bodyPart, bone) ->
|
||||
(smoothedRotations[bodyPart] ?: bone.rawRotation).lerpR(bone.rawRotation, alpha).unit()
|
||||
}
|
||||
smoothedLengths = state.rawBones.mapValues { (bodyPart, bone) ->
|
||||
smoothedLengths = state.boneInputs.mapValues { (bodyPart, bone) ->
|
||||
val prev = smoothedLengths[bodyPart] ?: bone.offset
|
||||
prev + (bone.offset - prev) * alpha
|
||||
}
|
||||
return state.copy(
|
||||
rawBones = state.rawBones.mapValues { (bodyPart, bone) ->
|
||||
boneInputs = state.boneInputs.mapValues { (bodyPart, bone) ->
|
||||
bone.copy(
|
||||
rawRotation = smoothedRotations[bodyPart] ?: bone.rawRotation,
|
||||
offset = smoothedLengths[bodyPart] ?: bone.offset,
|
||||
@@ -191,7 +191,7 @@ class PredictionProcessor(val settings: Settings) : SkeletonProcessor {
|
||||
if (config.type != FilteringType.PREDICTION) return state
|
||||
|
||||
val newVelocities = mutableMapOf<BodyPart, BoneVelocity>()
|
||||
val newBones = state.rawBones.mapValues { (bodyPart, bone) ->
|
||||
val newBones = state.boneInputs.mapValues { (bodyPart, bone) ->
|
||||
val prev = velocities[bodyPart]
|
||||
if (prev == null) {
|
||||
newVelocities[bodyPart] = BoneVelocity(bone.rawRotation, Quaternion.IDENTITY, bone.offset, Vector3.NULL)
|
||||
@@ -215,6 +215,6 @@ class PredictionProcessor(val settings: Settings) : SkeletonProcessor {
|
||||
)
|
||||
}
|
||||
velocities = newVelocities
|
||||
return state.copy(rawBones = newBones)
|
||||
return state.copy(boneInputs = newBones)
|
||||
}
|
||||
}
|
||||
@@ -161,8 +161,8 @@ class FloorClipProcessor : SkeletonProcessor {
|
||||
fun getDisplacement(footPos: Vector3): Vector3 = Vector3(0f, footPos.y.coerceAtMost(0f), 0f)
|
||||
|
||||
override fun process(state: SkeletonState): SkeletonState {
|
||||
val newBones = state.rawBones.mapValues { (bodyPart, bone) -> }
|
||||
state.rawBones[BodyPart.LEFT_FOOT]?.let {
|
||||
val newBones = state.boneInputs.mapValues { (bodyPart, bone) -> }
|
||||
state.boneInputs[BodyPart.LEFT_FOOT]?.let {
|
||||
val correction = getDisplacement(it.offset)
|
||||
// TODO Apply displacement onto skeleton
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class SkeletonProportionsBehaviour(
|
||||
|
||||
private fun buildConfigResponse(): SkeletonProportionsResponse {
|
||||
val proportions = userConfig.context.state.value.data.proportions
|
||||
val bones = skeleton.context.state.value.rawBones
|
||||
val bones = skeleton.context.state.value.boneInputs
|
||||
val skeletonParts = bones.mapValues { it.value.offset }.toBoneValues().map { (offset, bone) -> SkeletonPart(offset, bone) }
|
||||
val expanded = configToBoneValues(proportions)
|
||||
val userHeight = if (expanded.isNotEmpty()) {
|
||||
|
||||
Reference in new issue
Block a user