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