Skip to content

Commit

Permalink
Update based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Jan 31, 2024
1 parent 953dcf7 commit 84f3a12
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 102 deletions.
54 changes: 0 additions & 54 deletions Editor/CesiumOriginShiftEditor.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Editor/CesiumOriginShiftEditor.cs.meta

This file was deleted.

26 changes: 5 additions & 21 deletions Runtime/CesiumOriginShift.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ namespace CesiumForUnity
[IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
public class CesiumOriginShift : MonoBehaviour
{
/// <summary>
/// When false, the origin will be shifted every frame.
/// When true, <see cref="activationDistance"/> will be used to specify the distance from the old
/// origin after which the origin will be shifted.
/// </summary>
public bool useActivationDistance
{
get => _useActivationDistance;
set => _useActivationDistance = value;
}

[SerializeField]
private bool _useActivationDistance = false;

/// <summary>
/// Specifies the minimum distance in meters from the old origin to the current origin before the
/// origin of the parent <see cref="CesiumGeoreference"/> will be shifted.
Expand All @@ -54,7 +40,7 @@ public double activationDistance
}

[SerializeField]
private double _activationDistance = 1.0;
private double _activationDistance = 0.0;

void LateUpdate()
{
Expand All @@ -69,15 +55,13 @@ void LateUpdate()
return;
}

this.UpdateFromEcef(georeference, anchor);
this.UpdateFromEcef(georeference, anchor.positionGlobeFixed);
}

private List<CesiumSubScene> _sublevelsScratch = new List<CesiumSubScene>();

