Cesium for Unity 1.15.2
Loading...
Searching...
No Matches
CesiumOriginShift.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using Unity.Mathematics;
3using UnityEngine;
4
5namespace CesiumForUnity
6{
26 [RequireComponent(typeof(CesiumGlobeAnchor))]
27 [DisallowMultipleComponent]
28 [AddComponentMenu("Cesium/Cesium Origin Shift")]
29 [IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
30 public class CesiumOriginShift : MonoBehaviour
31 {
41 public double distance
42 {
43 get => _distance;
44 set => _distance = value;
45 }
46
47 [SerializeField]
48 [Min(0)]
49 [Tooltip("The maximum distance between the origin of the Unity coordinate system and " +
50 "the game object to which this component is attached. When this distance is" +
51 "exceeded, the CesiumGeoreference origin is shifted to bring it close to the " +
52 "game object.\n\n" +
53 "When the value of this property is 0.0, the origin is shifted continuously.")]
54 private double _distance = 0.0;
55
56 void LateUpdate()
57 {
58 CesiumGeoreference georeference = this.GetComponentInParent<CesiumGeoreference>();
59
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
66 CesiumGlobeAnchor anchor = this.GetComponent<CesiumGlobeAnchor>();
67
68 // The RequireComponent attribute should ensure the globe anchor exists, but it may not be active.
69 if (anchor == null || !anchor.isActiveAndEnabled)
70 {
71 Debug.LogWarning("CesiumOriginShift is doing nothing because its CesiumGlobeAnchor component is missing or disabled.");
72 return;
73 }
74
75 this.UpdateFromEcef(georeference, anchor.positionGlobeFixed);
76 }
77
78 private List<CesiumSubScene> _sublevelsScratch = new List<CesiumSubScene>();
79
80 private void UpdateFromEcef(CesiumGeoreference georeference, double3 ecef)
81 {
82 CesiumSubScene closestLevel = null;
83 double distanceSquaredToClosest = double.MaxValue;
84
85 // Are we inside a sub-level?
86 georeference.GetComponentsInChildren<CesiumSubScene>(true, this._sublevelsScratch);
87 foreach (CesiumSubScene level in this._sublevelsScratch)
88 {
89 // TODO: Make sure ECEF position is actually up-to-date
90 double x = level.ecefX - ecef.x;
91 double y = level.ecefY - ecef.y;
92 double z = level.ecefZ - ecef.z;
93 double distanceSquared = x * x + y * y + z * z;
94 if (distanceSquared > level.activationRadius * level.activationRadius)
95 // We're outside this level's activation radius
96 continue;
97
98 if (closestLevel == null || distanceSquared < distanceSquaredToClosest)
99 {
100 closestLevel = level;
101 distanceSquaredToClosest = distanceSquared;
102 }
103 }
104
105 if (closestLevel != null)
106 {
107 if (!closestLevel.isActiveAndEnabled)
108 {
109 // Setting a level active will automatically disable all other levels.
110 closestLevel.gameObject.SetActive(true);
111 closestLevel.enabled = true;
112
113 Physics.SyncTransforms();
114 }
115 }
116 else
117 {
118 bool deactivatedAnySublevel = false;
119
120 // Deactivate all active sub-levels
121 foreach (CesiumSubScene level in this._sublevelsScratch)
122 {
123 if (level.isActiveAndEnabled)
124 {
125 level.gameObject.SetActive(false);
126 deactivatedAnySublevel = true;
127 }
128 }
129
130 double distance = math.length(new double3(georeference.ecefX, georeference.ecefY, georeference.ecefZ) - ecef);
131
132 if (distance >= this._distance)
133 {
134 // Update the origin if we've surpassed the distance threshold.
135 georeference.SetOriginEarthCenteredEarthFixed(ecef.x, ecef.y, ecef.z);
136 // Make sure the physics system is informed that things have moved
137 Physics.SyncTransforms();
138 }
139 else if (deactivatedAnySublevel)
140 {
141 Physics.SyncTransforms();
142 }
143 }
144 }
145 }
146}
Controls how global geospatial coordinates are mapped to coordinates in the Unity scene.
Automatically shifts the origin of a CesiumGeoreference as the object to which it is attached moves.
double distance
The maximum distance between the origin of the Unity coordinate system and the game object to which t...