Skip to content

Commit

Permalink
Change Unloading bucket to unloading sqr distance based on the tolera…
Browse files Browse the repository at this point in the history
…nce added to the maximum loading distance (#236)
  • Loading branch information
mikhail-dcl authored Dec 28, 2023
1 parent 3323992 commit 82f5825
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public interface IRealmPartitionSettings
int MaxLoadingDistanceInParcels { get; }

/// <summary>
/// Bucket from which the scenes start to unload, the distance corresponding to this bucket should be bigger than <see cref="MaxLoadingDistanceInParcels" />
/// so distant parcels will start unloading gradually
/// Tolerance that is added to <see cref="MaxLoadingDistanceInParcels" /> to determine the distance at which scenes start unloading.
/// It should be slightly bigger than 0 to avoid scenes unloading and loading back immediately when the player moves back and forth
/// when the <see cref="MaxLoadingDistanceInParcels" /> is reached
/// </summary>
int UnloadBucket { get; }
int UnloadingDistanceToleranceInParcels { get; }

/// <summary>
/// The number of closest scenes that can be requested at a time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class RealmPartitionSettingsAsset : ScriptableObject, IRealmPartitionSett
[field: SerializeField]
public int MaxLoadingDistanceInParcels { get; private set; }

[field: SerializeField]
public int UnloadBucket { get; private set; }
[field: SerializeField] [field: Min(1)]
public int UnloadingDistanceToleranceInParcels { get; private set; } = 1;

[field: SerializeField]
public int ScenesRequestBatchSize { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using SceneRunner.Scene;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Pool;
using Utility;

Expand Down Expand Up @@ -65,7 +66,12 @@ protected override void Update(float t)
ProcessesFixedRealmQuery(World, maxLoadingSqrDistance);
}

StartUnloadingQuery(World);
float unloadingDistance = (Mathf.Max(1, realmPartitionSettings.UnloadingDistanceToleranceInParcels) + realmPartitionSettings.MaxLoadingDistanceInParcels)
* ParcelMathHelper.PARCEL_SIZE;

float unloadingSqrDistance = unloadingDistance * unloadingDistance;

StartUnloadingQuery(World, unloadingSqrDistance);
}

[Query]
Expand Down Expand Up @@ -145,17 +151,17 @@ private void StartScenesLoading(ref RealmComponent realmComponent, float maxLoad
[Query]
[All(typeof(SceneDefinitionComponent))]
[None(typeof(DeleteEntityIntention))]
private void StartUnloading(in Entity entity, ref PartitionComponent partitionComponent)
private void StartUnloading([Data] float unloadingSqrDistance, in Entity entity, ref PartitionComponent partitionComponent)
{
if (partitionComponent.Bucket < realmPartitionSettings.UnloadBucket) return;
if (partitionComponent.RawSqrDistance < unloadingSqrDistance) return;
World.Add(entity, DeleteEntityIntention.DeferredDeletion);
}

private struct OrderedData
{
public Entity Entity;
public PartitionComponent PartitionComponent;
public SceneDefinitionComponent DefinitionComponent;
public Entity Entity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public void LimitScenesLoading()
[Test]
public void StartUnloading()
{
realmPartitionSettings.UnloadBucket.Returns(3);
realmPartitionSettings.UnloadingDistanceToleranceInParcels.Returns(1);
realmPartitionSettings.MaxLoadingDistanceInParcels.Returns(1);

for (byte i = 2; i <= 4; i++)
{
Expand All @@ -83,7 +84,7 @@ public void StartUnloading()
},
},
},
new IpfsTypes.IpfsPath()), new PartitionComponent { Bucket = i });
new IpfsTypes.IpfsPath()), new PartitionComponent { Bucket = i, RawSqrDistance = (ParcelMathHelper.PARCEL_SIZE * i * ParcelMathHelper.PARCEL_SIZE * i) - 1f });
}

system.Update(0f);
Expand Down

0 comments on commit 82f5825

Please sign in to comment.