Cesium for Unity 1.15.2
Loading...
Searching...
No Matches
CesiumGeoreference.cs
Go to the documentation of this file.
1using Reinterop;
2using System;
3using System.Collections.Generic;
4using Unity.Mathematics;
5using UnityEngine;
6
7namespace CesiumForUnity
8{
32
53
85 [ExecuteInEditMode]
86 [ReinteropNativeImplementation("CesiumForUnityNative::CesiumGeoreferenceImpl", "CesiumGeoreferenceImpl.h")]
87 [AddComponentMenu("Cesium/Cesium Georeference")]
88 [IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
89 public partial class CesiumGeoreference : MonoBehaviour
90 {
91 #region Fields
92 [SerializeField]
93 private CesiumEllipsoid _ellipsoidOverride = null;
94
95 [SerializeField]
96 private CesiumGeoreferenceOriginPlacement _originPlacement = CesiumGeoreferenceOriginPlacement.CartographicOrigin;
97
98 [SerializeField]
99 private CesiumGeoreferenceOriginAuthority _originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
100
101 [SerializeField]
102 private double _latitude = 39.736401;
103
104 [SerializeField]
105 private double _longitude = -105.25737;
106
107 [SerializeField]
108 [Delayed]
109 private double _height = 2250.0;
110
111 [SerializeField]
112 [Delayed]
113 private double _ecefX = 6378137.0;
114
115 [SerializeField]
116 [Delayed]
117 private double _ecefY = 0.0;
118
119 [SerializeField]
120 [Delayed]
121 private double _ecefZ = 0.0;
122
123 [SerializeField]
124 private double _scale = 1.0;
125
126 [NonSerialized]
127 private double4x4 _localToEcef = double4x4.identity;
128
129 [NonSerialized]
130 private double4x4 _ecefToLocal = double4x4.identity;
131
132 [NonSerialized]
133 private bool _isInitialized = false;
134
135 [NonSerialized]
136 private HashSet<CesiumGlobeAnchor> _globeAnchors = new HashSet<CesiumGlobeAnchor>();
137
138 [NonSerialized]
139 private CesiumEllipsoid _ellipsoid = null;
140
141 #endregion
142
156 {
157 get => this._originPlacement;
158 set
159 {
160 this._originPlacement = value;
161 this.MoveOrigin();
162 }
163 }
164
173 {
174 get => this._originAuthority;
175 set
176 {
177 this._originAuthority = value;
178 this.originPlacement = CesiumGeoreferenceOriginPlacement.CartographicOrigin;
179 }
180 }
181
192 public double latitude
193 {
194 get => this._latitude;
195 set
196 {
197 this._latitude = Math.Clamp(value, -90, 90);
198 this.originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
199 }
200 }
201
212 public double longitude
213 {
214 get => this._longitude;
215 set
216 {
217 this._longitude = Math.Clamp(value, -180, 180);
218 this.originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
219 }
220 }
221
234 public double height
235 {
236 get => this._height;
237 set
238 {
239 this._height = value;
240 this.originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
241 }
242 }
243
254 public double ecefX
255 {
256 get => this._ecefX;
257 set
258 {
259 this._ecefX = value;
260 this.originAuthority = CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed;
261 }
262 }
263
274 public double ecefY
275 {
276 get => this._ecefY;
277 set
278 {
279 this._ecefY = value;
280 this.originAuthority = CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed;
281 }
282 }
283
294 public double ecefZ
295 {
296 get => this._ecefZ;
297 set
298 {
299 this._ecefZ = value;
300 this.originAuthority = CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed;
301 }
302 }
303
312 public double scale
313 {
314 get => this._scale;
315 set
316 {
317 this._scale = value;
318 this.MoveOrigin();
319 }
320 }
321
322 public double4x4 localToEcefMatrix
323 {
324 get
325 {
326 this.Initialize();
327 return this._localToEcef;
328 }
329 }
330
331 public double4x4 ecefToLocalMatrix
332 {
333 get
334 {
335 this.Initialize();
336 return this._ecefToLocal;
337 }
338 }
339
344 {
345 get
346 {
347 if (this._ellipsoid == null)
348 {
349 // Make a copy of the ellipsoid ScriptableObject
350 this._ellipsoid = ScriptableObject.CreateInstance<CesiumEllipsoid>();
351 this._ellipsoid.SetRadii((this._ellipsoidOverride ?? CesiumEllipsoid.WGS84).radii);
352 }
353
354 return this._ellipsoid;
355 }
356 set
357 {
358 this._ellipsoid = value;
359 }
360 }
361
365 [Tooltip("An event raised when the georeference origin changes.")]
366 public event Action changed;
367
378 public void SetOriginEarthCenteredEarthFixed(double x, double y, double z)
379 {
380 this._ecefX = x;
381 this._ecefY = y;
382 this._ecefZ = z;
383 this.originAuthority = CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed;
384 }
385
400 public void SetOriginLongitudeLatitudeHeight(double longitude, double latitude, double height)
401 {
402 this._longitude = longitude;
403 this._latitude = latitude;
404 this._height = height;
405 this.originAuthority = CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight;
406 }
407
417 public void AddGlobeAnchor(CesiumGlobeAnchor globeAnchor)
418 {
419 this._globeAnchors.Add(globeAnchor);
420 }
421
431 public void RemoveGlobeAnchor(CesiumGlobeAnchor globeAnchor)
432 {
433 this._globeAnchors.Remove(globeAnchor);
434 }
435
454 public void Initialize()
455 {
456 if (!this._isInitialized)
457 {
458 this._isInitialized = true;
459 this.UpdateOtherCoordinates();
460 this.UpdateTransformations();
461 }
462 }
463
467 public void ReloadEllipsoid()
468 {
469 // clear cached ellipsoid so it has to be rebuilt
470 this._ellipsoid = null;
471 this.UpdateTransformations();
472 this.UpdateOtherCoordinates();
473
474 Cesium3DTileset[] tilesets = GetComponentsInChildren<Cesium3DTileset>();
475 foreach (var tileset in tilesets)
476 {
477 tileset.RecreateTileset();
478 }
479 }
480
481 private void UpdateTransformations()
482 {
483 this._localToEcef = this.ComputeLocalToEarthCenteredEarthFixedTransformation();
484 this._ecefToLocal = math.inverse(this._localToEcef);
485 }
486
491 public void MoveOrigin()
492 {
493 if (!this._isInitialized)
494 throw new InvalidOperationException("The origin of a CesiumGeoreference must not be set before its Initialize method is called, either explicitly or via OnEnable.");
495
496 // Scale must be greater than 0
497 if (this._scale < 1e-8)
498 this._scale = 1e-8;
499
500 this.UpdateOtherCoordinates();
501
502 double4x4 oldLocalToEcef = this._localToEcef;
503
504 this.UpdateTransformations();
505
506 if (oldLocalToEcef.Equals(this._localToEcef))
507 {
508 // Origin didn't change meaningfully.
509 return;
510 }
511
512 foreach (CesiumGlobeAnchor anchor in this._globeAnchors)
513 {
514 if (anchor == null)
515 continue;
516
517 anchor.Sync();
518 }
519
520 if (this.changed != null)
521 {
522 this.changed();
523 }
524 }
525
526 private void OnValidate()
527 {
528 if (this._isInitialized)
529 {
530 this.MoveOrigin();
531 }
532 }
533
534 private void OnEnable()
535 {
536 this.Initialize();
537 }
538
539 private void OnDisable()
540 {
541 this._isInitialized = false;
542 }
543
544 private void UpdateOtherCoordinates()
545 {
546 if (this._originAuthority == CesiumGeoreferenceOriginAuthority.LongitudeLatitudeHeight)
547 {
548 double3 ecef =
550 new double3(this._longitude, this._latitude, this._height));
551 this._ecefX = ecef.x;
552 this._ecefY = ecef.y;
553 this._ecefZ = ecef.z;
554 }
555 else if (this._originAuthority == CesiumGeoreferenceOriginAuthority.EarthCenteredEarthFixed)
556 {
557 double3 llh =
559 new double3(this._ecefX, this._ecefY, this._ecefZ));
560 this._longitude = llh.x;
561 this._latitude = llh.y;
562 this._height = llh.z;
563 }
564 }
565
574 public double3
576 {
577 this.Initialize();
578 return math.mul(this._localToEcef, new double4(unityPosition, 1.0)).xyz;
579 }
580
589 public double3
590 TransformEarthCenteredEarthFixedPositionToUnity(double3 earthCenteredEarthFixed)
591 {
592 this.Initialize();
593 return math.mul(this._ecefToLocal, new double4(earthCenteredEarthFixed, 1.0)).xyz;
594 }
595
604 public double3
606 {
607 this.Initialize();
608 return math.mul(this._localToEcef, new double4(unityDirection, 0.0)).xyz;
609 }
610
619 public double3
620 TransformEarthCenteredEarthFixedDirectionToUnity(double3 earthCenteredEarthFixedDirection)
621 {
622 this.Initialize();
623 return math.mul(this._ecefToLocal, new double4(earthCenteredEarthFixedDirection, 0.0)).xyz;
624 }
625
626 private partial double4x4 ComputeLocalToEarthCenteredEarthFixedTransformation();
627 }
628}
A tileset in the 3D Tiles format.
static CesiumEllipsoid WGS84
Obtain a WGS84 ellipsoid.
partial double3 LongitudeLatitudeHeightToCenteredFixed(double3 longitudeLatitudeHeight)
Convert longitude, latitude, and height to Ellipsoid-Centered, Ellipsoid-Fixed (ECEF) coordinates.
partial void SetRadii(double3 newRadii)
Sets the radii of this ellipsoid to the given values.
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.
double3 TransformEarthCenteredEarthFixedPositionToUnity(double3 earthCenteredEarthFixed)
Transform an Earth-Centered, Earth-Fixed position to Unity coordinates.
double scale
The scale of the globe in the Unity world.
void AddGlobeAnchor(CesiumGlobeAnchor globeAnchor)
Register a globe anchor with this georeference.
double ecefZ
The Earth-Centered, Earth-Fixed Z coordinate of the origin of the coordinate system,...
double3 TransformUnityDirectionToEarthCenteredEarthFixed(double3 unityDirection)
Transform a Unity direction to a direction in Earth-Centered, Earth-Fixed (ECEF) coordinates.
CesiumGeoreferenceOriginAuthority originAuthority
Identifies which set of coordinates authoritatively defines the origin of this georeference.
double ecefX
The Earth-Centered, Earth-Fixed X coordinate of the origin of the coordinate system,...
void SetOriginLongitudeLatitudeHeight(double longitude, double latitude, double height)
Sets the origin of the coordinate system to a particular longitude, latitude, and height.
CesiumGeoreferenceOriginPlacement originPlacement
The placement of this GameObject's origin (coordinate 0,0,0) within the tileset.
double latitude
The latitude of the origin of the coordinate system, in degrees, in the range -90 to 90.
double ecefY
The Earth-Centered, Earth-Fixed Y coordinate of the origin of the coordinate system,...
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.
double longitude
The longitude of the origin of the coordinate system, in degrees, in the range -180 to 180.
double height
The height in the origin of the coordinate system, in meters above the ellipsoid.
double3 TransformUnityPositionToEarthCenteredEarthFixed(double3 unityPosition)
Transform a Unity position to Earth-Centered, Earth-Fixed (ECEF) coordinates.
Action changed
An event raised when the georeference changes.
void MoveOrigin()
Recomputes the coordinate system based on an updated origin.
void RemoveGlobeAnchor(CesiumGlobeAnchor globeAnchor)
Deregisters a globe anchor with this georeference, so the globe anchor will no longer be updated when...
void ReloadEllipsoid()
Called when the ellipsoid override property has changed.
void SetOriginEarthCenteredEarthFixed(double x, double y, double z)
Sets the origin of the coordinate system to particular ecefX, ecefY, ecefZ coordinates in the Earth-C...
double3 TransformEarthCenteredEarthFixedDirectionToUnity(double3 earthCenteredEarthFixedDirection)
Transform an Earth-Centered, Earth-Fixed direction to Unity coordinates.
Anchors this game object to the globe.
void Sync()
Synchronizes the properties of this CesiumGlobeAnchor.
CesiumGeoreferenceOriginPlacement
An enumeration of the possible strategies for placing the origin of a CesiumGeoreference.
@ CartographicOrigin
Use a custom position within the tileset as the GameObject's origin.
@ TrueOrigin
Use the tileset's true origin as the GameObject's origin.
CesiumGeoreferenceOriginAuthority
Identifies the set of the coordinates that authoritatively define the origin of a CesiumGeoreference.
@ EarthCenteredEarthFixed
The CesiumGeoreference.ecefX, CesiumGeoreference.ecefY, and CesiumGeoreference.ecefZ properties autho...
@ LongitudeLatitudeHeight
The CesiumGeoreference.longitude, CesiumGeoreference.latitude, and CesiumGeoreference....