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

Change CesiumCameraController and CesiumFlyToController to use CesiumGlobeAnchor on parent GameObjects #422

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 39 additions & 9 deletions Runtime/CesiumCameraController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace CesiumForUnity
/// it automatically changes its own up direction such that the world always
/// looks right-side up.
/// </summary>
[RequireComponent(typeof(CesiumOriginShift))]
[RequireComponent(typeof(Camera))]
[DisallowMultipleComponent]
[AddComponentMenu("Cesium/Cesium Camera Controller")]
Expand Down Expand Up @@ -231,27 +230,27 @@ void ConfigureInputs()

#region Initialization

void InitializeCamera()
private void InitializeCamera()
{
this._camera = this.gameObject.GetComponent<Camera>();
this._initialNearClipPlane = this._camera.nearClipPlane;
this._initialFarClipPlane = this._camera.farClipPlane;
}

void InitializeController()
private void InitializeController()
{
if (this.gameObject.GetComponent<CharacterController>() != null)
if (this._globeAnchor.GetComponent<CharacterController>() != null)
{
Debug.LogWarning(
"A CharacterController component was manually " +
"added to the CesiumCameraController's game object. " +
"added to the CesiumGlobeAnchor's game object. " +
"This may interfere with the CesiumCameraController's movement.");

this._controller = this.gameObject.GetComponent<CharacterController>();
this._controller = this._globeAnchor.GetComponent<CharacterController>();
}
else
{
this._controller = this.gameObject.AddComponent<CharacterController>();
this._controller = this._globeAnchor.gameObject.AddComponent<CharacterController>();
this._controller.hideFlags = HideFlags.HideInInspector;
}

Expand Down Expand Up @@ -302,8 +301,19 @@ void Awake()
"with a CesiumGeoreference.");
}

// CesiumOriginShift will add a CesiumGlobeAnchor automatically.
this._globeAnchor = this.gameObject.GetComponent<CesiumGlobeAnchor>();
this._globeAnchor = this.gameObject.GetComponentInParent<CesiumGlobeAnchor>();
if (this._globeAnchor == null)
{
Debug.LogError("CesiumCameraController must have a CesiumGlobeAnchor " +
"attached to itself or a parent");
}

CesiumOriginShift originShift = this._globeAnchor.GetComponent<CesiumOriginShift>();
if (originShift == null)
{
Debug.LogError("CesiumCameraController expects a CesiumOriginShift " +
$"on {this._globeAnchor?.name}, none found");
}

this.InitializeCamera();
this.InitializeController();
Expand All @@ -314,6 +324,26 @@ void Awake()
#endif
}

#if UNITY_EDITOR
// Ensures required components are present in the editor.
private void Reset()
{
CesiumGlobeAnchor anchor = this.gameObject.GetComponentInParent<CesiumGlobeAnchor>();
if (anchor == null)
{
anchor = this.gameObject.AddComponent<CesiumGlobeAnchor>();
Debug.LogWarning("CesiumCameraController missing a CesiumGlobeAnchor - adding");
}

CesiumOriginShift origin = anchor.GetComponent<CesiumOriginShift>();
if (origin == null)
{
origin = anchor.gameObject.AddComponent<CesiumOriginShift>();
Debug.LogWarning("CesiumCameraController missing a CesiumOriginShift - adding");
}
}
#endif

#endregion

#region Update
Expand Down
36 changes: 32 additions & 4 deletions Runtime/CesiumFlyToController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace CesiumForUnity
/// it will disable inputs on CesiumCameraController as necessary, such as
/// camera rotation with the mouse.
/// </remarks>
[RequireComponent(typeof(CesiumOriginShift))]
[DisallowMultipleComponent]
[AddComponentMenu("Cesium/Cesium Fly To Controller")]
[IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
Expand Down Expand Up @@ -156,8 +155,17 @@ void Awake()
"with a CesiumGeoreference.");
}

// CesiumOriginShift will add a CesiumGlobeAnchor automatically.
this._globeAnchor = this.gameObject.GetComponent<CesiumGlobeAnchor>();
this._globeAnchor = this.gameObject.GetComponentInParent<CesiumGlobeAnchor>();
if (this._globeAnchor == null)
{
Debug.LogError("CesiumFlyToController expects a CesiumGlobeAnchor to be attached to itself or to a parent");
}

CesiumOriginShift originShift = this._globeAnchor.GetComponent<CesiumOriginShift>();
if (originShift == null)
{
Debug.LogError("CesiumFlyToController expects a CesiumOriginShift to be attached to the CesiumGlobeAnchor above it");
}

// If a CesiumCameraController is present, CesiumFlyToController will account for
// its inputs and modify it during flights.
Expand All @@ -172,6 +180,26 @@ void Update()
}
}

#if UNITY_EDITOR
// Ensures required components are present in the editor.
private void Reset()
{
CesiumGlobeAnchor anchor = this.gameObject.GetComponentInParent<CesiumGlobeAnchor>();
if(anchor == null)
{
anchor = this.gameObject.AddComponent<CesiumGlobeAnchor>();
Debug.LogWarning("CesiumFlyToController missing a CesiumGlobeAnchor - adding");
}

CesiumOriginShift origin = anchor.GetComponent<CesiumOriginShift>();
if(origin == null)
{
origin = anchor.gameObject.AddComponent<CesiumOriginShift>();
Debug.LogWarning("CesiumFlyToController missing a CesiumOriginShift - adding");
}
}
#endif

/// <summary>
/// Whether this controller detects movement from other controllers on the game object.
/// </summary>
Expand Down Expand Up @@ -276,7 +304,7 @@ private void CompleteFlight()
{
double3 finalPoint = this._keypoints[this._keypoints.Count - 1];
this._globeAnchor.positionGlobeFixed = finalPoint;

this._globeAnchor.rotationEastUpNorth = this._flyToDestinationRotation;

this._flyingToLocation = false;
Expand Down
3 changes: 2 additions & 1 deletion Tests/TestCesiumFlyToController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public IEnumerator FlyToLocationLongitudeLatitudeHeight()
GameObject goFlyer = new GameObject("Flyer");
goFlyer.transform.parent = goGeoreference.transform;

CesiumGlobeAnchor anchor = goFlyer.AddComponent<CesiumGlobeAnchor>();
goFlyer.AddComponent<CesiumOriginShift>();
CesiumFlyToController flyToController = goFlyer.AddComponent<CesiumFlyToController>();
CesiumGlobeAnchor anchor = goFlyer.GetComponent<CesiumGlobeAnchor>();

// Make the flight fast so the test doesn't take too long.
flyToController.flyToDuration = 0.25;
Expand Down
Loading