private void UpdateFromEcef(CesiumGeoreference georeference, CesiumGlobeAnchor anchor)
private void UpdateFromEcef(CesiumGeoreference georeference, double3 ecef)
{
double3 ecef = anchor.positionGlobeFixed;

CesiumSubScene closestLevel = null;
double distanceSquaredToClosest = double.MaxValue;

Expand Down Expand Up @@ -128,9 +112,9 @@ private void UpdateFromEcef(CesiumGeoreference georeference, CesiumGlobeAnchor a

double distance = math.length(new double3(georeference.ecefX, georeference.ecefY, georeference.ecefZ) - ecef);

if (!this.useActivationDistance || distance >= this._activationDistance)
if (distance >= this._activationDistance)
{
// Update the origin continuously.
// Update the origin if we've surpassed the distance threshold.
georeference.SetOriginEarthCenteredEarthFixed(ecef.x, ecef.y, ecef.z);
// Make sure the physics system is informed that things have moved
Physics.SyncTransforms();
Expand Down
27 changes: 23 additions & 4 deletions Tests/Comparers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,45 @@ internal class Comparers
{
private class DoubleComparer : IEqualityComparer<double>
{
private readonly double _epsilon;
private readonly double _absoluteEpsilon;
private readonly double _relativeEpsilon;

public DoubleComparer(double epsilon)
public DoubleComparer(double absoluteEpsilon)
{
this._epsilon = epsilon;
this._absoluteEpsilon = absoluteEpsilon;
this._relativeEpsilon = 0.0;
}

public DoubleComparer(double absoluteEpsilon, double relativeEpsilon) : this(absoluteEpsilon)
{
this._relativeEpsilon = relativeEpsilon;
}

public bool Equals(double x, double y)
{
return Math.Abs(x - y) <= this._epsilon;
double diff = Math.Abs(x - y);
return diff <= this._absoluteEpsilon ||
diff <= RelativeToAbsoluteEpsilon(x, y, this._relativeEpsilon);
}

public int GetHashCode(double obj)
{
throw new NotImplementedException();
}

private double RelativeToAbsoluteEpsilon(double left, double right, double relativeEpsilon)
{
return relativeEpsilon * Math.Max(Math.Abs(left), Math.Abs(right));
}
}

public static IEqualityComparer<double> Double(double absoluteEpsilon)
{
return new DoubleComparer(absoluteEpsilon);
}

public static IEqualityComparer<double> Double(double absoluteEpsilon, double relativeEpsilon)
{
return new DoubleComparer(absoluteEpsilon, relativeEpsilon);
}
}
26 changes: 14 additions & 12 deletions Tests/TestCesiumOriginShift.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CesiumForUnity;
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.TestTools;
Expand Down Expand Up @@ -114,7 +115,6 @@ public IEnumerator UsesActivationDistanceProperty()
CesiumGlobeAnchor globeAnchor = goOriginShift.AddComponent<CesiumGlobeAnchor>();

CesiumOriginShift originShift = goOriginShift.AddComponent<CesiumOriginShift>();
originShift.useActivationDistance = true;
originShift.activationDistance = 5000;

yield return null;
Expand All @@ -123,19 +123,23 @@ public IEnumerator UsesActivationDistanceProperty()

yield return null;

Assert.AreEqual(baseEcef.x + 4999.0, globeAnchor.positionGlobeFixed.x);
Assert.AreEqual(baseEcef.x + 0, georeference.ecefX);
IEqualityComparer<double> epsilon6 = Comparers.Double(1e-6);

// The anchor is still within the distance threshold, so the georeference should remain unchanged.
Assert.That(baseEcef.x + 4999.0, Is.EqualTo(globeAnchor.positionGlobeFixed.x).Using(epsilon6));
Assert.That(baseEcef.x, Is.EqualTo(georeference.ecefX).Using(epsilon6));

globeAnchor.positionGlobeFixed = baseEcef + new double3(5010.0, 0, 0);
yield return null;

Assert.AreEqual(baseEcef.x + 5010.0, globeAnchor.positionGlobeFixed.x);
Assert.AreEqual(baseEcef.x + 5010.0, georeference.ecefX);
// The anchor has surpassed the distance threshold, so the georeference should shift to the anchor's ECEF coordinates.
Assert.That(baseEcef.x + 5010.0, Is.EqualTo(globeAnchor.positionGlobeFixed.x).Using(epsilon6));
Assert.That(globeAnchor.positionGlobeFixed.x, Is.EqualTo(georeference.ecefX).Using(epsilon6));
}

// Testing a bug where the character controller would cause a jump ahead when the activation distance is hit.
[UnityTest]
public IEnumerator ShiftingOriginWithCharacterController()
public IEnumerator ShiftsOriginWithCharacterController()
{
double3 baseEcef = new double3(-2694020, -4297355, 3854720);

Expand All @@ -150,7 +154,6 @@ public IEnumerator ShiftingOriginWithCharacterController()
CesiumGlobeAnchor globeAnchor = goOriginShift.AddComponent<CesiumGlobeAnchor>();

CesiumOriginShift originShift = goOriginShift.AddComponent<CesiumOriginShift>();
originShift.useActivationDistance = true;
originShift.activationDistance = 5000;

CharacterController controller = goOriginShift.AddComponent<CharacterController>();
Expand All @@ -161,7 +164,9 @@ public IEnumerator ShiftingOriginWithCharacterController()

yield return new WaitForEndOfFrame();

Assert.AreEqual((float)baseEcef.x, (float)globeAnchor.positionGlobeFixed.x);
IEqualityComparer<double> epsilon6 = Comparers.Double(1e-6, 1e-6);

Assert.That(baseEcef.x, Is.EqualTo(globeAnchor.positionGlobeFixed.x).Using(epsilon6));

// speed per second
double speed = 1000.0;
Expand All @@ -182,11 +187,8 @@ public IEnumerator ShiftingOriginWithCharacterController()
yield return new WaitForEndOfFrame();

globeAnchor.Sync();
Assert.AreEqual((float)(previousPositionEcef.x + unitsEcef), (float)globeAnchor.positionGlobeFixed.x);
Assert.That(previousPositionEcef.x + unitsEcef, Is.EqualTo(globeAnchor.positionGlobeFixed.x).Using(epsilon6));
Assert.Less(georeference.ecefX - globeAnchor.positionGlobeFixed.x, 5000.0);

Debug.Log($"georeference d: ({georeference.ecefX - baseEcef.x}, {georeference.ecefY - baseEcef.y}, {georeference.ecefZ - baseEcef.z})");
Debug.Log($"anchor d: {globeAnchor.positionGlobeFixed - baseEcef}");
}
}
}

0 comments on commit 84f3a12

Please sign in to comment.