Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify CesiumSubScene to use parent CesiumGeoreference's coordinates when added in-editor. #391

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
##### Fixes :wrench:

- Fixed a bug that prevented the default `CesiumIonServer` asset from remembering its token in a clean project.
- Fixed a bug where adding a `CesiumSubScene` as the child of an existing `CesiumGeoreference` in editor would cause the parent `CesiumGeoreference` to have its coordinates reset to the default.

### v1.7.0 - 2023-12-14

Expand Down
8 changes: 8 additions & 0 deletions EditorTests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions EditorTests/CesiumEditorTests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "CesiumEditorTests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"CesiumRuntime",
"Unity.Mathematics"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions EditorTests/CesiumEditorTests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions EditorTests/TestCesiumSubScene.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CesiumForUnity;
using NUnit.Framework;
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;

public class TestCesiumSubScene
{
[UnityTest]
public IEnumerator AddingSubSceneCopiesGeoreferenceCoordinates()
{
GameObject goGeoreference = new GameObject("Georeference");
CesiumGeoreference georeference = goGeoreference.AddComponent<CesiumGeoreference>();
georeference.SetOriginLongitudeLatitudeHeight(-55.0, 55.0, 1000.0);

GameObject goSubScene = new GameObject("SubScene");
goSubScene.transform.parent = goGeoreference.transform;
CesiumSubScene subScene = goSubScene.AddComponent<CesiumSubScene>();

yield return null;

Assert.AreEqual(-55.0, georeference.longitude);
Assert.AreEqual(55.0, georeference.latitude);
Assert.AreEqual(1000.0, georeference.height);
Assert.AreEqual(georeference.longitude, subScene.longitude);
Assert.AreEqual(georeference.latitude, subScene.latitude);
Assert.AreEqual(georeference.height, subScene.height);
}

[UnityTest]
public IEnumerator ModifyingSubsceneModifiesParentGeoreference()
{
GameObject goGeoreference = new GameObject("Georeference");
CesiumGeoreference georeference = goGeoreference.AddComponent<CesiumGeoreference>();
georeference.SetOriginLongitudeLatitudeHeight(-55.0, 55.0, 1000.0);

GameObject goSubScene = new GameObject("SubScene");
goSubScene.transform.parent = goGeoreference.transform;
CesiumSubScene subScene = goSubScene.AddComponent<CesiumSubScene>();

yield return null;

subScene.SetOriginLongitudeLatitudeHeight(-10.0, 10.0, 100.0);

yield return null;

Assert.AreEqual(subScene.longitude, -10.0);
Assert.AreEqual(subScene.latitude, 10.0);
Assert.AreEqual(subScene.height, 100.0);

Assert.AreEqual(georeference.longitude, -10.0);
Assert.AreEqual(georeference.latitude, 10.0);
Assert.AreEqual(georeference.height, 100.0);
}
}
11 changes: 11 additions & 0 deletions EditorTests/TestCesiumSubScene.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 50 additions & 9 deletions Runtime/CesiumSubScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ public double ecefZ
[NonSerialized]
internal CesiumGeoreference _parentGeoreference;

// The coordinates of the parent CesiumGeoreference before being changed by `UpdateOrigin`.
// This is because `OnEnable` runs before `Reset` does, so to be able to use the parent's
// coordinates as the default values without changing the OnEnable behavior of CesiumSubScene,
// we need to store what those values were before.
azrogers marked this conversation as resolved.
Show resolved Hide resolved
[NonSerialized]
private double3 _oldParentCoordinates = double3.zero;

[NonSerialized]
private CesiumGeoreferenceOriginAuthority _oldParentOriginAuthority;

azrogers marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Sets the origin of the coordinate system to particular <see cref="ecefX"/>, <see cref="ecefY"/>,
/// <see cref="ecefZ"/> coordinates in the Earth-Centered, Earth-Fixed (ECEF) frame.
Expand Down Expand Up @@ -231,6 +241,17 @@ public void SetOriginLongitudeLatitudeHeight(double longitude, double latitude,
this.originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
}

private void CopyParentCoordinates()
{
this._longitude = this._parentGeoreference.longitude;
this._latitude = this._parentGeoreference.latitude;
this._height = this._parentGeoreference.height;

this._ecefX = this._parentGeoreference.ecefX;
this._ecefY = this._parentGeoreference.ecefY;
this._ecefZ = this._parentGeoreference.ecefZ;
}

private void DetachFromParentIfNeeded()
{
if (this._parentGeoreference != null)
Expand Down Expand Up @@ -271,6 +292,22 @@ private void OnValidate()
private void Reset()
azrogers marked this conversation as resolved.
Show resolved Hide resolved
{
this.UpdateParentReference();

// The default coordinates for the CesiumSubScene component should be the coordinates of its parent, if possible.
// This means adding the component as the child of an existing CesiumGeoreference won't reset the parent's coordinates.
if (this._parentGeoreference != null)
{
if(this._oldParentOriginAuthority == CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed)
{
this._parentGeoreference.SetOriginEarthCenteredEarthFixed(this._oldParentCoordinates.x, this._oldParentCoordinates.y, this._oldParentCoordinates.z);
}
else
{
this._parentGeoreference.SetOriginLongitudeLatitudeHeight(this._oldParentCoordinates.x, this._oldParentCoordinates.y, this._oldParentCoordinates.z);
}

this.CopyParentCoordinates();
}
}

private void OnEnable()
Expand Down Expand Up @@ -310,13 +347,7 @@ private void OnParentChanged()

// Update our origin to our parent georef, maintain our origin authority,
// and copy both sets of reference coordinates. No need to calculate any of this again
this._longitude = this._parentGeoreference.longitude;
this._latitude = this._parentGeoreference.latitude;
this._height = this._parentGeoreference.height;

this._ecefX = this._parentGeoreference.ecefX;
this._ecefY = this._parentGeoreference.ecefY;
this._ecefZ = this._parentGeoreference.ecefZ;
CopyParentCoordinates();
j9liu marked this conversation as resolved.
Show resolved Hide resolved
}

private void OnDisable()
Expand Down Expand Up @@ -373,6 +404,16 @@ public void UpdateOrigin()
if (this._parentGeoreference == null)
throw new InvalidOperationException("CesiumSubScene is not nested inside a game object with a CesiumGeoreference.");

this._oldParentOriginAuthority = this._parentGeoreference.originAuthority;
if(this._oldParentOriginAuthority == CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed)
{
this._oldParentCoordinates = new double3(this._parentGeoreference.ecefX, this._parentGeoreference.ecefY, this._parentGeoreference.ecefZ);
}
else
{
this._oldParentCoordinates = new double3(this._parentGeoreference.longitude, this._parentGeoreference.latitude, this._parentGeoreference.height);
}

azrogers marked this conversation as resolved.
Show resolved Hide resolved
if (this.originAuthority == CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed)
this._parentGeoreference.SetOriginEarthCenteredEarthFixed(
this._ecefX,
Expand All @@ -386,7 +427,7 @@ public void UpdateOrigin()
}
}

#if UNITY_EDITOR
#if UNITY_EDITOR
private void OnDrawGizmos()
{
if (this._showActivationRadius)
Expand All @@ -396,6 +437,6 @@ private void OnDrawGizmos()
Gizmos.DrawWireSphere(this.transform.position, (float)this._activationRadius);
}
}
#endif
#endif
}
}
Loading