From 60f021e5431849b21d061cd7103427f8a90421ca Mon Sep 17 00:00:00 2001 From: vis2k Date: Fri, 10 Mar 2023 14:25:46 +0800 Subject: [PATCH] Snapshot Interpolation Demo: simulate 1 fps --- .../Snapshot Interpolation/ClientCube.cs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Examples/Snapshot Interpolation/ClientCube.cs b/Assets/Mirror/Examples/Snapshot Interpolation/ClientCube.cs index f998d105b..bd3641b6b 100644 --- a/Assets/Mirror/Examples/Snapshot Interpolation/ClientCube.cs +++ b/Assets/Mirror/Examples/Snapshot Interpolation/ClientCube.cs @@ -94,6 +94,10 @@ public class ClientCube : MonoBehaviour public Color slowdownColor = Color.red; // red traffic light = go slow Color defaultColor; + [Header("Simulation")] + bool lowFpsMode; + double accumulatedDeltaTime; + void Awake() { defaultColor = render.sharedMaterial.color; @@ -145,6 +149,15 @@ public void OnMessage(Snapshot3D snap) void Update() { + // accumulated delta allows us to simulate correct low fps + deltaTime + // if necessary in client low fps mode. + accumulatedDeltaTime += Time.unscaledDeltaTime; + + // simulate low fps mode. only update once per second. + // to simulate webgl background tabs, etc. + // after a while, disable low fps mode and see how it behaves. + if (lowFpsMode && accumulatedDeltaTime < 1) return; + // only while we have snapshots. // timeline starts when the first snapshot arrives. if (snapshots.Count > 0) @@ -155,7 +168,9 @@ void Update() // step SnapshotInterpolation.Step( snapshots, - Time.unscaledDeltaTime, + // accumulate delta is Time.unscaledDeltaTime normally. + // and sum of past 10 delta's in low fps mode. + accumulatedDeltaTime, ref localTimeline, localTimescale, out Snapshot3D fromSnapshot, @@ -175,6 +190,9 @@ void Update() } } + // reset simulation helpers + accumulatedDeltaTime = 0; + // color material while catching up / slowing down if (localTimescale < 1) render.material.color = slowdownColor; @@ -193,6 +211,19 @@ void OnGUI() Vector2 screen = Camera.main.WorldToScreenPoint(transform.position); string str = $"{snapshots.Count}"; GUI.Label(new Rect(screen.x - width / 2, screen.y - height / 2, width, height), str); + + // client simulation buttons on the bottom of the screen + float areaHeight = 100; + GUILayout.BeginArea(new Rect(0, Screen.height - areaHeight, Screen.width, areaHeight)); + GUILayout.BeginHorizontal(); + GUILayout.Label("Client Simulation:"); + if (GUILayout.Button((lowFpsMode ? "Disable" : "Enable") + " 1 FPS")) + { + lowFpsMode = !lowFpsMode; + } + GUILayout.FlexibleSpace(); + GUILayout.EndHorizontal(); + GUILayout.EndArea(); } void OnValidate()