3using System.Collections;
5using Unity.Mathematics;
44 [ReinteropNativeImplementation(
"CesiumForUnityNative::CesiumGlobeAnchorImpl",
"CesiumGlobeAnchorImpl.h", staticOnly:
true)]
45 [AddComponentMenu(
"Cesium/Cesium Globe Anchor")]
46 [IconAttribute(
"Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
55 internal bool _adjustOrientationForGlobeWhenMoving =
true;
58 internal bool _detectTransformChanges =
true;
61 internal double4x4 _localToGlobeFixedMatrix = double4x4.identity;
66 internal bool _localToGlobeFixedMatrixIsValid =
false;
71 internal bool _lastLocalsAreValid =
false;
74 internal Vector3 _lastLocalPosition;
77 internal Quaternion _lastLocalRotation;
80 internal Vector3 _lastLocalScale;
88 internal double3? _lastEllipsoidRadii;
92 #region User-editable properties
120 get => this._adjustOrientationForGlobeWhenMoving;
121 set => this._adjustOrientationForGlobeWhenMoving = value;
143 get => this._detectTransformChanges;
146 this._detectTransformChanges = value;
147 this.StartOrStopDetectingTransformChanges();
165 this.InitializeEcefIfNeeded();
166 return this._localToGlobeFixedMatrix;
170 this.InitializeEcefIfNeeded();
171 this.UpdateEcef(value);
190 this.UpdateGeoreferenceIfNecessary();
192 if (this._georeference ==
null || this._georeference.
ellipsoid ==
null)
195 return new double3(0.0, 0.0, 0.0);
202 this.UpdateGeoreferenceIfNecessary();
204 if (this._georeference ==
null || this._georeference.
ellipsoid ==
null)
226 newModelToEcef.c3 =
new double4(value.x, value.y, value.z, 1.0);
227 this.localToGlobeFixedMatrix = newModelToEcef;
253 Helpers.MatrixToTranslationRotationAndScale(this.
localToGlobeFixedMatrix, out translation, out rotation, out scale);
263 Helpers.MatrixToTranslationRotationAndScale(this.
localToGlobeFixedMatrix, out translation, out rotation, out scale);
264 this.localToGlobeFixedMatrix = Helpers.TranslationRotationAndScaleToMatrix(translation, value, scale);
282 this.InitializeEcefIfNeeded();
283 return this.GetLocalToEastUpNorthRotation();
287 this.InitializeEcefIfNeeded();
288 this.SetLocalToEastUpNorthRotation(value);
308 Helpers.MatrixToTranslationRotationAndScale(this.
localToGlobeFixedMatrix, out translation, out rotation, out scale);
318 Helpers.MatrixToTranslationRotationAndScale(this.
localToGlobeFixedMatrix, out translation, out rotation, out scale);
319 this.localToGlobeFixedMatrix = Helpers.TranslationRotationAndScaleToMatrix(translation, rotation, value);
336 set => this.scaleGlobeFixed = -value;
341 #region Deprecated Functionality
343 [Obsolete(
"Use positionGlobeFixed.x instead.")]
351 this.positionGlobeFixed = position;
355 [Obsolete(
"Use positionGlobeFixed.y instead.")]
363 this.positionGlobeFixed = position;
367 [Obsolete(
"Use positionGlobeFixed.z instead.")]
375 this.positionGlobeFixed = position;
379 [Obsolete(
"Use longitudeLatitudeHeight.x instead.")]
387 this.longitudeLatitudeHeight = position;
391 [Obsolete(
"Use longitudeLatitudeHeight.y instead.")]
399 this.longitudeLatitudeHeight = position;
403 [Obsolete(
"Use longitudeLatitudeHeight.z instead.")]
411 this.longitudeLatitudeHeight = position;
415 [Obsolete(
"Set the longitudeLatitudeHeight property instead.")]
422 [Obsolete(
"Set the positionGlobeFixed property instead.")]
425 this.positionGlobeFixed =
new double3(x, y, z);
430 #region Public Methods
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;
477 bool updateFromTransform = !this._localToGlobeFixedMatrixIsValid;
478 if (!isEllipsoidChanged && !updateFromTransform && this._lastLocalsAreValid)
482 updateFromTransform =
483 this._lastLocalPosition != this.transform.localPosition ||
484 this._lastLocalRotation != this.transform.localRotation ||
485 this._lastLocalScale != this.transform.localScale;
488 if (isEllipsoidChanged || updateFromTransform)
489 this.UpdateEcefFromTransform();
491 this.UpdateEcef(this._localToGlobeFixedMatrix);
501 this.UpdateGeoreference();
503 if (this._georeference ==
null)
504 Debug.LogWarning($
"{this.gameObject.name} is not nested inside a game object with a CesiumGeoreference. Most of its CesiumGlobeAnchor functionality will not work.");
507 this.StartOrStopDetectingTransformChanges();
512 #region Unity Messages
514 private void UpdateGeoreference()
516 if (this._georeference !=
null)
521 if (this._georeference !=
null)
524 if (this.isActiveAndEnabled)
529 internal void UpdateGeoreferenceIfNecessary()
531 if (this._georeference ==
null)
532 this.UpdateGeoreference();
535 private void OnEnable()
550 private void OnDisable()
552 if (this._georeference !=
null)
554 this._georeference =
null;
557 private void OnTransformParentChanged()
559 this.UpdateGeoreference();
567 private void StartOrStopDetectingTransformChanges()
569 this.StopCoroutine(
"DetectTransformChanges");
571 bool start = this._detectTransformChanges;
575 if (!EditorApplication.isPlaying)
580 if (!this.isActiveAndEnabled)
584 this.StartCoroutine(
"DetectTransformChanges");
587 private IEnumerator DetectTransformChanges()
591 WaitUntil waitForChanges =
new WaitUntil(() => this._lastLocalsAreValid && (
592 this.transform.localPosition !=
this._lastLocalPosition ||
593 this.transform.localRotation !=
this._lastLocalRotation ||
594 this.transform.localScale !=
this._lastLocalScale));
598 yield
return waitForChanges;
599 this.UpdateEcefFromTransform();
607 private void InitializeEcefIfNeeded()
609 if (!this._localToGlobeFixedMatrixIsValid)
610 this.UpdateEcefFromTransform();
613 private void UpdateEcef(double4x4 newModelToEcef)
615 this.UpdateGeoreferenceIfNecessary();
616 this.SetNewLocalToGlobeFixedMatrix(newModelToEcef);
619 private void UpdateEcefFromTransform()
621 this.UpdateGeoreferenceIfNecessary();
622 if (this._georeference !=
null)
623 this.SetNewLocalToGlobeFixedMatrixFromTransform();
625 PrefabUtility.RecordPrefabInstancePropertyModifications(
this);
636 private partial
void SetNewLocalToGlobeFixedMatrix(double4x4 newLocalToGlobeFixedMatrix);
644 private partial
void SetNewLocalToGlobeFixedMatrixFromTransform();
652 private partial quaternion GetLocalToEastUpNorthRotation();
660 private partial
void SetLocalToEastUpNorthRotation(quaternion newRotation);
partial double3 LongitudeLatitudeHeightToCenteredFixed(double3 longitudeLatitudeHeight)
Convert longitude, latitude, and height to Ellipsoid-Centered, Ellipsoid-Fixed (ECEF) coordinates.
partial double3 CenteredFixedToLongitudeLatitudeHeight(double3 ellipsoidCenteredEllipsoidFixed)
Convert Ellipsoid-Centered, Ellipsoid-Fixed (ECEF) coordinates to longitude, latitude,...
Controls how global geospatial coordinates are mapped to coordinates in the Unity scene.
void AddGlobeAnchor(CesiumGlobeAnchor globeAnchor)
Register a globe anchor with this georeference.
void Initialize()
Initializes this georeference so that other objects may use it to locate the globe in the world.
CesiumEllipsoid ellipsoid
The CesiumEllipsoid that is referenced.
void RemoveGlobeAnchor(CesiumGlobeAnchor globeAnchor)
Deregisters a globe anchor with this georeference, so the globe anchor will no longer be updated when...
Anchors this game object to the globe.
void SetPositionEarthCenteredEarthFixed(double x, double y, double z)
double3 longitudeLatitudeHeight
Gets or sets the longitude, latitude, and height of this object.
quaternion rotationGlobeFixed
Gets or sets the rotation from the game object's coordinate system to the Earth-Centered,...
double4x4 localToGlobeFixedMatrix
Gets or sets the 4x4 transformation matrix from this game object's local coordinate system to the Ear...
bool detectTransformChanges
Gets or sets whether to automatically detect changes in the game object's.
bool adjustOrientationForGlobeWhenMoving
Gets or sets whether to adjust the game object's orientation based on globe curvature as the game obj...
quaternion rotationEastUpNorth
Gets or sets the rotation from the game object's coordinate system to a local coordinate system cente...
void SetPositionLongitudeLatitudeHeight(double longitude, double latitude, double height)
double3 scaleGlobeFixed
Gets the scale from the game object's coordinate system to the Earth-Centered, Earth-Fixed coordinate...
double3 positionGlobeFixed
Gets or sets the game object's position in the Earth-Centered, Earth-Fixed (ECEF) coordinates in mete...
void Sync()
Synchronizes the properties of this CesiumGlobeAnchor.
double3 scaleEastUpNorth
Gets or sets the scale from the game object's coordinate system to a local coordinate system centered...
void Restart()
Completely re-initializes the state of this object from its serialized properties.
An interface for an object with a Restart method, allowing the state of the object to be reinitialize...