Skip to content

Commit f97f2f2

Browse files
authored
Merge pull request #480 from CesiumGS/globe-anchor-disabled-pos
Fix changing CesiumGlobeAnchor position while disabled
2 parents 0e49fa3 + 5e78d0f commit f97f2f2

File tree

6 files changed

+70
-10
lines changed

6 files changed

+70
-10
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## ? - ?
4+
5+
##### Fixes :wrench:
6+
7+
- Fixed a bug that caused a `NullReferenceException` when attempting to get or set the `longitudeLatitudeHeight` property on a disabled `CesiumGlobeAnchor`.
8+
39
### v1.11.0 - 2024-07-01
410

511
##### Additions :tada:

Runtime/CesiumCameraController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ void FixedUpdate()
408408

409409
private bool RaycastTowardsEarthCenter(out float hitDistance)
410410
{
411+
if (this._georeference == null)
412+
{
413+
hitDistance = 0.0f;
414+
return false;
415+
}
416+
411417
double3 center =
412418
this._georeference.TransformEarthCenteredEarthFixedPositionToUnity(new double3(0.0));
413419

Runtime/CesiumGlobeAnchor.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,28 @@ public double4x4 localToGlobeFixedMatrix
185185
/// </remarks>
186186
public double3 longitudeLatitudeHeight
187187
{
188-
get => this._georeference.ellipsoid.CenteredFixedToLongitudeLatitudeHeight(this.positionGlobeFixed);
188+
get
189+
{
190+
this.UpdateGeoreferenceIfNecessary();
191+
192+
if (this._georeference == null || this._georeference.ellipsoid == null)
193+
{
194+
// Cannot get property if there is no georeference, or the georeference has no ellipsoid.
195+
return new double3(0.0, 0.0, 0.0);
196+
}
197+
198+
return this._georeference.ellipsoid.CenteredFixedToLongitudeLatitudeHeight(this.positionGlobeFixed);
199+
}
189200
set
190201
{
202+
this.UpdateGeoreferenceIfNecessary();
203+
204+
if (this._georeference == null || this._georeference.ellipsoid == null)
205+
{
206+
// Cannot set property if there is no georeference, or the georeference has no ellipsoid.
207+
return;
208+
}
209+
191210
this.positionGlobeFixed = this._georeference.ellipsoid.LongitudeLatitudeHeightToCenteredFixed(value);
192211
}
193212
}
@@ -449,10 +468,10 @@ public void Sync()
449468
{
450469
// If the ellipsoid changed since last sync, we need to update from transform since our ECEF mapping
451470
// is going to be invalid.
452-
bool isEllipsoidChanged = _lastEllipsoidRadii.HasValue ?
453-
(_lastEllipsoidRadii.Value.x != _georeference.ellipsoid.radii.x ||
454-
_lastEllipsoidRadii.Value.y != _georeference.ellipsoid.radii.y ||
455-
_lastEllipsoidRadii.Value.z != _georeference.ellipsoid.radii.z) : true;
471+
bool isEllipsoidChanged = this._lastEllipsoidRadii.HasValue && this._georeference != null ?
472+
(this._lastEllipsoidRadii.Value.x != this._georeference.ellipsoid.radii.x ||
473+
this._lastEllipsoidRadii.Value.y != this._georeference.ellipsoid.radii.y ||
474+
this._lastEllipsoidRadii.Value.z != this._georeference.ellipsoid.radii.z) : true;
456475

457476
// If we don't have a local -> globe fixed matrix yet, we must update from the Transform
458477
bool updateFromTransform = !this._localToGlobeFixedMatrixIsValid;

Runtime/CesiumOriginShift.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ void LateUpdate()
5757
{
5858
CesiumGeoreference georeference = this.GetComponentInParent<CesiumGeoreference>();
5959

60+
if (georeference == null)
61+
{
62+
Debug.LogWarning("CesiumOriginShift is doing nothing because it is not nested inside a game object with a CesiumGeoreference component.");
63+
return;
64+
}
65+
6066
CesiumGlobeAnchor anchor = this.GetComponent<CesiumGlobeAnchor>();
6167

6268
// The RequireComponent attribute should ensure the globe anchor exists, but it may not be active.

Tests/TestCesiumGlobeAnchor.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,27 @@ public void GivesCorrectResultsForDifferentEllipsoids()
290290
anchor.Sync();
291291
Assert.That(anchor.positionGlobeFixed, Is.EqualTo(actualPosEcef).Using(epsilon6));
292292
}
293+
294+
[Test]
295+
public void CanChangePositionWhileDisabled()
296+
{
297+
IEqualityComparer<double3> epsilon6 = Comparers.Double3(1e-6, 1e-4);
298+
299+
double3 positionLlh = new double3(-20, -10, 1000.0);
300+
301+
GameObject goGeoreference = new GameObject("Georeference");
302+
CesiumGeoreference georeference = goGeoreference.AddComponent<CesiumGeoreference>();
303+
georeference.ellipsoid = CesiumEllipsoid.WGS84;
304+
georeference.SetOriginLongitudeLatitudeHeight(positionLlh.x, positionLlh.y, positionLlh.z);
305+
306+
GameObject goAnchored = new GameObject("Anchored");
307+
goAnchored.transform.parent = goGeoreference.transform;
308+
goAnchored.transform.SetPositionAndRotation(new Vector3(0, 0, 0), Quaternion.identity);
309+
goAnchored.SetActive(false);
310+
311+
CesiumGlobeAnchor anchor = goAnchored.AddComponent<CesiumGlobeAnchor>();
312+
anchor.enabled = false;
313+
anchor.longitudeLatitudeHeight = new double3(1, 1, 1);
314+
Assert.That(anchor.longitudeLatitudeHeight, Is.EqualTo(new double3(1, 1, 1)).Using(epsilon6));
315+
}
293316
}

native~/Runtime/src/CesiumGlobeAnchorImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ namespace {
2828

2929
const CesiumGeospatial::Ellipsoid&
3030
getAnchorEllipsoid(const ::DotNet::CesiumForUnity::CesiumGlobeAnchor& anchor) {
31-
3231
anchor.UpdateGeoreferenceIfNecessary();
33-
return anchor._georeference()
34-
.ellipsoid()
35-
.NativeImplementation()
36-
.GetEllipsoid();
32+
CesiumForUnity::CesiumGeoreference georeference = anchor._georeference();
33+
if (georeference == nullptr) {
34+
return CesiumGeospatial::Ellipsoid::WGS84;
35+
}
36+
return georeference.ellipsoid().NativeImplementation().GetEllipsoid();
3737
}
3838

3939
GlobeAnchor createOrUpdateNativeGlobeAnchorFromEcef(

0 commit comments

Comments
 (0)