From 34ef979d2a3b3f59cb7844b5d452843189b46af8 Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Sat, 5 Aug 2023 14:57:46 -0400 Subject: [PATCH] jitter testing, avoid microphone for webgl build, dashboard ci --- Runtime/Util/SyncRigidbody.cs | 66 ++++++++----------- Runtime/Util/SyncTransform.cs | 7 +- Runtime/VelVoice/VelVoice.cs | 7 ++ Samples/FullExample/Scripts/NetworkGUI.cs | 2 + .../Prefabs/PlayerPrefab.prefab | 28 ++++---- .../Scripts/MicrophoneSelection.cs | 4 ++ 6 files changed, 57 insertions(+), 57 deletions(-) diff --git a/Runtime/Util/SyncRigidbody.cs b/Runtime/Util/SyncRigidbody.cs index 5937ff4..083ea1b 100644 --- a/Runtime/Util/SyncRigidbody.cs +++ b/Runtime/Util/SyncRigidbody.cs @@ -1,20 +1,22 @@ using System.IO; using UnityEngine; +using UnityEngine.Serialization; namespace VelNet { /// /// A simple class that will sync the position and rotation of a network object with a rigidbody + /// This only uses the rigidbody for interpolation - it doesn't do any interpolatin itself /// [AddComponentMenu("VelNet/VelNet Sync Rigidbody")] [RequireComponent(typeof(Rigidbody))] public class SyncRigidbody : NetworkSerializedObjectStream { public bool useLocalTransform; - [Tooltip("0 to disable.")] - public float teleportDistance; - [Tooltip("0 to disable.")] - public float teleportAngle; + public float minPosDelta = .01f; + public float minAngleDelta = .1f; + public float minVelDelta = .01f; + public float minAngVelDelta = .1f; public bool syncKinematic = true; public bool syncGravity = true; @@ -23,6 +25,8 @@ namespace VelNet private Vector3 targetPosition; private Quaternion targetRotation; + private Vector3 targetVel; + private Vector3 targetAngVel; private float distanceAtReceiveTime; private float angleAtReceiveTime; private Rigidbody rb; @@ -58,11 +62,12 @@ namespace VelNet writer.Write(transform.rotation); } + if (syncVelocity) writer.Write(rb.velocity); + if (syncAngularVelocity) writer.Write(rb.angularVelocity); + // writer.Write((new bool[] {rb.isKinematic, rb.useGravity}).GetBitmasks()); if (syncKinematic) writer.Write(rb.isKinematic); if (syncGravity) writer.Write(rb.useGravity); - if (syncVelocity) writer.Write(rb.velocity); - if (syncAngularVelocity) writer.Write(rb.angularVelocity); } /// @@ -74,21 +79,23 @@ namespace VelNet targetPosition = reader.ReadVector3(); targetRotation = reader.ReadQuaternion(); + if (syncVelocity) targetVel = reader.ReadVector3(); + if (syncAngularVelocity) targetAngVel = reader.ReadVector3(); + if (syncKinematic) rb.isKinematic = reader.ReadBoolean(); if (syncGravity) rb.useGravity = reader.ReadBoolean(); - if (syncVelocity) rb.velocity = reader.ReadVector3(); - if (syncAngularVelocity) rb.angularVelocity = reader.ReadVector3(); // record the distance from the target for interpolation if (useLocalTransform) { distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.localPosition); angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.localRotation); - if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime) + if (minPosDelta < distanceAtReceiveTime) { transform.localPosition = targetPosition; } - if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) + + if (minAngleDelta < angleAtReceiveTime) { transform.localRotation = targetRotation; } @@ -97,46 +104,27 @@ namespace VelNet { distanceAtReceiveTime = Vector3.Distance(targetPosition, transform.position); angleAtReceiveTime = Quaternion.Angle(targetRotation, transform.rotation); - if (teleportDistance != 0 && teleportDistance < distanceAtReceiveTime) + if (minPosDelta < distanceAtReceiveTime) { transform.position = targetPosition; } - if (teleportAngle != 0 && teleportAngle < angleAtReceiveTime) + + if (minAngleDelta < angleAtReceiveTime) { transform.rotation = targetRotation; } } - } - private void Update() - { - if (IsMine) return; - - if (useLocalTransform) + float velDelta = Vector3.Distance(targetVel, rb.velocity); + float angVelDelta = Vector3.Distance(targetAngVel, rb.angularVelocity); + if (velDelta > minVelDelta) { - transform.localPosition = Vector3.MoveTowards( - transform.localPosition, - targetPosition, - Time.deltaTime * distanceAtReceiveTime * serializationRateHz - ); - transform.localRotation = Quaternion.RotateTowards( - transform.localRotation, - targetRotation, - Time.deltaTime * angleAtReceiveTime * serializationRateHz - ); + rb.velocity = targetVel; } - else + + if (angVelDelta > minAngVelDelta) { - transform.position = Vector3.MoveTowards( - transform.position, - targetPosition, - Time.deltaTime * distanceAtReceiveTime * serializationRateHz - ); - transform.rotation = Quaternion.RotateTowards( - transform.rotation, - targetRotation, - Time.deltaTime * angleAtReceiveTime * serializationRateHz - ); + rb.angularVelocity = targetAngVel; } } } diff --git a/Runtime/Util/SyncTransform.cs b/Runtime/Util/SyncTransform.cs index 61648d0..29be722 100644 --- a/Runtime/Util/SyncTransform.cs +++ b/Runtime/Util/SyncTransform.cs @@ -9,13 +9,11 @@ namespace VelNet [AddComponentMenu("VelNet/VelNet Sync Transform")] public class SyncTransform : NetworkSerializedObjectStream { - [Space] - public bool position = true; + [Space] public bool position = true; public bool rotation = true; [Tooltip("Scale is always local")] public bool scale; - [Space] - public bool useLocalTransform; + [Space] public bool useLocalTransform; [Tooltip("0 to disable.")] public float teleportDistance; [Tooltip("0 to disable.")] public float teleportAngle; @@ -57,6 +55,7 @@ namespace VelNet if (position) writer.Write(transform.position); if (rotation) writer.Write(transform.rotation); } + if (scale) writer.Write(transform.localScale); } diff --git a/Runtime/VelVoice/VelVoice.cs b/Runtime/VelVoice/VelVoice.cs index 5a3fb9a..bdd90b4 100644 --- a/Runtime/VelVoice/VelVoice.cs +++ b/Runtime/VelVoice/VelVoice.cs @@ -101,7 +101,9 @@ namespace VelNet.Voice /// public void StartMicrophone() { +#if !UNITY_WEBGL && !UNITY_EDITOR StartMicrophone(Microphone.devices.FirstOrDefault()); +#endif } /// @@ -110,6 +112,7 @@ namespace VelNet.Voice /// The device name of the microphone to record with public void StartMicrophone(string micDeviceName) { +#if !UNITY_WEBGL && !UNITY_EDITOR Debug.Log("Starting with microphone: " + micDeviceName); if (micDeviceName == null) return; device = micDeviceName; @@ -121,6 +124,7 @@ namespace VelNet.Voice Debug.Log("Frequency:" + clip.frequency); tempData = new float[clip.samples * clip.channels]; Debug.Log("channels: " + clip.channels); +#endif } private void OnApplicationQuit() @@ -149,8 +153,10 @@ namespace VelNet.Voice // Update is called once per frame private void Update() { +#if !UNITY_WEBGL && !UNITY_EDITOR if (clip != null) { + int micPosition = Microphone.GetPosition(device); if (micPosition == lastPosition) { @@ -238,6 +244,7 @@ namespace VelNet.Voice sendQueue.Clear(); } +#endif } public float[] DecodeOpusData(byte[] data, int count) diff --git a/Samples/FullExample/Scripts/NetworkGUI.cs b/Samples/FullExample/Scripts/NetworkGUI.cs index 750e539..5c7a21c 100644 --- a/Samples/FullExample/Scripts/NetworkGUI.cs +++ b/Samples/FullExample/Scripts/NetworkGUI.cs @@ -19,8 +19,10 @@ namespace VelNet // Start is called before the first frame update private void Start() { +#if !UNITY_WEBGL && !UNITY_EDITOR microphones.AddOptions(new List(Microphone.devices)); HandleMicrophoneSelection(); +#endif } diff --git a/Samples/VelVoiceExample/Prefabs/PlayerPrefab.prefab b/Samples/VelVoiceExample/Prefabs/PlayerPrefab.prefab index 30b082b..607d6eb 100644 --- a/Samples/VelVoiceExample/Prefabs/PlayerPrefab.prefab +++ b/Samples/VelVoiceExample/Prefabs/PlayerPrefab.prefab @@ -208,9 +208,9 @@ GameObject: - component: {fileID: 5845716565458182149} - component: {fileID: 9102273340480352682} - component: {fileID: 7564913803199044469} - - component: {fileID: 3931189231264197893} - component: {fileID: 1833529818468634519} - component: {fileID: 1058783918878763107} + - component: {fileID: 3933010506106872429} m_Layer: 0 m_Name: PlayerPrefab m_TagString: TestSphere @@ -340,19 +340,6 @@ MonoBehaviour: useLocalTransform: 0 teleportDistance: 0 teleportAngle: 0 ---- !u!114 &3931189231264197893 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 6139051692386484099} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 544bfd0908e09d141adb9828d99baa08, type: 3} - m_Name: - m_EditorClassIdentifier: - networkObject: {fileID: 9102273340480352682} --- !u!114 &1833529818468634519 MonoBehaviour: m_ObjectHideFlags: 0 @@ -466,6 +453,19 @@ AudioSource: m_PreInfinity: 2 m_PostInfinity: 2 m_RotationOrder: 4 +--- !u!114 &3933010506106872429 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6139051692386484099} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1006f76dad9c183459e79084f8c67539, type: 3} + m_Name: + m_EditorClassIdentifier: + networkObject: {fileID: 9102273340480352682} --- !u!1 &7360746642267561319 GameObject: m_ObjectHideFlags: 0 diff --git a/Samples/VelVoiceExample/Scripts/MicrophoneSelection.cs b/Samples/VelVoiceExample/Scripts/MicrophoneSelection.cs index 9842dc0..7c06080 100644 --- a/Samples/VelVoiceExample/Scripts/MicrophoneSelection.cs +++ b/Samples/VelVoiceExample/Scripts/MicrophoneSelection.cs @@ -12,12 +12,16 @@ namespace VelNet private void Start() { +#if !UNITY_WEBGL && !UNITY_EDITOR microphones.AddOptions(Microphone.devices.ToList()); +#endif } public void HandleMicrophoneSelection() { +#if !UNITY_WEBGL && !UNITY_EDITOR velVoice.StartMicrophone(microphones.options[microphones.value].text); +#endif } } } \ No newline at end of file