Skip to content

Commit da3339e

Browse files
committed
fixing up the bugs from last test
1 parent 3c84dd3 commit da3339e

File tree

14 files changed

+3991
-275
lines changed

14 files changed

+3991
-275
lines changed

Basis/Packages/com.basis.eventdriver/BasisEventDriver.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public void Update()
131131
catch (Exception ex) { Debug.LogError($"MainThread action failed: {ex}"); }
132132
}
133133

134+
BasisNetworkManagement.UpdatePlayerArray();
134135
BasisNetworkManagement.SimulateNetworkCompute(unscaledDeltaTime);
135136
BasisObjectSyncDriver.ScheduleRemoteLerp(DeltaTime);
136137
#if UNITY_SERVER
@@ -159,6 +160,8 @@ public void FixedUpdate()
159160
/// <summary>
160161
/// LateUpdate step for device management loop, eye simulation, local player late sim,
161162
/// microphone updates (client), network apply, and JigglePhysics scheduling/pose/render.
163+
/// any jobs that require jobified transforms cannot be mulithreaded while another task is interacting with transforms
164+
/// welcome to unity!
162165
/// </summary>
163166
public void LateUpdate()
164167
{
@@ -177,8 +180,8 @@ public void LateUpdate()
177180
JigglePhysics.SchedulePose(TimeAsDouble);//requires free access to all transform of a player.
178181

179182
// Device management tick
180-
BasisDeviceManagement.OnDeviceManagementLoop?.Invoke(); //can do
181-
BasisRemoteAudioDriver.Simulate(DeltaTime); //can do
183+
BasisDeviceManagement.OnDeviceManagementLoop?.Invoke();
184+
BasisRemoteAudioDriver.Simulate(DeltaTime);
182185
#if UNITY_SERVER
183186
#else
184187
BasisLocalMicrophoneDriver.MicrophoneUpdate();
@@ -204,7 +207,7 @@ public void LateUpdate()
204207
if (BasisLocalPlayer.PlayerReady)
205208
{
206209
// Eye driver (local)
207-
BasisLocalPlayer.Instance.LocalEyeDriver.Simulate(DeltaTime); // cant do
210+
BasisLocalPlayer.Instance.LocalEyeDriver.Simulate(DeltaTime);
208211
}
209212

210213
if (SMModuleDebugOptions.UseGizmos)

Basis/Packages/com.basis.framework/Drivers/Remote/BasisRemoteBoneDriver.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Basis.Scripts.Common;
22
using Basis.Scripts.Drivers;
3-
using System;
43
using System.Collections.Generic;
54
using Unity.Burst;
65
using Unity.Collections;
@@ -250,15 +249,13 @@ struct GatherHipsJob : IJobParallelForTransform
250249
[WriteOnly] public NativeArray<float3> hipsPos;
251250
/// <summary>Output hips rotations.</summary>
252251
[WriteOnly] public NativeArray<quaternion> hipsRot;
253-
254252
/// <summary>Executes per-hip sampling.</summary>
255253
public void Execute(int index, TransformAccess tx)
256254
{
257255
hipsPos[index] = tx.position;
258256
hipsRot[index] = tx.rotation;
259257
}
260258
}
261-
262259
/// <summary>
263260
/// Applies the mouth transform directly from the computed <see cref="RemoteFrameOutput"/>.
264261
/// </summary>
@@ -268,14 +265,12 @@ struct ApplyMouthJob : IJobParallelForTransform
268265
/// <summary>Read-only pose data to apply.</summary>
269266
[ReadOnly]
270267
public NativeArray<RemoteFrameOutput> MouthRotation;
271-
272268
/// <summary>Applies position and rotation to the bound mouth transform.</summary>
273269
public void Execute(int index, TransformAccess tx)
274270
{
275271
tx.SetPositionAndRotation(MouthRotation[index].pos_Mouth, MouthRotation[index].rot_Mouth);
276272
}
277273
}
278-
279274
/// <summary>
280275
/// Positions the floating nameplate relative to the avatar and rotates it to face the camera (yaw only).
281276
/// Uses derived TPose vertical delta to place the plate above the head.
@@ -285,10 +280,8 @@ public struct MappedNameplateApplyJob : IJobParallelForTransform
285280
{
286281
/// <summary>Camera world position used to bill-board the plate (yaw-only).</summary>
287282
public float3 CameraPosition;
288-
289283
/// <summary>Input pose data (per-avatar) for nameplate placement.</summary>
290284
[ReadOnly] public NativeArray<RemoteFrameOutput> NamePlateIn;
291-
292285
/// <summary>Computes position above hips and rotates toward camera.</summary>
293286
public void Execute(int jobIndex, TransformAccess tx)
294287
{
@@ -331,11 +324,9 @@ struct AgrigateTranslationalData : IJobParallelFor
331324
[ReadOnly] public NativeArray<quaternion> tposeHeadRot;
332325
/// <summary>TPose hips quaternions.</summary>
333326
[ReadOnly] public NativeArray<quaternion> tposeHipsRot;
334-
335327
/// <summary>Combined output to be consumed by <see cref="BasisRemoteBoneJob"/>.</summary>
336328
[WriteOnly]
337329
public NativeArray<GeneratedTranslationalData> InOut;
338-
339330
/// <summary>Aggregates inputs into a single SoA element.</summary>
340331
public void Execute(int i)
341332
{
@@ -359,7 +350,6 @@ public void Execute(int i)
359350
/// </summary>
360351
public static class RemoteBoneJobSystem
361352
{
362-
// Persistent SoA
363353
/// <summary>Authoring TPose/offsets per avatar.</summary>
364354
static NativeList<TposeAndOffsetDataJob> sAuthoring;
365355
/// <summary>Per-frame inputs per avatar.</summary>
@@ -368,37 +358,28 @@ public static class RemoteBoneJobSystem
368358
static NativeList<RemoteScaleCache> sScale;
369359
/// <summary>Per-frame pose outputs per avatar.</summary>
370360
static NativeList<RemoteFrameOutput> sOut;
371-
372-
// Cached TPose quats (job friendly)
373361
/// <summary>TPose head quaternions per avatar.</summary>
374362
static NativeList<quaternion> sTPoseHeadRot;
375363
/// <summary>TPose hips quaternions per avatar.</summary>
376364
static NativeList<quaternion> sTPoseHipsRot;
377-
378-
// Transform access arrays (roots / heads / hips)
379365
/// <summary>Root transforms per avatar.</summary>
380366
static TransformAccessArray sRoots;
381367
/// <summary>Head transforms per avatar.</summary>
382368
static TransformAccessArray sHeads;
383369
/// <summary>Hips transforms per avatar.</summary>
384370
static TransformAccessArray sHips;
385-
386371
/// <summary>Nameplate transforms per avatar.</summary>
387372
static TransformAccessArray sNamePlate;
388373
/// <summary>Avatar scale proxy transforms per avatar.</summary>
389374
static TransformAccessArray sAvatarScale;
390375
/// <summary>Mouth transforms per avatar.</summary>
391376
static TransformAccessArray sMouth;
392-
393-
// Temp per-frame buffers (reused)
394377
/// <summary>Temp root positions.</summary>
395378
static NativeArray<float3> sTmpRootPos, sTmpHeadPos, sTmpHipsPos;
396379
/// <summary>Temp root scales.</summary>
397380
static NativeArray<float3> sTmpRootScale;
398381
/// <summary>Temp head rotations.</summary>
399382
static NativeArray<quaternion> sTmpHeadRot, sTmpHipsRot;
400-
401-
// Bookkeeping
402383
/// <summary>Map from external key → internal SoA index.</summary>
403384
static readonly Dictionary<int, int> sKeyToIndex = new Dictionary<int, int>();
404385
/// <summary>Pending job handle chain.</summary>
@@ -413,7 +394,10 @@ public static class RemoteBoneJobSystem
413394
/// <param name="initialCapacity">Optional starting capacity hint.</param>
414395
public static void Initialize(int initialCapacity = 0)
415396
{
416-
if (sInitialized) return;
397+
if (sInitialized)
398+
{
399+
return;
400+
}
417401

418402
sAuthoring = new NativeList<TposeAndOffsetDataJob>(initialCapacity, Allocator.Persistent);
419403
sIn = new NativeList<GeneratedTranslationalData>(initialCapacity, Allocator.Persistent);
@@ -492,7 +476,11 @@ public static int AddRemotePlayer(int key, Transform remotePlayerRoot, Transform
492476
BasisCalibratedCoords tposeHead, BasisCalibratedCoords tposeHips, float3 authoredCenterEyeWorld,
493477
float3 authoredMouthWorld, Transform NamePlate, Transform AvatarScale, Transform MouthTransform)
494478
{
495-
if (!sInitialized) Initialize();
479+
if (!sInitialized)
480+
{
481+
Initialize();
482+
}
483+
496484
CompletePending();
497485

498486
float3 rootWorld = remotePlayerRoot.position;
@@ -562,10 +550,17 @@ public static int AddRemotePlayer(int key, Transform remotePlayerRoot, Transform
562550
/// <returns><c>true</c> if found and removed; otherwise <c>false</c>.</returns>
563551
public static bool RemoveRemotePlayer(int key)
564552
{
565-
if (!sInitialized) return false;
553+
if (!sInitialized)
554+
{
555+
return false;
556+
}
557+
566558
CompletePending();
567559

568-
if (!sKeyToIndex.TryGetValue(key, out int idx)) return false;
560+
if (!sKeyToIndex.TryGetValue(key, out int idx))
561+
{
562+
return false;
563+
}
569564

570565
int last = sAuthoring.Length - 1;
571566
if (idx != last)
@@ -766,7 +761,10 @@ public static JobHandle Schedule(int batchSize = 64)
766761
public static void Complete(JobHandle handle)
767762
{
768763
handle.Complete();
769-
if (!sInitialized) return;
764+
if (!sInitialized)
765+
{
766+
return;
767+
}
770768

771769
CompletePending();
772770
}

Basis/Packages/com.basis.framework/Networking/BasisNetworkLifeCycle.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public static void Initalize(BasisNetworkManagement Management)
4040

4141
Management.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
4242
BasisNetworkManagement.OnEnableInstanceCreate?.Invoke();
43-
44-
BasisNetworkPlayers.PublishReceiversSnapshot();
4543
BasisNetworkManagement.NetworkRunning = true;
4644
}
4745
public static bool GoingThroughReboot = false;

Basis/Packages/com.basis.framework/Networking/BasisNetworkManagement.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Basis.Scripts.Profiler;
66
using System;
77
using System.Collections.Concurrent;
8+
using System.Linq;
89
using System.Threading;
910
using Unity.Jobs;
1011
using UnityEngine;
@@ -151,8 +152,8 @@ public static bool IsMainThread()
151152
public static float Beta = 2;
152153
// [Header("DerivativeCutoff This controls how noisy the speed estimate itself is.Before the filter adapts, it estimates velocity:")]
153154
public static float DerivativeCutoff = 2;
154-
public static BasisNetworkReceiver[] snapshot;
155-
public static int Length;
155+
public static BasisNetworkReceiver[] ReceiverArray;
156+
public static int ReceiverArrayLength;
156157
/// <summary>
157158
/// Simulates network computation step (state updates, bone drivers, profiler update).
158159
/// </summary>
@@ -163,13 +164,16 @@ public static void SimulateNetworkCompute(float UnscaledDeltaTime)
163164
{
164165
return;
165166
}
166-
167-
snapshot = BasisNetworkPlayers.ReceiversSnapshot;
167+
if (ReceiverArrayLength > BasisRemoteNetworkDriver.FixedCapacity)
168+
{
169+
BasisDebug.LogError("To Many Players out of array Data!");
170+
return;
171+
}
168172
BoneJobSystem = RemoteBoneJobSystem.Schedule(); // will always be a frame behind
169-
Length = snapshot.Length;
170-
for (int Index = 0; Index < Length; Index++)
173+
float step = Mathf.Max(UnscaledDeltaTime, 0f);
174+
for (int Index = 0; Index < ReceiverArrayLength; Index++)
171175
{
172-
snapshot[Index].Compute(UnscaledDeltaTime);
176+
ReceiverArray[Index].Compute(step);
173177
}
174178
BasisRemoteNetworkDriver.Compute();
175179
BasisNetworkProfiler.Update();
@@ -186,6 +190,11 @@ public static void SimulateNetworkCompute(float UnscaledDeltaTime)
186190
}
187191
}
188192
}
193+
public static void UpdatePlayerArray()
194+
{
195+
ReceiverArray = BasisNetworkPlayers.RemotePlayers.Count == 0 ? Array.Empty<BasisNetworkReceiver>() : BasisNetworkPlayers.RemotePlayers.Values.ToArray();
196+
ReceiverArrayLength = ReceiverArray.Length;
197+
}
189198
/// <summary>
190199
/// Applies networked state changes to receivers.
191200
/// </summary>
@@ -195,11 +204,14 @@ public static void SimulateNetworkApply()
195204
{
196205
return;
197206
}
198-
207+
if (ReceiverArrayLength > BasisRemoteNetworkDriver.FixedCapacity)
208+
{
209+
return;
210+
}
199211
BasisRemoteNetworkDriver.Apply();
200-
for (int Index = 0; Index < Length; Index++)
212+
for (int Index = 0; Index < ReceiverArrayLength; Index++)
201213
{
202-
snapshot[Index].Apply();
214+
ReceiverArray[Index].Apply();
203215
}
204216
}
205217

Basis/Packages/com.basis.framework/Networking/BasisNetworkPlayers.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ public static class BasisNetworkPlayers
2020
public static readonly List<ushort> JoiningPlayers = new(); // used as a set
2121
public static readonly ConcurrentDictionary<string, ushort> OwnershipPairing = new();
2222

23-
// Receiver snapshot for multi-threaded compute/apply phases.
24-
private static volatile BasisNetworkReceiver[] _receiversSnapshot = Array.Empty<BasisNetworkReceiver>();
25-
public static BasisNetworkReceiver[] ReceiversSnapshot => _receiversSnapshot;
26-
public static int ReceiverCount => _receiversSnapshot.Length;
27-
2823
// --- Lifecycle helpers ---------------------------------------------
2924
public static void ClearAllRegistries()
3025
{
@@ -36,11 +31,6 @@ public static void ClearAllRegistries()
3631
RemotePlayers.Clear();
3732
JoiningPlayers.Clear();
3833
OwnershipPairing.Clear();
39-
PublishReceiversSnapshot();
40-
}
41-
public static void PublishReceiversSnapshot()
42-
{
43-
_receiversSnapshot = RemotePlayers.Count == 0 ? Array.Empty<BasisNetworkReceiver>() : RemotePlayers.Values.ToArray();
4434
}
4535

4636
// --- Registry APIs --------------------------------------------------
@@ -84,8 +74,6 @@ public static bool AddPlayer(BasisNetworkPlayer netPlayer)
8474
BasisDebug.LogError($"Failed to add remote player {netPlayer.playerId} to RemotePlayers. Rolled back from Players.");
8575
return false;
8676
}
87-
88-
PublishReceiversSnapshot();
8977
}
9078

9179
return true;
@@ -102,7 +90,6 @@ public static bool RemovePlayer(ushort netId, out BasisNetworkPlayer player)
10290

10391
Players.TryRemove(netId, out player);
10492
RemotePlayers.TryRemove(netId, out _);
105-
PublishReceiversSnapshot();
10693
return true;
10794
}
10895

