| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEditor; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Skybox |
| | 8 | | { |
| | 9 | |
|
| | 10 | | public class SatelliteLayerBehaviour : MonoBehaviour |
| | 11 | | { |
| | 12 | | Config3DSatellite layerProperties; |
| | 13 | | public GameObject satelliteOrbit; |
| | 14 | | public GameObject dummyObj; |
| | 15 | | public GameObject satellite; |
| | 16 | |
|
| 0 | 17 | | public float thickness = 1; |
| | 18 | |
|
| | 19 | | private float currentAngle; |
| 0 | 20 | | private float cycleTime = 24; |
| | 21 | | private List<Material> materials; |
| | 22 | | private Transform camTransform; |
| | 23 | |
|
| | 24 | | private void UpdateRotation() |
| | 25 | | { |
| 0 | 26 | | if (satellite == null) |
| | 27 | | { |
| 0 | 28 | | return; |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | if (camTransform == null) |
| | 32 | | { |
| 0 | 33 | | if (Camera.main != null) |
| | 34 | | { |
| 0 | 35 | | camTransform = Camera.main.transform; |
| | 36 | | } |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | switch (layerProperties.satelliteRotation) |
| | 40 | | { |
| | 41 | | case RotationType.Fixed: |
| 0 | 42 | | satellite.transform.localRotation = Quaternion.Euler(layerProperties.fixedRotation); |
| 0 | 43 | | break; |
| | 44 | | case RotationType.Rotate: |
| 0 | 45 | | satellite.transform.Rotate(layerProperties.rotateAroundAxis, layerProperties.rotateSpeed * Time.delt |
| 0 | 46 | | break; |
| | 47 | | case RotationType.LookAtOrbit: |
| 0 | 48 | | if (camTransform != null) |
| | 49 | | { |
| 0 | 50 | | satellite.transform.LookAt(camTransform); |
| | 51 | | } |
| | 52 | | break; |
| | 53 | | default: |
| | 54 | | break; |
| | 55 | | } |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | internal void AssignValues(Config3DSatellite properties, float timeOfTheDay, float cycleTime, bool isEditor = fa |
| | 59 | | { |
| | 60 | | // Check and assign Materials |
| 0 | 61 | | CheckAndAssignMats(); |
| | 62 | |
|
| 0 | 63 | | layerProperties = properties; |
| 0 | 64 | | layerProperties.radius = Mathf.Clamp(layerProperties.radius, 0, Mathf.Infinity); |
| | 65 | |
|
| 0 | 66 | | this.cycleTime = cycleTime; |
| | 67 | |
|
| 0 | 68 | | if (!CheckIfSatelliteInTimeBounds(timeOfTheDay)) |
| | 69 | | { |
| 0 | 70 | | satellite.gameObject.SetActive(false); |
| 0 | 71 | | ChangeRenderType(LayerRenderType.NotRendering); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | satellite.gameObject.SetActive(true); |
| 0 | 76 | | ChangeRenderType(LayerRenderType.Rendering); |
| | 77 | |
|
| | 78 | | // Apply fade |
| 0 | 79 | | ApplyFade(timeOfTheDay); |
| | 80 | |
|
| | 81 | | // Update orbit rotation |
| 0 | 82 | | UpdateOrbit(); |
| | 83 | | // Change satellite size |
| 0 | 84 | | UpdateSatelliteSize(); |
| | 85 | |
|
| | 86 | | // Update SatellitePosition |
| 0 | 87 | | UpdateSatellitePos(timeOfTheDay); |
| | 88 | |
|
| 0 | 89 | | UpdateRotation(); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void CheckAndAssignMats() |
| | 93 | | { |
| 0 | 94 | | if (materials == null) |
| | 95 | | { |
| 0 | 96 | | materials = new List<Material>(); |
| 0 | 97 | | Renderer[] renderers = GetComponentsInChildren<Renderer>(); |
| 0 | 98 | | for (int i = 0; i < renderers.Length; i++) |
| | 99 | | { |
| 0 | 100 | | for (int j = 0; j < renderers[i].sharedMaterials.Length; j++) |
| | 101 | | { |
| 0 | 102 | | materials.Add(renderers[i].sharedMaterials[j]); |
| | 103 | | } |
| | 104 | | } |
| | 105 | | } |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | public void ChangeRenderType(LayerRenderType type) |
| | 109 | | { |
| 0 | 110 | | if (layerProperties == null) |
| | 111 | | { |
| 0 | 112 | | return; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | layerProperties.renderType = type; |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void UpdateSatellitePos(float timeOfTheDay) |
| | 119 | | { |
| 0 | 120 | | float timeOfDayEdited = timeOfTheDay; |
| | 121 | |
|
| 0 | 122 | | if (timeOfTheDay < layerProperties.timeSpanStart) |
| | 123 | | { |
| 0 | 124 | | timeOfDayEdited += cycleTime; |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | float diff = timeOfDayEdited - layerProperties.timeSpanStart; |
| 0 | 128 | | currentAngle = layerProperties.initialAngle + (diff * layerProperties.movementSpeed); |
| | 129 | |
|
| 0 | 130 | | dummyObj.transform.localPosition = GetSatellitePosition(layerProperties.radius, currentAngle); |
| 0 | 131 | | satellite.transform.position = dummyObj.transform.position; |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | private void UpdateSatelliteSize() |
| | 135 | | { |
| 0 | 136 | | if (satellite == null) |
| | 137 | | { |
| 0 | 138 | | return; |
| | 139 | | } |
| | 140 | | // change satellite size |
| 0 | 141 | | satellite.transform.localScale = Vector3.one * layerProperties.satelliteSize; |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Update Rotation and position |
| | 146 | | /// </summary> |
| | 147 | | private void UpdateOrbit() |
| | 148 | | { |
| 0 | 149 | | if (satelliteOrbit == null) |
| | 150 | | { |
| 0 | 151 | | Debug.LogError("Satellite Orbit not assigned"); |
| 0 | 152 | | return; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | // Orbit vertical placement |
| 0 | 156 | | Vector3 pos = satelliteOrbit.transform.localPosition; |
| 0 | 157 | | pos.y = layerProperties.orbitYOffset; |
| 0 | 158 | | satelliteOrbit.transform.localPosition = pos; |
| | 159 | |
|
| | 160 | | // Rotate orbit plane along horizon line |
| 0 | 161 | | Vector3 rot = satelliteOrbit.transform.localRotation.eulerAngles; |
| 0 | 162 | | rot.z = 0; |
| 0 | 163 | | rot.y = layerProperties.horizonPlaneRotation; |
| | 164 | | // Rotate orbit plane along inclination line |
| 0 | 165 | | rot.x = layerProperties.inclination; |
| 0 | 166 | | satelliteOrbit.transform.localRotation = Quaternion.Euler(rot); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | private Vector3 GetSatellitePosition(float radius, float angle) |
| | 170 | | { |
| 0 | 171 | | angle = angle % 360; |
| 0 | 172 | | float angleEdited = (90 - angle) * Mathf.Deg2Rad; |
| 0 | 173 | | float x = radius * Mathf.Cos(angleEdited); |
| 0 | 174 | | float y = radius * Mathf.Sin(angleEdited); |
| 0 | 175 | | return new Vector3(x, y, 0); |
| | 176 | | } |
| | 177 | |
|
| | 178 | | private bool CheckIfSatelliteInTimeBounds(float timeOfTheDay) |
| | 179 | | { |
| | 180 | | // Calculate edited time for the case of out time less than in time (over the day scenario) |
| 0 | 181 | | float outTimeEdited = layerProperties.timeSpanEnd; |
| 0 | 182 | | float timeOfTheDayEdited = timeOfTheDay; |
| | 183 | |
|
| 0 | 184 | | if (layerProperties.timeSpanEnd < layerProperties.timeSpanStart) |
| | 185 | | { |
| 0 | 186 | | outTimeEdited += cycleTime; |
| | 187 | | } |
| | 188 | |
|
| 0 | 189 | | if (timeOfTheDay < layerProperties.timeSpanStart) |
| | 190 | | { |
| 0 | 191 | | timeOfTheDayEdited += cycleTime; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | if (timeOfTheDayEdited >= layerProperties.timeSpanStart && timeOfTheDayEdited <= outTimeEdited) |
| | 195 | | { |
| 0 | 196 | | return true; |
| | 197 | | } |
| | 198 | | else |
| | 199 | | { |
| 0 | 200 | | return false; |
| | 201 | | } |
| | 202 | | } |
| | 203 | |
|
| | 204 | | private void ApplyFade(float timeOfTheDay) |
| | 205 | | { |
| 0 | 206 | | float fadeAmount = 1; |
| | 207 | |
|
| 0 | 208 | | if (CheckFadingIn(timeOfTheDay, out fadeAmount)) |
| | 209 | | { |
| 0 | 210 | | for (int i = 0; i < materials.Count; i++) |
| | 211 | | { |
| 0 | 212 | | materials[i].SetFloat(SkyboxShaderUtils.Opacity, fadeAmount); |
| | 213 | | } |
| 0 | 214 | | } |
| 0 | 215 | | else if (CheckFadingOut(timeOfTheDay, out fadeAmount)) |
| | 216 | | { |
| 0 | 217 | | for (int i = 0; i < materials.Count; i++) |
| | 218 | | { |
| 0 | 219 | | materials[i].SetFloat(SkyboxShaderUtils.Opacity, fadeAmount); |
| | 220 | | } |
| 0 | 221 | | } |
| | 222 | | else |
| | 223 | | { |
| 0 | 224 | | for (int i = 0; i < materials.Count; i++) |
| | 225 | | { |
| 0 | 226 | | materials[i].SetFloat(SkyboxShaderUtils.Opacity, fadeAmount); |
| | 227 | | } |
| | 228 | |
|
| | 229 | | } |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | private bool CheckFadingIn(float dayTime, out float fadeAmt) |
| | 233 | | { |
| 0 | 234 | | fadeAmt = 1; |
| 0 | 235 | | bool fadeChanged = false; |
| 0 | 236 | | float fadeInCompletionTime = layerProperties.timeSpanStart + layerProperties.fadeInTime; |
| 0 | 237 | | float dayTimeEdited = dayTime; |
| 0 | 238 | | if (dayTime < layerProperties.timeSpanStart) |
| | 239 | | { |
| 0 | 240 | | dayTimeEdited = cycleTime + dayTime; |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | if (dayTimeEdited < fadeInCompletionTime) |
| | 244 | | { |
| 0 | 245 | | float percentage = Mathf.InverseLerp(layerProperties.timeSpanStart, fadeInCompletionTime, dayTimeEdited) |
| 0 | 246 | | fadeAmt = percentage; |
| 0 | 247 | | fadeChanged = true; |
| | 248 | | } |
| | 249 | |
|
| 0 | 250 | | return fadeChanged; |
| | 251 | | } |
| | 252 | |
|
| | 253 | | private bool CheckFadingOut(float dayTime, out float fadeAmt) |
| | 254 | | { |
| 0 | 255 | | fadeAmt = 1; |
| 0 | 256 | | bool fadeChanged = false; |
| 0 | 257 | | float endTimeEdited = layerProperties.timeSpanEnd; |
| 0 | 258 | | float dayTimeEdited = dayTime; |
| | 259 | |
|
| 0 | 260 | | if (layerProperties.timeSpanEnd < layerProperties.timeSpanStart) |
| | 261 | | { |
| 0 | 262 | | endTimeEdited = cycleTime + layerProperties.timeSpanEnd; |
| | 263 | | } |
| | 264 | |
|
| 0 | 265 | | if (dayTime < layerProperties.timeSpanStart) |
| | 266 | | { |
| 0 | 267 | | dayTimeEdited = cycleTime + dayTime; |
| | 268 | | } |
| | 269 | |
|
| | 270 | |
|
| 0 | 271 | | float fadeOutStartTime = endTimeEdited - layerProperties.fadeOutTime; |
| | 272 | |
|
| 0 | 273 | | if (dayTimeEdited > fadeOutStartTime) |
| | 274 | | { |
| 0 | 275 | | float percentage = Mathf.InverseLerp(endTimeEdited, fadeOutStartTime, dayTimeEdited); |
| 0 | 276 | | fadeAmt = percentage; |
| 0 | 277 | | fadeChanged = true; |
| | 278 | | } |
| | 279 | |
|
| 0 | 280 | | return fadeChanged; |
| | 281 | | } |
| | 282 | |
|
| | 283 | | #if UNITY_EDITOR |
| | 284 | | private void OnDrawGizmos() |
| | 285 | | { |
| 0 | 286 | | var tr = transform; |
| 0 | 287 | | var position = tr.position; |
| 0 | 288 | | Handles.color = Color.gray; |
| 0 | 289 | | Handles.DrawWireDisc(position, Vector3.up, 1000, thickness); |
| | 290 | |
|
| 0 | 291 | | if (satelliteOrbit == null || layerProperties == null) |
| | 292 | | { |
| 0 | 293 | | return; |
| | 294 | | } |
| 0 | 295 | | position = satelliteOrbit.transform.position; |
| | 296 | | // Draw wire disc of green color for orbit orthogonal to y = 1 |
| 0 | 297 | | Handles.color = Color.green; |
| 0 | 298 | | Handles.DrawWireDisc(position, satelliteOrbit.transform.forward, layerProperties.radius, thickness); |
| 0 | 299 | | } |
| | 300 | | #endif |
| | 301 | | } |
| | 302 | | } |