| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.ServerTime; |
| | 5 | |
|
| | 6 | | namespace DCL.Skybox |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// This class will handle runtime execution of skybox cycle. |
| | 10 | | /// Load and assign material to the Skybox. |
| | 11 | | /// This will mostly increment the time cycle and apply values from configuration to the material. |
| | 12 | | /// </summary> |
| | 13 | | public class SkyboxController : IPlugin |
| | 14 | | { |
| | 15 | | public event SkyboxConfiguration.TimelineEvents OnTimelineEvent; |
| | 16 | |
|
| 0 | 17 | | public static SkyboxController i { get; private set; } |
| | 18 | |
|
| | 19 | | public const string DEFAULT_SKYBOX_ID = "Generic_Skybox"; |
| | 20 | |
|
| | 21 | | public string loadedConfig; |
| | 22 | | //Time for one complete circle. In Hours. default 24 |
| 0 | 23 | | public float cycleTime = 24; |
| 0 | 24 | | public float lifecycleDuration = 2; |
| | 25 | |
|
| | 26 | | private float timeOfTheDay; // (Nishant.K) Time will be provided from outside, So rem |
| | 27 | | private Light directionalLight; |
| | 28 | | private SkyboxConfiguration configuration; |
| | 29 | | private Material selectedMat; |
| | 30 | | private bool overrideDefaultSkybox; |
| | 31 | | private string overrideSkyboxID; |
| | 32 | | private bool isPaused; |
| | 33 | | private float timeNormalizationFactor; |
| | 34 | | private int slotCount; |
| | 35 | | private bool overrideByEditor = false; |
| | 36 | |
|
| | 37 | | // Reflection probe// |
| | 38 | | private ReflectionProbe skyboxProbe; |
| | 39 | | private bool probeParented = false; |
| 0 | 40 | | private float reflectionUpdateTime = 1; // In Mins |
| | 41 | | private ReflectionProbeRuntime runtimeReflectionObj; |
| | 42 | |
|
| | 43 | | // Timer sync |
| | 44 | | private int syncCounter = 0; |
| 0 | 45 | | private int syncAfterCount = 10; |
| | 46 | |
|
| | 47 | | // Report to kernel |
| 0 | 48 | | private ITimeReporter timeReporter { get; set; } = new TimeReporter(); |
| | 49 | |
|
| 0 | 50 | | public SkyboxController() |
| | 51 | | { |
| 0 | 52 | | i = this; |
| | 53 | |
|
| | 54 | | // Find and delete test directional light obj if any |
| 0 | 55 | | Light[] testDirectionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.name == "The Sun_Temp").To |
| 0 | 56 | | for (int i = 0; i < testDirectionalLight.Length; i++) |
| | 57 | | { |
| 0 | 58 | | GameObject.DestroyImmediate(testDirectionalLight[i].gameObject); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | // Get or Create new Directional Light Object |
| 0 | 62 | | directionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.type == LightType.Directional).FirstOr |
| | 63 | |
|
| 0 | 64 | | if (directionalLight == null) |
| | 65 | | { |
| 0 | 66 | | GameObject temp = new GameObject("The Sun"); |
| | 67 | | // Add the light component |
| 0 | 68 | | directionalLight = temp.AddComponent<Light>(); |
| 0 | 69 | | directionalLight.type = LightType.Directional; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | GetOrCreateEnvironmentProbe(); |
| | 73 | |
|
| | 74 | | // Get current time from the server |
| 0 | 75 | | GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime()); |
| 0 | 76 | | DataStore.i.worldTimer.OnTimeChanged += GetTimeFromTheServer; |
| | 77 | |
|
| | 78 | | // Update config whenever skybox config changed in data store. Can be used for both testing and runtime |
| 0 | 79 | | DataStore.i.skyboxConfig.objectUpdated.OnChange += UpdateConfig; |
| | 80 | |
|
| | 81 | | // Change as Kernel config is initialized or updated |
| 0 | 82 | | KernelConfig.i.EnsureConfigInitialized() |
| | 83 | | .Then(config => |
| | 84 | | { |
| 0 | 85 | | KernelConfig_OnChange(config, null); |
| 0 | 86 | | }); |
| | 87 | |
|
| 0 | 88 | | KernelConfig.i.OnChange += KernelConfig_OnChange; |
| | 89 | |
|
| 0 | 90 | | DCL.Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update); |
| | 91 | |
|
| | 92 | | // Register UI related events |
| 0 | 93 | | DataStore.i.skyboxConfig.useDynamicSkybox.OnChange += UseDynamicSkybox_OnChange; |
| 0 | 94 | | DataStore.i.skyboxConfig.fixedTime.OnChange += FixedTime_OnChange; |
| 0 | 95 | | DataStore.i.skyboxConfig.reflectionResolution.OnChange += ReflectionResolution_OnChange; |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void FixedTime_OnChange(float current, float previous) |
| | 99 | | { |
| 0 | 100 | | if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get()) |
| | 101 | | { |
| 0 | 102 | | PauseTime(true, current); |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | if (runtimeReflectionObj != null) |
| | 106 | | { |
| 0 | 107 | | runtimeReflectionObj.FixedSkyboxTimeChanged(); |
| | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | private void UseDynamicSkybox_OnChange(bool current, bool previous) |
| | 112 | | { |
| 0 | 113 | | if (current) |
| | 114 | | { |
| | 115 | | // Get latest time from server |
| 0 | 116 | | UpdateConfig(); |
| 0 | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 0 | 120 | | PauseTime(true, DataStore.i.skyboxConfig.fixedTime.Get()); |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | if (runtimeReflectionObj != null) |
| | 124 | | { |
| 0 | 125 | | runtimeReflectionObj.SkyboxModeChanged(current); |
| | 126 | | } |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | private void GetOrCreateEnvironmentProbe() |
| | 130 | | { |
| | 131 | | // Get Reflection Probe Object |
| 0 | 132 | | skyboxProbe = GameObject.FindObjectsOfType<ReflectionProbe>().Where(s => s.name == "SkyboxProbe").FirstOrDef |
| | 133 | |
|
| 0 | 134 | | if (DataStore.i.skyboxConfig.disableReflection.Get()) |
| | 135 | | { |
| 0 | 136 | | if (skyboxProbe != null) |
| | 137 | | { |
| 0 | 138 | | skyboxProbe.gameObject.SetActive(false); |
| | 139 | | } |
| | 140 | |
|
| 0 | 141 | | RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox; |
| 0 | 142 | | RenderSettings.customReflection = null; |
| 0 | 143 | | Debug.Log("Procedural Skybox :: Reflection disabled from server: " + DataStore.i.skyboxConfig.disableRef |
| 0 | 144 | | return; |
| | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (skyboxProbe == null) |
| | 148 | | { |
| | 149 | | // Instantiate new probe from the resources |
| 0 | 150 | | GameObject temp = Resources.Load<GameObject>("SkyboxReflectionProbe/SkyboxProbe"); |
| 0 | 151 | | GameObject probe = GameObject.Instantiate<GameObject>(temp); |
| 0 | 152 | | probe.name = "SkyboxProbe"; |
| 0 | 153 | | skyboxProbe = probe.GetComponent<ReflectionProbe>(); |
| | 154 | |
|
| | 155 | | // make probe a child of main camera |
| 0 | 156 | | AssignCameraInstancetoProbe(); |
| | 157 | | } |
| | 158 | |
|
| | 159 | | // Update time in Reflection Probe |
| 0 | 160 | | runtimeReflectionObj = skyboxProbe.GetComponent<ReflectionProbeRuntime>(); |
| 0 | 161 | | if (runtimeReflectionObj == null) |
| | 162 | | { |
| 0 | 163 | | runtimeReflectionObj = skyboxProbe.gameObject.AddComponent<ReflectionProbeRuntime>(); |
| | 164 | | } |
| | 165 | |
|
| | 166 | | // Update resolution |
| 0 | 167 | | runtimeReflectionObj.UpdateResolution(DataStore.i.skyboxConfig.reflectionResolution.Get()); |
| | 168 | |
|
| | 169 | | // Assign as seconds |
| 0 | 170 | | runtimeReflectionObj.updateAfter = reflectionUpdateTime * 60; |
| | 171 | |
|
| 0 | 172 | | RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Custom; |
| 0 | 173 | | RenderSettings.customReflection = null; |
| 0 | 174 | | } |
| | 175 | |
|
| 0 | 176 | | private void ReflectionResolution_OnChange(int current, int previous) { runtimeReflectionObj.UpdateResolution(cu |
| | 177 | |
|
| | 178 | | private void AssignCameraInstancetoProbe() |
| | 179 | | { |
| 0 | 180 | | if (skyboxProbe == null) |
| | 181 | | { |
| | 182 | | #if UNITY_EDITOR |
| 0 | 183 | | Debug.LogError("Cannot parent the probe as probe is not instantiated"); |
| | 184 | | #endif |
| 0 | 185 | | return; |
| | 186 | | } |
| | 187 | |
|
| | 188 | | // make probe a child of main camera |
| 0 | 189 | | if (Camera.main != null) |
| | 190 | | { |
| 0 | 191 | | GameObject mainCam = Camera.main.gameObject; |
| 0 | 192 | | runtimeReflectionObj.followTransform = mainCam.transform; |
| 0 | 193 | | probeParented = true; |
| | 194 | | } |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void KernelConfig_OnChange(KernelConfigModel current, KernelConfigModel previous) |
| | 198 | | { |
| 0 | 199 | | if (overrideByEditor) |
| | 200 | | { |
| 0 | 201 | | return; |
| | 202 | | } |
| | 203 | | // set skyboxConfig to true |
| 0 | 204 | | DataStore.i.skyboxConfig.configToLoad.Set(current.proceduralSkyboxConfig.configToLoad); |
| 0 | 205 | | DataStore.i.skyboxConfig.lifecycleDuration.Set(current.proceduralSkyboxConfig.lifecycleDuration); |
| 0 | 206 | | DataStore.i.skyboxConfig.jumpToTime.Set(current.proceduralSkyboxConfig.fixedTime); |
| 0 | 207 | | DataStore.i.skyboxConfig.updateReflectionTime.Set(current.proceduralSkyboxConfig.updateReflectionTime); |
| 0 | 208 | | DataStore.i.skyboxConfig.disableReflection.Set(current.proceduralSkyboxConfig.disableReflection); |
| | 209 | |
|
| | 210 | | // Call update on skybox config which will call Update config in this class. |
| 0 | 211 | | DataStore.i.skyboxConfig.objectUpdated.Set(true, true); |
| 0 | 212 | | } |
| | 213 | |
|
| | 214 | | /// <summary> |
| | 215 | | /// Called whenever any change in skyboxConfig is observed |
| | 216 | | /// </summary> |
| | 217 | | /// <param name="current"></param> |
| | 218 | | /// <param name="previous"></param> |
| | 219 | | public void UpdateConfig(bool current = true, bool previous = false) |
| | 220 | | { |
| 0 | 221 | | if (overrideByEditor) |
| | 222 | | { |
| 0 | 223 | | return; |
| | 224 | | } |
| | 225 | |
|
| | 226 | | // Reset Object Update value without notifying |
| 0 | 227 | | DataStore.i.skyboxConfig.objectUpdated.Set(false, false); |
| | 228 | |
|
| 0 | 229 | | if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get()) |
| | 230 | | { |
| 0 | 231 | | return; |
| | 232 | | } |
| | 233 | |
|
| 0 | 234 | | if (loadedConfig != DataStore.i.skyboxConfig.configToLoad.Get()) |
| | 235 | | { |
| | 236 | | // Apply configuration |
| 0 | 237 | | overrideDefaultSkybox = true; |
| 0 | 238 | | overrideSkyboxID = DataStore.i.skyboxConfig.configToLoad.Get(); |
| | 239 | | } |
| | 240 | |
|
| | 241 | | // Apply time |
| 0 | 242 | | lifecycleDuration = DataStore.i.skyboxConfig.lifecycleDuration.Get(); |
| | 243 | |
|
| 0 | 244 | | ApplyConfig(); |
| | 245 | |
|
| | 246 | | // if Paused |
| 0 | 247 | | if (DataStore.i.skyboxConfig.jumpToTime.Get() >= 0) |
| | 248 | | { |
| 0 | 249 | | PauseTime(true, DataStore.i.skyboxConfig.jumpToTime.Get()); |
| 0 | 250 | | } |
| | 251 | | else |
| | 252 | | { |
| 0 | 253 | | ResumeTime(); |
| | 254 | | } |
| | 255 | |
|
| | 256 | | // Update reflection time |
| 0 | 257 | | if (DataStore.i.skyboxConfig.disableReflection.Get()) |
| | 258 | | { |
| 0 | 259 | | if (skyboxProbe != null) |
| | 260 | | { |
| 0 | 261 | | skyboxProbe.gameObject.SetActive(false); |
| | 262 | | } |
| | 263 | |
|
| 0 | 264 | | RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox; |
| 0 | 265 | | RenderSettings.customReflection = null; |
| 0 | 266 | | } |
| 0 | 267 | | else if (runtimeReflectionObj != null) |
| | 268 | | { |
| | 269 | | // If reflection update time is -1 then calculate time based on the cycle time, else assign same |
| 0 | 270 | | if (DataStore.i.skyboxConfig.updateReflectionTime.Get() >= 0) |
| | 271 | | { |
| 0 | 272 | | reflectionUpdateTime = DataStore.i.skyboxConfig.updateReflectionTime.Get(); |
| 0 | 273 | | } |
| | 274 | | else |
| | 275 | | { |
| | 276 | | // Evaluate with the cycle time |
| 0 | 277 | | reflectionUpdateTime = 1; // Default for an hour is 1 min |
| | 278 | | // get cycle time in hours |
| 0 | 279 | | reflectionUpdateTime = (DataStore.i.skyboxConfig.lifecycleDuration.Get() / 60); |
| | 280 | | } |
| 0 | 281 | | runtimeReflectionObj.updateAfter = Mathf.Clamp(reflectionUpdateTime * 60, 5, 86400); |
| | 282 | | } |
| 0 | 283 | | } |
| | 284 | |
|
| | 285 | | /// <summary> |
| | 286 | | /// Apply changed configuration |
| | 287 | | /// </summary> |
| | 288 | | bool ApplyConfig() |
| | 289 | | { |
| 0 | 290 | | if (overrideByEditor) |
| | 291 | | { |
| 0 | 292 | | return false; |
| | 293 | | } |
| | 294 | |
|
| 0 | 295 | | if (!SelectSkyboxConfiguration()) |
| | 296 | | { |
| 0 | 297 | | return false; |
| | 298 | | } |
| | 299 | |
|
| 0 | 300 | | if (!configuration.useDirectionalLight) |
| | 301 | | { |
| 0 | 302 | | directionalLight.gameObject.SetActive(false); |
| | 303 | | } |
| | 304 | |
|
| | 305 | | // Calculate time factor |
| 0 | 306 | | if (lifecycleDuration <= 0) |
| | 307 | | { |
| 0 | 308 | | lifecycleDuration = 0.01f; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | // Convert minutes in seconds and then normalize with cycle time |
| 0 | 312 | | timeNormalizationFactor = lifecycleDuration * 60 / cycleTime; |
| 0 | 313 | | timeReporter.Configure(timeNormalizationFactor, cycleTime); |
| | 314 | |
|
| 0 | 315 | | GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime()); |
| 0 | 316 | | return true; |
| | 317 | | } |
| | 318 | |
|
| | 319 | | void GetTimeFromTheServer(DateTime serverTime) |
| | 320 | | { |
| 0 | 321 | | DateTime serverTimeNoTicks = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, serverTime.Hour |
| 0 | 322 | | long elapsedTicks = serverTime.Ticks - serverTimeNoTicks.Ticks; |
| | 323 | |
|
| 0 | 324 | | float miliseconds = serverTime.Millisecond + (elapsedTicks / 10000); |
| | 325 | |
|
| | 326 | | // Convert miliseconds to seconds |
| 0 | 327 | | float seconds = serverTime.Second + (miliseconds / 1000); |
| | 328 | |
|
| | 329 | | // Convert seconds to minutes |
| 0 | 330 | | float minutes = serverTime.Minute + (seconds / 60); |
| | 331 | |
|
| | 332 | | // Convert minutes to hour (in float format) |
| 0 | 333 | | float totalTimeInMins = serverTime.Hour * 60 + minutes; |
| | 334 | |
|
| 0 | 335 | | float timeInCycle = (totalTimeInMins / lifecycleDuration) + 1; |
| 0 | 336 | | float percentageSkyboxtime = timeInCycle - (int)timeInCycle; |
| | 337 | |
|
| 0 | 338 | | timeOfTheDay = percentageSkyboxtime * cycleTime; |
| 0 | 339 | | } |
| | 340 | |
|
| | 341 | | /// <summary> |
| | 342 | | /// Select Configuration to load. |
| | 343 | | /// </summary> |
| | 344 | | private bool SelectSkyboxConfiguration() |
| | 345 | | { |
| 0 | 346 | | bool tempConfigLoaded = true; |
| | 347 | |
|
| 0 | 348 | | string configToLoad = loadedConfig; |
| 0 | 349 | | if (string.IsNullOrEmpty(loadedConfig)) |
| | 350 | | { |
| 0 | 351 | | configToLoad = DEFAULT_SKYBOX_ID; |
| | 352 | | } |
| | 353 | |
|
| | 354 | |
|
| 0 | 355 | | if (overrideDefaultSkybox) |
| | 356 | | { |
| 0 | 357 | | configToLoad = overrideSkyboxID; |
| 0 | 358 | | overrideDefaultSkybox = false; |
| | 359 | | } |
| | 360 | |
|
| | 361 | | // config already loaded, return |
| 0 | 362 | | if (configToLoad.Equals(loadedConfig)) |
| | 363 | | { |
| 0 | 364 | | return tempConfigLoaded; |
| | 365 | | } |
| | 366 | |
|
| 0 | 367 | | SkyboxConfiguration newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + config |
| | 368 | |
|
| 0 | 369 | | if (newConfiguration == null) |
| | 370 | | { |
| | 371 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 0 | 372 | | Debug.LogError(configToLoad + " configuration not found in Resources. Trying to load Default config: " + |
| | 373 | | #endif |
| | 374 | | // Try to load default config |
| 0 | 375 | | configToLoad = DEFAULT_SKYBOX_ID; |
| 0 | 376 | | newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + configToLoad); |
| | 377 | |
|
| 0 | 378 | | if (newConfiguration == null) |
| | 379 | | { |
| | 380 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 0 | 381 | | Debug.LogError("Default configuration not found in Resources. Shifting to old skybox. (Default path |
| | 382 | | #endif |
| 0 | 383 | | tempConfigLoaded = false; |
| 0 | 384 | | return tempConfigLoaded; |
| | 385 | | } |
| | 386 | | } |
| | 387 | |
|
| | 388 | | // Register to timelineEvents |
| 0 | 389 | | if (configuration != null) |
| | 390 | | { |
| 0 | 391 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| | 392 | | } |
| 0 | 393 | | newConfiguration.OnTimelineEvent += Configuration_OnTimelineEvent; |
| 0 | 394 | | configuration = newConfiguration; |
| | 395 | |
|
| | 396 | | // Apply material as per number of Slots. |
| 0 | 397 | | MaterialReferenceContainer.Mat_Layer matLayer = MaterialReferenceContainer.i.GetMat_LayerForLayers(5); |
| 0 | 398 | | if (matLayer == null) |
| | 399 | | { |
| 0 | 400 | | matLayer = MaterialReferenceContainer.i.materials[0]; |
| | 401 | | } |
| | 402 | |
|
| 0 | 403 | | configuration.ResetMaterial(matLayer.material, matLayer.numberOfSlots); |
| 0 | 404 | | selectedMat = matLayer.material; |
| 0 | 405 | | slotCount = matLayer.numberOfSlots; |
| | 406 | |
|
| 0 | 407 | | RenderSettings.skybox = selectedMat; |
| | 408 | |
|
| | 409 | | // Update loaded config |
| 0 | 410 | | loadedConfig = configToLoad; |
| | 411 | |
|
| 0 | 412 | | return tempConfigLoaded; |
| | 413 | | } |
| | 414 | |
|
| 0 | 415 | | private void Configuration_OnTimelineEvent(string tag, bool enable, bool trigger) { OnTimelineEvent?.Invoke(tag, |
| | 416 | |
|
| | 417 | | // Update is called once per frame |
| | 418 | | public void Update() |
| | 419 | | { |
| 0 | 420 | | if (!DataStore.i.skyboxConfig.disableReflection.Get() && skyboxProbe != null && !probeParented) |
| | 421 | | { |
| 0 | 422 | | AssignCameraInstancetoProbe(); |
| | 423 | | } |
| | 424 | |
|
| 0 | 425 | | if (configuration == null || isPaused) |
| | 426 | | { |
| 0 | 427 | | return; |
| | 428 | | } |
| | 429 | |
|
| | 430 | | // Control is in editor tool |
| 0 | 431 | | if (overrideByEditor) |
| | 432 | | { |
| 0 | 433 | | return; |
| | 434 | | } |
| | 435 | |
|
| 0 | 436 | | timeOfTheDay += Time.deltaTime / timeNormalizationFactor; |
| | 437 | |
|
| 0 | 438 | | syncCounter++; |
| | 439 | |
|
| 0 | 440 | | if (syncCounter >= syncAfterCount) |
| | 441 | | { |
| 0 | 442 | | GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime()); |
| 0 | 443 | | syncCounter = 0; |
| | 444 | | } |
| | 445 | |
|
| 0 | 446 | | timeOfTheDay = Mathf.Clamp(timeOfTheDay, 0.01f, cycleTime); |
| 0 | 447 | | DataStore.i.skyboxConfig.currentVirtualTime.Set(timeOfTheDay); |
| 0 | 448 | | timeReporter.ReportTime(timeOfTheDay); |
| | 449 | |
|
| 0 | 450 | | float normalizedDayTime = GetNormalizedDayTime(); |
| 0 | 451 | | configuration.ApplyOnMaterial(selectedMat, timeOfTheDay, normalizedDayTime, slotCount, directionalLight, cyc |
| 0 | 452 | | ApplyAvatarColor(normalizedDayTime); |
| | 453 | |
|
| | 454 | | // Cycle resets |
| 0 | 455 | | if (timeOfTheDay >= cycleTime) |
| | 456 | | { |
| 0 | 457 | | timeOfTheDay = 0.01f; |
| 0 | 458 | | configuration.CycleResets(); |
| | 459 | | } |
| | 460 | |
|
| 0 | 461 | | } |
| | 462 | |
|
| | 463 | | public void Dispose() |
| | 464 | | { |
| | 465 | | // set skyboxConfig to false |
| 0 | 466 | | DataStore.i.skyboxConfig.objectUpdated.OnChange -= UpdateConfig; |
| | 467 | |
|
| 0 | 468 | | DataStore.i.worldTimer.OnTimeChanged -= GetTimeFromTheServer; |
| 0 | 469 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| 0 | 470 | | KernelConfig.i.OnChange -= KernelConfig_OnChange; |
| 0 | 471 | | DCL.Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 0 | 472 | | DataStore.i.skyboxConfig.useDynamicSkybox.OnChange -= UseDynamicSkybox_OnChange; |
| 0 | 473 | | DataStore.i.skyboxConfig.fixedTime.OnChange -= FixedTime_OnChange; |
| 0 | 474 | | DataStore.i.skyboxConfig.reflectionResolution.OnChange -= ReflectionResolution_OnChange; |
| | 475 | |
|
| 0 | 476 | | timeReporter.Dispose(); |
| 0 | 477 | | } |
| | 478 | |
|
| | 479 | | public void PauseTime(bool overrideTime = false, float newTime = 0) |
| | 480 | | { |
| 0 | 481 | | isPaused = true; |
| 0 | 482 | | if (overrideTime) |
| | 483 | | { |
| 0 | 484 | | timeOfTheDay = Mathf.Clamp(newTime, 0, 24); |
| 0 | 485 | | configuration.ApplyOnMaterial(selectedMat, (float)timeOfTheDay, GetNormalizedDayTime(), slotCount, direc |
| 0 | 486 | | ApplyAvatarColor(GetNormalizedDayTime()); |
| | 487 | | } |
| 0 | 488 | | timeReporter.ReportTime(timeOfTheDay); |
| 0 | 489 | | } |
| | 490 | |
|
| | 491 | | public void ResumeTime(bool overrideTime = false, float newTime = 0) |
| | 492 | | { |
| 0 | 493 | | isPaused = false; |
| 0 | 494 | | if (overrideTime) |
| | 495 | | { |
| 0 | 496 | | timeOfTheDay = newTime; |
| | 497 | | } |
| 0 | 498 | | } |
| | 499 | |
|
| 0 | 500 | | public bool IsPaused() { return isPaused; } |
| | 501 | |
|
| | 502 | | private float GetNormalizedDayTime() |
| | 503 | | { |
| 0 | 504 | | double tTime = 0; |
| | 505 | |
|
| 0 | 506 | | tTime = timeOfTheDay / cycleTime; |
| | 507 | |
|
| 0 | 508 | | tTime = ClampDouble(tTime, 0, 1); |
| | 509 | |
|
| 0 | 510 | | return (float)tTime; |
| | 511 | | } |
| | 512 | |
|
| 0 | 513 | | public SkyboxConfiguration GetCurrentConfiguration() { return configuration; } |
| | 514 | |
|
| 0 | 515 | | public float GetCurrentTimeOfTheDay() { return (float)timeOfTheDay; } |
| | 516 | |
|
| | 517 | | public bool SetOverrideController(bool editorOveride) |
| | 518 | | { |
| 0 | 519 | | overrideByEditor = editorOveride; |
| 0 | 520 | | return overrideByEditor; |
| | 521 | | } |
| | 522 | |
|
| | 523 | | // Whenever Skybox editor closed at runtime control returns back to controller with the values in the editor |
| | 524 | | public bool GetControlBackFromEditor(string currentConfig, float timeOfTheday, float lifecycleDuration, bool isP |
| | 525 | | { |
| 0 | 526 | | overrideByEditor = false; |
| | 527 | |
|
| 0 | 528 | | DataStore.i.skyboxConfig.configToLoad.Set(currentConfig); |
| 0 | 529 | | DataStore.i.skyboxConfig.lifecycleDuration.Set(lifecycleDuration); |
| | 530 | |
|
| 0 | 531 | | if (isPaused) |
| | 532 | | { |
| 0 | 533 | | PauseTime(true, timeOfTheday); |
| 0 | 534 | | } |
| | 535 | | else |
| | 536 | | { |
| 0 | 537 | | ResumeTime(true, timeOfTheday); |
| | 538 | | } |
| | 539 | |
|
| | 540 | | // Call update on skybox config which will call Update config in this class. |
| 0 | 541 | | DataStore.i.skyboxConfig.objectUpdated.Set(true, true); |
| | 542 | |
|
| 0 | 543 | | return overrideByEditor; |
| | 544 | | } |
| | 545 | |
|
| | 546 | | public void UpdateConfigurationTimelineEvent(SkyboxConfiguration newConfig) |
| | 547 | | { |
| 0 | 548 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| 0 | 549 | | newConfig.OnTimelineEvent += Configuration_OnTimelineEvent; |
| 0 | 550 | | } |
| | 551 | |
|
| | 552 | | public void ApplyAvatarColor(float normalizedDayTime) |
| | 553 | | { |
| 0 | 554 | | if (DataStore.i.skyboxConfig.avatarMatProfile.Get() == AvatarMaterialProfile.InWorld) |
| | 555 | | { |
| 0 | 556 | | configuration.ApplyInWorldAvatarColor(normalizedDayTime, directionalLight.gameObject); |
| 0 | 557 | | } |
| | 558 | | else |
| | 559 | | { |
| 0 | 560 | | configuration.ApplyEditorAvatarColor(); |
| | 561 | | } |
| 0 | 562 | | } |
| | 563 | |
|
| | 564 | | private double ClampDouble(double timeOfTheDay, double min, float max) |
| | 565 | | { |
| 0 | 566 | | double result = timeOfTheDay; |
| 0 | 567 | | if (timeOfTheDay < min) |
| | 568 | | { |
| 0 | 569 | | result = min; |
| | 570 | | } |
| | 571 | |
|
| 0 | 572 | | if (timeOfTheDay > max) |
| | 573 | | { |
| 0 | 574 | | result = max; |
| | 575 | | } |
| | 576 | |
|
| 0 | 577 | | return result; |
| | 578 | | } |
| | 579 | |
|
| | 580 | | } |
| | 581 | | } |