diff --git a/gui/app/public/i18n/en/translation.ftl b/gui/app/public/i18n/en/translation.ftl index 87f5df36f..9c424647e 100644 --- a/gui/app/public/i18n/en/translation.ftl +++ b/gui/app/public/i18n/en/translation.ftl @@ -593,20 +593,10 @@ settings-general-mounting_method = Mounting calibration method settings-general-mounting_method-pose_only = Only used by pose mounting calibration. settings-general-mounting_method-description = What mounting calibration method to use to calibrate your trackers' mounting orientations. settings-general-tracker_mechanics-filtering = Filtering -settings-general-tracker_mechanics-filtering-type = Filtering type -# This also cares about multilines -settings-general-tracker_mechanics-filtering-description = - Choose the filtering type for your trackers. - "{ settings-general-tracker_mechanics-filtering-type-smoothing }" makes movement smoother while "{ settings-general-tracker_mechanics-filtering-type-prediction }" minimizes latency. -settings-general-tracker_mechanics-filtering-type-none = No filtering -settings-general-tracker_mechanics-filtering-type-none-description = Use rotations as is. Will not do any filtering. -settings-general-tracker_mechanics-filtering-type-smoothing = Smoothing -settings-general-tracker_mechanics-filtering-type-smoothing-description = Smooths movements but adds some latency. -settings-general-tracker_mechanics-filtering-type-prediction = Prediction -settings-general-tracker_mechanics-filtering-type-prediction-description = Reduces latency and makes movements snappier, but increases jitter. -settings-general-tracker_mechanics-filtering-amount = Filter strength -settings-general-tracker_mechanics-filtering-amount-description = How strong the selected filter is. Higher values increase smoothing or prediction while lower ones make the effect more subtle. -settings-general-tracker_mechanics-filtering-amount-no_filtering = Pick a filtering type to set its strength. +settings-general-tracker_mechanics-filtering-smoothing-strength = Smoothing strength +settings-general-tracker_mechanics-filtering-smoothing-strength-description = Smooths out movement. Set to 0 to disable. +settings-general-tracker_mechanics-filtering-prediction-strength = Prediction strength +settings-general-tracker_mechanics-filtering-prediction-strength-description = Compensates for latency. Set to 0 to disable. settings-general-fk_settings-reset_behaviour_group = Reset behaviour settings-general-tracker_mechanics-yaw-reset-smooth-time = Yaw reset smooth time settings-general-tracker_mechanics-yaw-reset-smooth-time-description = Smooths the trackers' rotations upon doing a yaw reset. 0s disables smoothing. diff --git a/gui/app/src/components/settings/pages/TrackingSettings.tsx b/gui/app/src/components/settings/pages/TrackingSettings.tsx index 96c2832f3..218efc4b4 100644 --- a/gui/app/src/components/settings/pages/TrackingSettings.tsx +++ b/gui/app/src/components/settings/pages/TrackingSettings.tsx @@ -3,9 +3,7 @@ import { useEffect } from 'react'; import { FieldPath, useForm } from 'react-hook-form'; import { ChangeSkeletonSettingsRequestT, - FilteringType, RpcMessage, - SkeletonFilteringT, SkeletonRatiosT, SkeletonSettingsRequestT, SkeletonSettingsResponseT, @@ -16,7 +14,6 @@ import { useSettingsDefaults } from '@/hooks/settings-defaults'; import { useLocaleConfig } from '@/i18n/config'; import { CheckBox } from '@/components/commons/Checkbox'; import { NumberSelector } from '@/components/commons/NumberSelector'; -import { Radio } from '@/components/commons/Radio'; import { SettingsPagePaneLayout } from '@/components/settings/SettingsPagePaneLayout'; import { settingReset } from '@/components/settings/SettingReset'; import { @@ -41,15 +38,13 @@ type SkeletonForm = { enforceConstraints: boolean; }; ratios: { + smoothingStrength: number; + predictionStrength: number; imputeSpineFromUpperToLower: number; spineCurvature: number; interpolateUpperLegsTwistWithLowerLegs: number; skatingCorrectionStrength: number; }; - filtering: { - type: number; - amount: number; - }; }; // Exported so other components (e.g. StayAlignedSettings' debug copy) can @@ -64,7 +59,7 @@ const skeletonSettingsValueAtom = selectAtom( ); function toForm(settings: SkeletonSettingsResponseT): SkeletonForm { - const { toggles, ratios, filtering } = settings; + const { toggles, ratios } = settings; return { toggles: { floorClip: toggles?.floorClip ?? false, @@ -76,16 +71,14 @@ function toForm(settings: SkeletonSettingsResponseT): SkeletonForm { enforceConstraints: toggles?.enforceConstraints ?? false, }, ratios: { + smoothingStrength: ratios?.smoothingStrength ?? 0, + predictionStrength: ratios?.predictionStrength ?? 0, imputeSpineFromUpperToLower: ratios?.imputeSpineFromUpperToLower ?? 0, spineCurvature: ratios?.spineCurvature ?? 0, interpolateUpperLegsTwistWithLowerLegs: ratios?.interpolateUpperLegsTwistWithLowerLegs ?? 0, skatingCorrectionStrength: ratios?.skatingCorrectionStrength ?? 0, }, - filtering: { - type: filtering?.type ?? FilteringType.NONE, - amount: filtering?.amount ?? 0, - }, }; } @@ -96,15 +89,17 @@ export const trackingSettingsPage = defineSettingsPage({ filtering: { title: 'settings-general-tracker_mechanics-filtering', rows: { - type: { - title: 'settings-general-tracker_mechanics-filtering-type', + smoothing: { + title: + 'settings-general-tracker_mechanics-filtering-smoothing-strength', description: - 'settings-general-tracker_mechanics-filtering-description', + 'settings-general-tracker_mechanics-filtering-smoothing-strength-description', }, - amount: { - title: 'settings-general-tracker_mechanics-filtering-amount', + prediction: { + title: + 'settings-general-tracker_mechanics-filtering-prediction-strength', description: - 'settings-general-tracker_mechanics-filtering-amount-description', + 'settings-general-tracker_mechanics-filtering-prediction-strength-description', }, }, }, @@ -197,7 +192,6 @@ const { groups } = trackingSettingsPage; export function TrackingSettings() { const setSettings = useSetAtom(skeletonSettingsAtom); const settings = useAtomValue(skeletonSettingsValueAtom); - const { l10n } = useLocalization(); const { currentLocales } = useLocaleConfig(); const { sendRPCPacket, useRPCPacket, useConnectedEffect } = useWebsocketAPI(); const defaults = useSettingsDefaults().skeleton; @@ -228,6 +222,8 @@ export function TrackingSettings() { settingsReq.toggles = toggles; const ratios = new SkeletonRatiosT(); + ratios.smoothingStrength = values.ratios.smoothingStrength; + ratios.predictionStrength = values.ratios.predictionStrength; ratios.imputeSpineFromUpperToLower = values.ratios.imputeSpineFromUpperToLower; ratios.spineCurvature = values.ratios.spineCurvature; @@ -236,11 +232,6 @@ export function TrackingSettings() { ratios.skatingCorrectionStrength = values.ratios.skatingCorrectionStrength; settingsReq.ratios = ratios; - const filtering = new SkeletonFilteringT(); - filtering.type = values.filtering.type; - filtering.amount = values.filtering.amount; - settingsReq.filtering = filtering; - sendRPCPacket(RpcMessage.ChangeSkeletonSettingsRequest, settingsReq); }; @@ -270,7 +261,6 @@ export function TrackingSettings() { } ); - const filteringEnabled = watch('filtering.type') !== FilteringType.NONE; const skatingCorrection = watch('toggles.skatingCorrection'); const resetTo = (name: FieldPath) => @@ -320,52 +310,13 @@ export function TrackingSettings() {
- - - -
- } - > - - + setting={groups.filtering.rows.smoothing} + {...stepper('ratios.smoothingStrength')} + /> + diff --git a/gui/app/src/hooks/onboarding.ts b/gui/app/src/hooks/onboarding.ts index 1f7184193..7c9364c86 100644 --- a/gui/app/src/hooks/onboarding.ts +++ b/gui/app/src/hooks/onboarding.ts @@ -117,7 +117,6 @@ export function useProvideOnboarding() { const skeletonReq = new ChangeSkeletonSettingsRequestT(); skeletonReq.toggles = toggles; skeletonReq.ratios = skeletonSettings.ratios; - skeletonReq.filtering = skeletonSettings.filtering; sendRPCPacket(RpcMessage.ChangeSkeletonSettingsRequest, skeletonReq); setResetsSettings({ 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 5b8e11422..e782dfe66 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -13,7 +13,6 @@ import kotlinx.serialization.json.jsonPrimitive import solarxr_protocol.datatypes.BodyPart import solarxr_protocol.datatypes.MountingMethod import solarxr_protocol.rpc.ArmsMountingResetMode -import solarxr_protocol.rpc.FilteringType import solarxr_protocol.rpc.KeybindId import solarxr_protocol.rpc.RoutingOutput @@ -117,24 +116,18 @@ data class SkeletonTogglesConfig( // Used in SkeletonConfig @Serializable data class SkeletonRatiosConfig( + val smoothingStrength: Float = 0.1f, + val predictionStrength: Float = 0.2f, val spineCurvature: Float = 0.75f, val imputeSpineFromUpperToLower: Float = DEFAULT_SPINE_UPPER_LOWER, val interpolateUpperLegsTwistWithLowerLegs: Float = 0.85f, val skatingCorrectionStrength: Float = 0.5f, ) -// Used in SkeletonConfig -@Serializable -data class SkeletonFilteringConfig( - val type: FilteringType = FilteringType.NONE, - val amount: Float = 0.25f, -) - @Serializable data class SkeletonConfig( val toggles: SkeletonTogglesConfig = SkeletonTogglesConfig(), val ratios: SkeletonRatiosConfig = SkeletonRatiosConfig(), - val filtering: SkeletonFilteringConfig = SkeletonFilteringConfig(), ) const val DEFAULT_VRC_OSC_PORT_OUT: Int = 9000 diff --git a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt index 6795ab3ba..5fbaa1eae 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/computedprocessors/velocity.kt @@ -44,8 +44,7 @@ private fun smoothVelocity(currentVelocity: Velocity, lastVelocity: Velocity, de class VelocityComputedProcessor : SkeletonComputedProcessor, ResettableSkeletonProcessor { - private val lastVelocities: BodyPartMap = bodyPartMap() - private val lastVelocityData: BodyPartMap = bodyPartMap() + private val lastVelocities: BodyPartMap> = bodyPartMap() private var lastProcessTime = timeSource.markNow() override fun process(mutableComputedSkeleton: ComputedSkeleton, inputSkeleton: InputSkeleton) { @@ -57,18 +56,17 @@ class VelocityComputedProcessor : for ((bodyPart, bone) in mutableComputedSkeleton) { // Compute current velocity val currentVelocityData = VelocityData(bone.rotation, bone.tailPosition) - val currentVelocity = lastVelocityData[bodyPart]?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY + val currentVelocity = lastVelocities[bodyPart]?.second?.let { computeVelocity(currentVelocityData, it, deltaTime) } ?: ZERO_VELOCITY - val smoothedVelocity = smoothVelocity(currentVelocity, lastVelocities[bodyPart] ?: ZERO_VELOCITY, deltaTime) + val smoothedVelocity = smoothVelocity(currentVelocity, lastVelocities[bodyPart]?.first ?: ZERO_VELOCITY, deltaTime) mutableComputedSkeleton[bodyPart] = bone.copy(velocity = smoothedVelocity) - lastVelocityData[bodyPart] = currentVelocityData - lastVelocities[bodyPart] = smoothedVelocity + lastVelocities[bodyPart] = smoothedVelocity to currentVelocityData } } override fun reset(resetType: ResetType) { lastVelocities.clear() - lastVelocityData.clear() + lastProcessTime = timeSource.markNow() } } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/filtering.kt similarity index 62% rename from server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt rename to server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/filtering.kt index 27ee14505..3a81545ed 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upsampling.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/filtering.kt @@ -1,5 +1,7 @@ package dev.slimevr.skeleton.inputprocessors +import com.jme3.math.FastMath +import dev.slimevr.config.Settings import dev.slimevr.skeleton.BodyPartMap import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.ResettableSkeletonProcessor @@ -25,17 +27,40 @@ private data class BoneSamples( val runningSample: BoneSample?, val targetSample: BoneSample?, val timeBetweenPoses: Duration?, + val predictionRotation: Quaternion?, val timeLeftForLastPose: Duration = Duration.ZERO, ) +// TODO fine tune these values // Smoothing -private const val TIME_CARRYOVER_DECAY = 0.9 // Carries over 90% of the last pose's remaining time +private const val TIME_CARRYOVER_DECAY_MIN = 0.8f +private const val TIME_CARRYOVER_DECAY_MAX = 0.1f + +// Smaller = more smoother +private const val ROTATION_SMOOTHING_MIN = 0.3f +private const val ROTATION_SMOOTHING_MAX = 0.04f // Prediction -private const val EXTRAPOLATION_LOG_BASE = 5.0 // Higher = less extrapolation -private const val EXTRAPOLATION_MAX = 1.0 // Allows 5 seconds +private const val EXTRAPOLATION_MAX_SECONDS = 4.5 + +// Higher = less extrapolation +private const val EXTRAPOLATION_LOG_BASE_MIN = 8.0f +private const val EXTRAPOLATION_LOG_BASE_MAX = 4.0f + +// Higher = more prediction +private const val ROTATION_PREDICTION_MIN = 0f +private const val ROTATION_PREDICTION_MAX = 0.95f + +// Lower = more biased towards recent rotations +private const val PREVIOUS_PREDICTION_DECAY = 0.2f + +// TODO this could use 'some' (A LOT OF) refactoring +// pls don't judge the code quality, I was just trying to make it work for now! /** + * This processor handles upsampling, smoothing and prediction, all in one processor for optimization's sake. + * + * ----- Upsampling ----- * Since the skeleton runs at a different rate than trackers (e.g: 500hz vs 100hz), * this processor handles interpolating data received from trackers across skeleton frames up * until the next tracker data (e.g: interpolating the 100hz data across 5 frames to reach 500hz). @@ -48,14 +73,28 @@ private const val EXTRAPOLATION_MAX = 1.0 // Allows 5 seconds * * With this, processors don't have to care that trackers only send at 100hz and can know that the data from one skeleton * frame to another will have reliably changed according to the IRL movements. + * + * ----- Smoothing ----- + * Smoothing decides how fast to get to the target rotation by just scaling down the interpolation ratio. + * + * ----- Prediction ----- + * Prediction decides how far away the target rotation is using an average of past rotation deltas. */ -class UpsamplingInputProcessor : +class FilteringInputProcessor(val settings: Settings) : SkeletonInputProcessor, ResettableSkeletonProcessor { private val boneSamples: BodyPartMap = bodyPartMap() override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { val now = timeSource.markNow() + val skeletonRatios = settings.context.state.value.data.skeletonConfig.ratios + val smoothingStrength = skeletonRatios.smoothingStrength + val predictionStrength = skeletonRatios.predictionStrength + val timeCarryoverDecay = FastMath.remapClamped(smoothingStrength, 0f, 1f, TIME_CARRYOVER_DECAY_MIN, TIME_CARRYOVER_DECAY_MAX).toDouble() + val rotationSmoothingMultiplier = FastMath.remapClamped(smoothingStrength, 0f, 1f, ROTATION_SMOOTHING_MIN, ROTATION_SMOOTHING_MAX) + val extrapolationLogBase = FastMath.remapClamped(predictionStrength, 0f, 1f, EXTRAPOLATION_LOG_BASE_MIN, EXTRAPOLATION_LOG_BASE_MAX).toDouble() + val rotationPredictionMultiplier = FastMath.remapClamped(predictionStrength, 0f, 1f, ROTATION_PREDICTION_MIN, ROTATION_PREDICTION_MAX) + for ((bodyPart, bone) in mutableInputSkeleton) { if (bone.expectedTps == null || bone.poseTime == null) continue @@ -71,13 +110,22 @@ class UpsamplingInputProcessor : val timeBetweenLastPoses = bone.poseTime - targetPoseTime oldSamples.runningSample?.poseTime?.let { runningPoseTime -> // Add some of last pose's remaining time - val lastPoseCarryoverTime = oldSamples.timeLeftForLastPose * TIME_CARRYOVER_DECAY + val lastPoseCarryoverTime = oldSamples.timeLeftForLastPose * timeCarryoverDecay val timeSinceRunningPose = now - runningPoseTime (timeBetweenLastPoses + lastPoseCarryoverTime - timeSinceRunningPose).coerceAtLeast(Duration.ZERO) } ?: timeBetweenLastPoses } + val predictionRotation = oldSamples?.targetSample?.rotation?.let { previousRotation -> + if (predictionStrength > 0f) { + val previousPrediction = oldSamples.predictionRotation?.pow(PREVIOUS_PREDICTION_DECAY) ?: Quaternion.IDENTITY + val currentDelta = (previousRotation.inv() * targetSample.rotation).pow(rotationPredictionMultiplier) + previousPrediction * currentDelta + } else { + null + } + } - BoneSamples(fromSample, null, targetSample, timeBetweenPoses) + BoneSamples(fromSample, null, targetSample, timeBetweenPoses, predictionRotation) } else { oldSamples } @@ -89,16 +137,23 @@ class UpsamplingInputProcessor : // Time between now and tracker data. val timeSinceLastPose = now - bone.poseTime // Ratio of how close we should be to the next pose. - // 0 = we just got started, 1 = we should have the next one, >1 = we didn't get it yet, packet loss? + // 0 = we just got started, 1 = we should have the next one, >1 = we didn't get it yet; packet lost or late val rawRatio = timeSinceLastPose / timeBetweenPoses // In case the rawRatio is >1, we extrapolate using log to keep some smooth perceived movement. - val extrapolationAmount = log((rawRatio).coerceAtLeast(1.0), EXTRAPOLATION_LOG_BASE).coerceAtMost(EXTRAPOLATION_MAX) + val extrapolationAmount = log((rawRatio).coerceIn(1.0, EXTRAPOLATION_MAX_SECONDS), extrapolationLogBase) // Resulting ratio to use for interpolation, with extrapolation if any. val interpolationRatio = (rawRatio.coerceAtMost(1.0) + extrapolationAmount).toFloat() // Upsample rotation, acceleration and position. val rotation = if (bone.isRotationActive) { - fromSample.rotation.interpQ(targetSample.rotation, interpolationRatio) + val rotationInterpolationRatio = if (smoothingStrength > 0f) { + val extrapolationAmount = log((rawRatio * rotationSmoothingMultiplier).coerceIn(1.0, EXTRAPOLATION_MAX_SECONDS), extrapolationLogBase) + ((rawRatio * rotationSmoothingMultiplier).coerceAtMost(1.0) + extrapolationAmount).toFloat() + } else { + interpolationRatio + } + val targetRotation = samples.predictionRotation?.let { targetSample.rotation * it } ?: targetSample.rotation + fromSample.rotation.interpQ(targetRotation, rotationInterpolationRatio) } else { targetSample.rotation } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt deleted file mode 100644 index 07e7d2387..000000000 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-prediction.kt +++ /dev/null @@ -1,79 +0,0 @@ -package dev.slimevr.skeleton.inputprocessors - -import dev.slimevr.config.Settings -import dev.slimevr.skeleton.BodyPartMap -import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.ResettableSkeletonProcessor -import dev.slimevr.skeleton.SkeletonInputProcessor -import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.util.timeSource -import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart -import solarxr_protocol.rpc.FilteringType -import solarxr_protocol.rpc.ResetType -import kotlin.time.ComparableTimeMark -import kotlin.time.Duration.Companion.milliseconds - -/** How far ahead the prediction reaches at amount 1, scaled down by the configured amount */ -val PREDICTION_LEAD = 12.milliseconds - -/** - * Tries to predict future rotations of bones. - */ -class RotationPredictionInputProcessor(val settings: Settings) : - SkeletonInputProcessor, - ResettableSkeletonProcessor { - private data class BoneDelta( - val lastRotation: Quaternion, - val rotationDelta: Quaternion, - val lastChange: ComparableTimeMark, - ) - - // Used to minimize latency even more for arm tracking where it is more noticeable in VR. - private val PART_MULTIPLIER = BodyPartMap( - mapOf( - BodyPart.LEFT_SHOULDER to 1.4f, - BodyPart.RIGHT_SHOULDER to 1.4f, - BodyPart.LEFT_UPPER_ARM to 1.4f, - BodyPart.RIGHT_UPPER_ARM to 1.4f, - BodyPart.LEFT_LOWER_ARM to 1.4f, - BodyPart.RIGHT_LOWER_ARM to 1.4f, - ), - ) - - private val deltas: BodyPartMap = bodyPartMap() - - override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - val config = settings.context.state.value.data.skeletonConfig.filtering - val filteringAmount = config.amount - if (config.type != FilteringType.PREDICTION || filteringAmount <= 0f) { - // Drop stale velocities so re-enabling doesn't diff against a long outdated pose - if (deltas.isNotEmpty()) deltas.clear() - return - } - val now = timeSource.markNow() - - for ((bodyPart, bone) in mutableInputSkeleton) { - if (!bone.isRotationActive) continue - - val prev = deltas[bodyPart] - if (prev == null) { - deltas[bodyPart] = BoneDelta(bone.rotation, Quaternion.IDENTITY, now) - continue - } - - val leadScale = (PREDICTION_LEAD / (now - prev.lastChange)).toFloat() - val rotationDelta = (bone.rotation / prev.lastRotation).pow(leadScale) - deltas[bodyPart] = BoneDelta(bone.rotation, rotationDelta, now) - - val bonePredictionAmount = filteringAmount * (PART_MULTIPLIER[bodyPart] ?: 1f) - val scaledDelta = rotationDelta.pow(bonePredictionAmount) - val predicted = scaledDelta * bone.rotation - if (predicted != bone.rotation) mutableInputSkeleton[bodyPart] = bone.copy(rotation = predicted) - } - } - - override fun reset(resetType: ResetType) { - deltas.clear() - } -} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt deleted file mode 100644 index 1a427b7b4..000000000 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/rotation-smoothing.kt +++ /dev/null @@ -1,59 +0,0 @@ -package dev.slimevr.skeleton.inputprocessors - -import dev.slimevr.config.Settings -import dev.slimevr.skeleton.BodyPartMap -import dev.slimevr.skeleton.InputSkeleton -import dev.slimevr.skeleton.ResettableSkeletonProcessor -import dev.slimevr.skeleton.SkeletonInputProcessor -import dev.slimevr.skeleton.bodyPartMap -import dev.slimevr.util.inFloatingSeconds -import dev.slimevr.util.timeSource -import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.rpc.FilteringType -import solarxr_protocol.rpc.ResetType - -private const val SMOOTHING_MULTIPLIER = 100f -private const val SMOOTH_MIN = 0.46f -private const val SMOOTH_MAX = 0.92f - -/** - * Running average of bone rotations to smooth them out. - */ -class RotationSmoothingInputProcessor(val settings: Settings) : - SkeletonInputProcessor, - ResettableSkeletonProcessor { - private val smoothed: BodyPartMap = bodyPartMap() - private var lastProcessTime = timeSource.markNow() - - override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { - val config = settings.context.state.value.data.skeletonConfig.filtering - val filteringAmount = config.amount - if (config.type != FilteringType.SMOOTHING || filteringAmount <= 0f) { - // Drop stale poses so re-enabling doesn't blend out of an outdated frame - if (smoothed.isNotEmpty()) smoothed.clear() - lastProcessTime = timeSource.markNow() - return - } - - // Normalize with frame time - val lastFrameTimeSeconds = lastProcessTime.elapsedNow().inFloatingSeconds - lastProcessTime = timeSource.markNow() - - val smoothingAmount = SMOOTH_MIN + filteringAmount * (SMOOTH_MAX - SMOOTH_MIN) - val alpha = ((1 - smoothingAmount) * lastFrameTimeSeconds * SMOOTHING_MULTIPLIER).coerceIn(0f, 1f) - - for ((bodyPart, bone) in mutableInputSkeleton) { - if (!bone.isRotationActive) continue - - val prev = smoothed[bodyPart] ?: bone.rotation - val rotation = prev.lerpQ(bone.rotation, alpha).unit() - smoothed[bodyPart] = rotation - if (rotation != bone.rotation) mutableInputSkeleton[bodyPart] = bone.copy(rotation = rotation) - } - } - - override fun reset(resetType: ResetType) { - lastProcessTime = timeSource.markNow() - smoothed.clear() - } -} 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 291f96806..a959d1a81 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -12,15 +12,13 @@ import dev.slimevr.skeleton.inputprocessors.BoneOffsetInputProcessor import dev.slimevr.skeleton.inputprocessors.ConstraintInputProcessor import dev.slimevr.skeleton.inputprocessors.DirectLinkInputProcessor import dev.slimevr.skeleton.inputprocessors.FallbackInputProcessor +import dev.slimevr.skeleton.inputprocessors.FilteringInputProcessor import dev.slimevr.skeleton.inputprocessors.FingersInputProcessor import dev.slimevr.skeleton.inputprocessors.HeadPositionFallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.NeckInputProcessor -import dev.slimevr.skeleton.inputprocessors.RotationPredictionInputProcessor -import dev.slimevr.skeleton.inputprocessors.RotationSmoothingInputProcessor import dev.slimevr.skeleton.inputprocessors.SpineInputProcessor import dev.slimevr.skeleton.inputprocessors.ToesInputProcessor import dev.slimevr.skeleton.inputprocessors.UpperLegsRollAlignInputProcessor -import dev.slimevr.skeleton.inputprocessors.UpsamplingInputProcessor import dev.slimevr.skeleton.targetprocessors.FloorClipTargetProcessor import dev.slimevr.skeleton.targetprocessors.PositionalTargetProcessor import dev.slimevr.skeleton.targetprocessors.SkatingCorrectionTargetProcessor @@ -277,15 +275,13 @@ class Skeleton( waiter = waiter, // Run before FK on the inputs inputProcessors = listOf( - UpsamplingInputProcessor(), - RotationPredictionInputProcessor(settings), - RotationSmoothingInputProcessor(settings), + FilteringInputProcessor(settings), BoneOffsetInputProcessor(), HeadPositionFallbackInputProcessor(), FallbackInputProcessor(), SpineInputProcessor(settings), - UpperLegsRollAlignInputProcessor(settings), NeckInputProcessor(), + UpperLegsRollAlignInputProcessor(settings), DirectLinkInputProcessor(), FingersInputProcessor(), ToesInputProcessor(), diff --git a/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-settings.kt b/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-settings.kt index 0bb5ff894..711fc23cf 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-settings.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/rpc/skeleton-settings.kt @@ -3,7 +3,6 @@ package dev.slimevr.solarxr.rpc import dev.slimevr.config.Settings import dev.slimevr.config.SettingsActions import dev.slimevr.config.SkeletonConfig -import dev.slimevr.config.SkeletonFilteringConfig import dev.slimevr.config.SkeletonRatiosConfig import dev.slimevr.config.SkeletonTogglesConfig import dev.slimevr.skeleton.SkeletonActions @@ -18,7 +17,6 @@ import solarxr_protocol.rpc.ChangeSkeletonSettingsRequest import solarxr_protocol.rpc.LegTweaksTmpChange import solarxr_protocol.rpc.LegTweaksTmpClear import solarxr_protocol.rpc.SetPauseTrackingRequest -import solarxr_protocol.rpc.SkeletonFiltering import solarxr_protocol.rpc.SkeletonRatios import solarxr_protocol.rpc.SkeletonSettingsRequest import solarxr_protocol.rpc.SkeletonSettingsResponse @@ -37,15 +35,13 @@ internal fun buildSkeletonSettingsResponse(config: SkeletonConfig) = SkeletonSet enforceConstraints = config.toggles.enforceConstraints, ), ratios = SkeletonRatios( + smoothingStrength = config.ratios.smoothingStrength, + predictionStrength = config.ratios.predictionStrength, spineCurvature = config.ratios.spineCurvature, imputeSpineFromUpperToLower = config.ratios.imputeSpineFromUpperToLower, interpolateUpperLegsTwistWithLowerLegs = config.ratios.interpolateUpperLegsTwistWithLowerLegs, skatingCorrectionStrength = config.ratios.skatingCorrectionStrength, ), - filtering = SkeletonFiltering( - type = config.filtering.type, - amount = config.filtering.amount, - ), ) class SkeletonSettingsBehaviour( @@ -84,18 +80,14 @@ class SkeletonSettingsBehaviour( } ?: skeletonConfig.toggles, ratios = req.ratios?.let { SkeletonRatiosConfig( + smoothingStrength = it.smoothingStrength ?: 0f, + predictionStrength = it.predictionStrength ?: 0f, spineCurvature = it.spineCurvature ?: 0f, imputeSpineFromUpperToLower = it.imputeSpineFromUpperToLower ?: 0f, interpolateUpperLegsTwistWithLowerLegs = it.interpolateUpperLegsTwistWithLowerLegs ?: 0f, skatingCorrectionStrength = it.skatingCorrectionStrength ?: 0f, ) } ?: skeletonConfig.ratios, - filtering = req.filtering?.let { - SkeletonFilteringConfig( - type = it.type, - amount = it.amount, - ) - } ?: skeletonConfig.filtering, ), ) }, diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt index d845b98f5..ef44fd848 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/Main.kt @@ -13,7 +13,6 @@ import dev.slimevr.config.AppConfig import dev.slimevr.context.debug.contextDebugEnabled import dev.slimevr.desktop.config.DesktopConfigStorage import dev.slimevr.desktop.hid.createDesktopHIDManager -import dev.slimevr.desktop.utils.executeShellCommand import dev.slimevr.desktop.ipc.createIpcServers import dev.slimevr.desktop.ipc.createSolarXRWebsocketServer import dev.slimevr.desktop.keybind.createDesktopKeybindManager @@ -25,6 +24,7 @@ import dev.slimevr.desktop.serial.createDesktopSerialServer import dev.slimevr.desktop.timing.createDesktopWaiter import dev.slimevr.desktop.trackingchecklist.SteamVRCheckBehaviour import dev.slimevr.desktop.udp.bindDesktopUdp +import dev.slimevr.desktop.utils.executeShellCommand import dev.slimevr.desktop.vrchat.createDesktopVRCConfigManager import dev.slimevr.desktop.vrchat.observeDesktopLocalIpAddress import dev.slimevr.desktop.vrchat.resolveDesktopLocalIpAddress @@ -89,11 +89,8 @@ fun main(args: Array) = runBlocking(appCoroutineExceptionHandler + for (arg in args) { when (arg) { "--steam", "-s" -> featureFlags.steam = true - "--no-udev", "-u" -> featureFlags.skipCheckUdev = true - "--skeleton-ticks", "-st" -> AppLogger.ShouldDebug.skeletonTicks = true - "--event-dispatcher", "-ed" -> AppLogger.ShouldDebug.eventDispatcher = true } } diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/utils/shell.kt b/server/desktop/src/main/java/dev/slimevr/desktop/utils/shell.kt index 289ff69de..6c83bee81 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/utils/shell.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/utils/shell.kt @@ -6,15 +6,15 @@ import kotlinx.coroutines.withContext import java.io.IOException suspend fun executeShellCommand(vararg command: String): Pair? = try { - withContext(Dispatchers.IO) { - val process = ProcessBuilder(*command) - .redirectErrorStream(true) - .start() + withContext(Dispatchers.IO) { + val process = ProcessBuilder(*command) + .redirectErrorStream(true) + .start() - val output = process.inputStream.bufferedReader().readText() - process.waitFor() - Pair(process.exitValue(), output) - } + val output = process.inputStream.bufferedReader().readText() + process.waitFor() + Pair(process.exitValue(), output) + } } catch (e: IOException) { AppLogger.install.warn("Error executing shell command: ${e.message}") null