Basis/Packages/com.basis.framework/Networking/BasisRemoteFaceManagement.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,14 @@ public static class BasisRemoteFaceManagement
4949
public const float LookSpeed = 15;
5050

5151
public static JobHandle handle;
52-
public static BasisNetworkReceiver[] snapshot;
53-
public static int count;
5452
public static void Simulate(double t,float dt)
5553
{
56-
snapshot = BasisNetworkPlayers.ReceiversSnapshot;
57-
count = BasisNetworkPlayers.ReceiverCount;
58-
if (count <= 0)
54+
if (BasisNetworkManagement.ReceiverArrayLength <= 0)
5955
{
6056
return;
6157
}
6258

63-
EnsureArrays(count, t, snapshot);
59+
EnsureArrays(BasisNetworkManagement.ReceiverArrayLength, t, BasisNetworkManagement.ReceiverArray);
6460

6561
// No per-frame main-thread copy into a NativeArray.
6662
// The job will update eyeOut in-place (as state), and Apply() will write eyeOut back to receivers.
@@ -90,18 +86,18 @@ public static void Simulate(double t,float dt)
9086
blinkOut = blinkOut,
9187
};
9288

93-
handle = job.Schedule(count, BatchSize);
89+
handle = job.Schedule(BasisNetworkManagement.ReceiverArrayLength, BatchSize);
9490
}
9591

9692
public static void Apply()
9793
{
98-
if (count <= 0) return;
94+
if (BasisNetworkManagement.ReceiverArrayLength <= 0) return;
9995

10096
handle.Complete();
10197

102-
for (int Index = 0; Index < count; Index++)
98+
for (int Index = 0; Index < BasisNetworkManagement.ReceiverArrayLength; Index++)
10399
{
104-
var receiver = snapshot[Index];
100+
var receiver = BasisNetworkManagement.ReceiverArray[Index];
105101
var remote = receiver.RemotePlayer;
106102
var Face = remote.RemoteFaceDriver;
107103

0 commit comments

Comments
 (0)