| | 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 | |
|
| 0 | 47 | | public SkyboxController() |
| | 48 | | { |
| 0 | 49 | | i = this; |
| | 50 | |
|
| | 51 | | // Find and delete test directional light obj if any |
| 0 | 52 | | Light[] testDirectionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.name == "The Sun_Temp").To |
| 0 | 53 | | for (int i = 0; i < testDirectionalLight.Length; i++) |
| | 54 | | { |
| 0 | 55 | | GameObject.DestroyImmediate(testDirectionalLight[i].gameObject); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | // Get or Create new Directional Light Object |
| 0 | 59 | | directionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.type == LightType.Directional).FirstOr |
| | 60 | |
|
| 0 | 61 | | if (directionalLight == null) |
| | 62 | | { |
| 0 | 63 | | GameObject temp = new GameObject("The Sun"); |
| | 64 | | // Add the light component |
| 0 | 65 | | directionalLight = temp.AddComponent<Light>(); |
| 0 | 66 | | directionalLight.type = LightType.Directional; |
| | 67 | | } |
| | 68 | |
|
| 0 | 69 | | CommonScriptableObjects.proceduralSkyboxDisabled.Set(false); |
| 0 | 70 | | CommonScriptableObjects.proceduralSkyboxEnabled.Set(true); |
| | 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.useProceduralSkybox.Set(true); |
| 0 | 205 | | DataStore.i.skyboxConfig.configToLoad.Set(current.proceduralSkyboxConfig.configToLoad); |
| 0 | 206 | | DataStore.i.skyboxConfig.lifecycleDuration.Set(current.proceduralSkyboxConfig.lifecycleDuration); |
| 0 | 207 | | DataStore.i.skyboxConfig.jumpToTime.Set(current.proceduralSkyboxConfig.fixedTime); |
| 0 | 208 | | DataStore.i.skyboxConfig.updateReflectionTime.Set(current.proceduralSkyboxConfig.updateReflectionTime); |
| 0 | 209 | | DataStore.i.skyboxConfig.disableReflection.Set(current.proceduralSkyboxConfig.disableReflection); |
| | 210 | |
|
| | 211 | | // Call update on skybox config which will call Update config in this class. |
| 0 | 212 | | DataStore.i.skyboxConfig.objectUpdated.Set(true, true); |
| 0 | 213 | | } |
| | 214 | |
|
| | 215 | | /// <summary> |
| | 216 | | /// Called whenever any change in skyboxConfig is observed |
| | 217 | | /// </summary> |
| | 218 | | /// <param name="current"></param> |
| | 219 | | /// <param name="previous"></param> |
| | 220 | | public void UpdateConfig(bool current = true, bool previous = false) |
| | 221 | | { |
| 0 | 222 | | if (overrideByEditor) |
| | 223 | | { |
| 0 | 224 | | return; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | // Reset Object Update value without notifying |
| 0 | 228 | | DataStore.i.skyboxConfig.objectUpdated.Set(false, false); |
| | 229 | |
|
| 0 | 230 | | if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get()) |
| | 231 | | { |
| 0 | 232 | | return; |
| | 233 | | } |
| | 234 | |
|
| 0 | 235 | | if (loadedConfig != DataStore.i.skyboxConfig.configToLoad.Get()) |
| | 236 | | { |
| | 237 | | // Apply configuration |
| 0 | 238 | | overrideDefaultSkybox = true; |
| 0 | 239 | | overrideSkyboxID = DataStore.i.skyboxConfig.configToLoad.Get(); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | // Apply time |
| 0 | 243 | | lifecycleDuration = DataStore.i.skyboxConfig.lifecycleDuration.Get(); |
| | 244 | |
|
| 0 | 245 | | if (DataStore.i.skyboxConfig.useProceduralSkybox.Get()) |
| | 246 | | { |
| 0 | 247 | | if (!ApplyConfig()) |
| | 248 | | { |
| 0 | 249 | | RenderProfileManifest.i.currentProfile.Apply(); |
| | 250 | | } |
| 0 | 251 | | } |
| | 252 | | else |
| | 253 | | { |
| 0 | 254 | | RenderProfileManifest.i.currentProfile.Apply(); |
| | 255 | | } |
| | 256 | |
|
| | 257 | | // if Paused |
| 0 | 258 | | if (DataStore.i.skyboxConfig.jumpToTime.Get() >= 0) |
| | 259 | | { |
| 0 | 260 | | PauseTime(true, DataStore.i.skyboxConfig.jumpToTime.Get()); |
| 0 | 261 | | } |
| | 262 | | else |
| | 263 | | { |
| 0 | 264 | | ResumeTime(); |
| | 265 | | } |
| | 266 | |
|
| | 267 | | // Update reflection time |
| 0 | 268 | | if (DataStore.i.skyboxConfig.disableReflection.Get()) |
| | 269 | | { |
| 0 | 270 | | if (skyboxProbe != null) |
| | 271 | | { |
| 0 | 272 | | skyboxProbe.gameObject.SetActive(false); |
| | 273 | | } |
| | 274 | |
|
| 0 | 275 | | RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox; |
| 0 | 276 | | RenderSettings.customReflection = null; |
| 0 | 277 | | } |
| 0 | 278 | | else if (runtimeReflectionObj != null) |
| | 279 | | { |
| | 280 | | // If reflection update time is -1 then calculate time based on the cycle time, else assign same |
| 0 | 281 | | if (DataStore.i.skyboxConfig.updateReflectionTime.Get() >= 0) |
| | 282 | | { |
| 0 | 283 | | reflectionUpdateTime = DataStore.i.skyboxConfig.updateReflectionTime.Get(); |
| 0 | 284 | | } |
| | 285 | | else |
| | 286 | | { |
| | 287 | | // Evaluate with the cycle time |
| 0 | 288 | | reflectionUpdateTime = 1; // Default for an hour is 1 min |
| | 289 | | // get cycle time in hours |
| 0 | 290 | | reflectionUpdateTime = (DataStore.i.skyboxConfig.lifecycleDuration.Get() / 60); |
| | 291 | | } |
| 0 | 292 | | runtimeReflectionObj.updateAfter = Mathf.Clamp(reflectionUpdateTime * 60, 5, 86400); |
| | 293 | | } |
| 0 | 294 | | } |
| | 295 | |
|
| | 296 | | /// <summary> |
| | 297 | | /// Apply changed configuration |
| | 298 | | /// </summary> |
| | 299 | | bool ApplyConfig() |
| | 300 | | { |
| 0 | 301 | | if (overrideByEditor) |
| | 302 | | { |
| 0 | 303 | | return false; |
| | 304 | | } |
| | 305 | |
|
| 0 | 306 | | if (!SelectSkyboxConfiguration()) |
| | 307 | | { |
| 0 | 308 | | return false; |
| | 309 | | } |
| | 310 | |
|
| 0 | 311 | | if (!configuration.useDirectionalLight) |
| | 312 | | { |
| 0 | 313 | | directionalLight.gameObject.SetActive(false); |
| | 314 | | } |
| | 315 | |
|
| | 316 | | // Calculate time factor |
| 0 | 317 | | if (lifecycleDuration <= 0) |
| | 318 | | { |
| 0 | 319 | | lifecycleDuration = 0.01f; |
| | 320 | | } |
| | 321 | |
|
| | 322 | | // Convert minutes in seconds and then normalize with cycle time |
| 0 | 323 | | timeNormalizationFactor = lifecycleDuration * 60 / cycleTime; |
| | 324 | |
|
| 0 | 325 | | GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime()); |
| 0 | 326 | | return true; |
| | 327 | | } |
| | 328 | |
|
| | 329 | | void GetTimeFromTheServer(DateTime serverTime) |
| | 330 | | { |
| 0 | 331 | | DateTime serverTimeNoTicks = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, serverTime.Hour |
| 0 | 332 | | long elapsedTicks = serverTime.Ticks - serverTimeNoTicks.Ticks; |
| | 333 | |
|
| 0 | 334 | | float miliseconds = serverTime.Millisecond + (elapsedTicks / 10000); |
| | 335 | |
|
| | 336 | | // Convert miliseconds to seconds |
| 0 | 337 | | float seconds = serverTime.Second + (miliseconds / 1000); |
| | 338 | |
|
| | 339 | | // Convert seconds to minutes |
| 0 | 340 | | float minutes = serverTime.Minute + (seconds / 60); |
| | 341 | |
|
| | 342 | | // Convert minutes to hour (in float format) |
| 0 | 343 | | float totalTimeInMins = serverTime.Hour * 60 + minutes; |
| | 344 | |
|
| 0 | 345 | | float timeInCycle = (totalTimeInMins / lifecycleDuration) + 1; |
| 0 | 346 | | float percentageSkyboxtime = timeInCycle - (int)timeInCycle; |
| | 347 | |
|
| 0 | 348 | | timeOfTheDay = percentageSkyboxtime * cycleTime; |
| 0 | 349 | | } |
| | 350 | |
|
| | 351 | | /// <summary> |
| | 352 | | /// Select Configuration to load. |
| | 353 | | /// </summary> |
| | 354 | | private bool SelectSkyboxConfiguration() |
| | 355 | | { |
| 0 | 356 | | bool tempConfigLoaded = true; |
| | 357 | |
|
| 0 | 358 | | string configToLoad = loadedConfig; |
| 0 | 359 | | if (string.IsNullOrEmpty(loadedConfig)) |
| | 360 | | { |
| 0 | 361 | | configToLoad = DEFAULT_SKYBOX_ID; |
| | 362 | | } |
| | 363 | |
|
| | 364 | |
|
| 0 | 365 | | if (overrideDefaultSkybox) |
| | 366 | | { |
| 0 | 367 | | configToLoad = overrideSkyboxID; |
| 0 | 368 | | overrideDefaultSkybox = false; |
| | 369 | | } |
| | 370 | |
|
| | 371 | | // config already loaded, return |
| 0 | 372 | | if (configToLoad.Equals(loadedConfig)) |
| | 373 | | { |
| 0 | 374 | | return tempConfigLoaded; |
| | 375 | | } |
| | 376 | |
|
| 0 | 377 | | SkyboxConfiguration newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + config |
| | 378 | |
|
| 0 | 379 | | if (newConfiguration == null) |
| | 380 | | { |
| | 381 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 0 | 382 | | Debug.LogError(configToLoad + " configuration not found in Resources. Trying to load Default config: " + |
| | 383 | | #endif |
| | 384 | | // Try to load default config |
| 0 | 385 | | configToLoad = DEFAULT_SKYBOX_ID; |
| 0 | 386 | | newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + configToLoad); |
| | 387 | |
|
| 0 | 388 | | if (newConfiguration == null) |
| | 389 | | { |
| | 390 | | #if UNITY_EDITOR || DEVELOPMENT_BUILD |
| 0 | 391 | | Debug.LogError("Default configuration not found in Resources. Shifting to old skybox. (Default path |
| | 392 | | #endif |
| 0 | 393 | | tempConfigLoaded = false; |
| 0 | 394 | | return tempConfigLoaded; |
| | 395 | | } |
| | 396 | | } |
| | 397 | |
|
| | 398 | | // Register to timelineEvents |
| 0 | 399 | | if (configuration != null) |
| | 400 | | { |
| 0 | 401 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| | 402 | | } |
| 0 | 403 | | newConfiguration.OnTimelineEvent += Configuration_OnTimelineEvent; |
| 0 | 404 | | configuration = newConfiguration; |
| | 405 | |
|
| | 406 | | // Apply material as per number of Slots. |
| 0 | 407 | | MaterialReferenceContainer.Mat_Layer matLayer = MaterialReferenceContainer.i.GetMat_LayerForLayers(5); |
| 0 | 408 | | if (matLayer == null) |
| | 409 | | { |
| 0 | 410 | | matLayer = MaterialReferenceContainer.i.materials[0]; |
| | 411 | | } |
| | 412 | |
|
| 0 | 413 | | configuration.ResetMaterial(matLayer.material, matLayer.numberOfSlots); |
| 0 | 414 | | selectedMat = matLayer.material; |
| 0 | 415 | | slotCount = matLayer.numberOfSlots; |
| | 416 | |
|
| 0 | 417 | | if (DataStore.i.skyboxConfig.useProceduralSkybox.Get()) |
| | 418 | | { |
| 0 | 419 | | RenderSettings.skybox = selectedMat; |
| | 420 | | } |
| | 421 | |
|
| | 422 | | // Update loaded config |
| 0 | 423 | | loadedConfig = configToLoad; |
| | 424 | |
|
| 0 | 425 | | return tempConfigLoaded; |
| | 426 | | } |
| | 427 | |
|
| 0 | 428 | | private void Configuration_OnTimelineEvent(string tag, bool enable, bool trigger) { OnTimelineEvent?.Invoke(tag, |
| | 429 | |
|
| | 430 | | // Update is called once per frame |
| | 431 | | public void Update() |
| | 432 | | { |
| 0 | 433 | | if (!DataStore.i.skyboxConfig.disableReflection.Get() && skyboxProbe != null && !probeParented) |
| | 434 | | { |
| 0 | 435 | | AssignCameraInstancetoProbe(); |
| | 436 | | } |
| | 437 | |
|
| 0 | 438 | | if (configuration == null || isPaused || !DataStore.i.skyboxConfig.useProceduralSkybox.Get()) |
| | 439 | | { |
| 0 | 440 | | return; |
| | 441 | | } |
| | 442 | |
|
| | 443 | | // Control is in editor tool |
| 0 | 444 | | if (overrideByEditor) |
| | 445 | | { |
| 0 | 446 | | return; |
| | 447 | | } |
| | 448 | |
|
| 0 | 449 | | timeOfTheDay += Time.deltaTime / timeNormalizationFactor; |
| | 450 | |
|
| 0 | 451 | | syncCounter++; |
| | 452 | |
|
| 0 | 453 | | if (syncCounter >= syncAfterCount) |
| | 454 | | { |
| 0 | 455 | | GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime()); |
| 0 | 456 | | syncCounter = 0; |
| | 457 | | } |
| | 458 | |
|
| 0 | 459 | | timeOfTheDay = Mathf.Clamp(timeOfTheDay, 0.01f, cycleTime); |
| 0 | 460 | | DataStore.i.skyboxConfig.currentVirtualTime.Set(timeOfTheDay); |
| | 461 | |
|
| 0 | 462 | | float normalizedDayTime = GetNormalizedDayTime(); |
| 0 | 463 | | configuration.ApplyOnMaterial(selectedMat, timeOfTheDay, normalizedDayTime, slotCount, directionalLight, cyc |
| 0 | 464 | | ApplyAvatarColor(normalizedDayTime); |
| | 465 | |
|
| | 466 | | // Cycle resets |
| 0 | 467 | | if (timeOfTheDay >= cycleTime) |
| | 468 | | { |
| 0 | 469 | | timeOfTheDay = 0.01f; |
| 0 | 470 | | configuration.CycleResets(); |
| | 471 | | } |
| | 472 | |
|
| 0 | 473 | | } |
| | 474 | |
|
| | 475 | | public void Dispose() |
| | 476 | | { |
| | 477 | |
|
| 0 | 478 | | CommonScriptableObjects.proceduralSkyboxDisabled.Set(true); |
| 0 | 479 | | CommonScriptableObjects.proceduralSkyboxEnabled.Set(false); |
| | 480 | |
|
| | 481 | | // set skyboxConfig to false |
| 0 | 482 | | DataStore.i.skyboxConfig.useProceduralSkybox.Set(false); |
| 0 | 483 | | DataStore.i.skyboxConfig.objectUpdated.OnChange -= UpdateConfig; |
| | 484 | |
|
| 0 | 485 | | DataStore.i.worldTimer.OnTimeChanged -= GetTimeFromTheServer; |
| 0 | 486 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| 0 | 487 | | KernelConfig.i.OnChange -= KernelConfig_OnChange; |
| 0 | 488 | | DCL.Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update); |
| 0 | 489 | | DataStore.i.skyboxConfig.useDynamicSkybox.OnChange -= UseDynamicSkybox_OnChange; |
| 0 | 490 | | DataStore.i.skyboxConfig.fixedTime.OnChange -= FixedTime_OnChange; |
| 0 | 491 | | DataStore.i.skyboxConfig.reflectionResolution.OnChange -= ReflectionResolution_OnChange; |
| 0 | 492 | | } |
| | 493 | |
|
| | 494 | | public void PauseTime(bool overrideTime = false, float newTime = 0) |
| | 495 | | { |
| 0 | 496 | | isPaused = true; |
| 0 | 497 | | if (overrideTime) |
| | 498 | | { |
| 0 | 499 | | timeOfTheDay = Mathf.Clamp(newTime, 0, 24); |
| 0 | 500 | | configuration.ApplyOnMaterial(selectedMat, (float)timeOfTheDay, GetNormalizedDayTime(), slotCount, direc |
| 0 | 501 | | ApplyAvatarColor(GetNormalizedDayTime()); |
| | 502 | | } |
| 0 | 503 | | } |
| | 504 | |
|
| | 505 | | public void ResumeTime(bool overrideTime = false, float newTime = 0) |
| | 506 | | { |
| 0 | 507 | | isPaused = false; |
| 0 | 508 | | if (overrideTime) |
| | 509 | | { |
| 0 | 510 | | timeOfTheDay = newTime; |
| | 511 | | } |
| 0 | 512 | | } |
| | 513 | |
|
| 0 | 514 | | public bool IsPaused() { return isPaused; } |
| | 515 | |
|
| | 516 | | private float GetNormalizedDayTime() |
| | 517 | | { |
| 0 | 518 | | double tTime = 0; |
| | 519 | |
|
| 0 | 520 | | tTime = timeOfTheDay / cycleTime; |
| | 521 | |
|
| 0 | 522 | | tTime = ClampDouble(tTime, 0, 1); |
| | 523 | |
|
| 0 | 524 | | return (float)tTime; |
| | 525 | | } |
| | 526 | |
|
| 0 | 527 | | public SkyboxConfiguration GetCurrentConfiguration() { return configuration; } |
| | 528 | |
|
| 0 | 529 | | public float GetCurrentTimeOfTheDay() { return (float)timeOfTheDay; } |
| | 530 | |
|
| | 531 | | public bool SetOverrideController(bool editorOveride) |
| | 532 | | { |
| 0 | 533 | | overrideByEditor = editorOveride; |
| 0 | 534 | | return overrideByEditor; |
| | 535 | | } |
| | 536 | |
|
| | 537 | | // Whenever Skybox editor closed at runtime control returns back to controller with the values in the editor |
| | 538 | | public bool GetControlBackFromEditor(string currentConfig, float timeOfTheday, float lifecycleDuration, bool isP |
| | 539 | | { |
| 0 | 540 | | overrideByEditor = false; |
| | 541 | |
|
| 0 | 542 | | DataStore.i.skyboxConfig.configToLoad.Set(currentConfig); |
| 0 | 543 | | DataStore.i.skyboxConfig.lifecycleDuration.Set(lifecycleDuration); |
| | 544 | |
|
| 0 | 545 | | if (isPaused) |
| | 546 | | { |
| 0 | 547 | | PauseTime(true, timeOfTheday); |
| 0 | 548 | | } |
| | 549 | | else |
| | 550 | | { |
| 0 | 551 | | ResumeTime(true, timeOfTheday); |
| | 552 | | } |
| | 553 | |
|
| | 554 | | // Call update on skybox config which will call Update config in this class. |
| 0 | 555 | | DataStore.i.skyboxConfig.objectUpdated.Set(true, true); |
| | 556 | |
|
| 0 | 557 | | return overrideByEditor; |
| | 558 | | } |
| | 559 | |
|
| | 560 | | public void UpdateConfigurationTimelineEvent(SkyboxConfiguration newConfig) |
| | 561 | | { |
| 0 | 562 | | configuration.OnTimelineEvent -= Configuration_OnTimelineEvent; |
| 0 | 563 | | newConfig.OnTimelineEvent += Configuration_OnTimelineEvent; |
| 0 | 564 | | } |
| | 565 | |
|
| | 566 | | public void ApplyAvatarColor(float normalizedDayTime) |
| | 567 | | { |
| 0 | 568 | | if (DataStore.i.skyboxConfig.avatarMatProfile.Get() == AvatarMaterialProfile.InWorld) |
| | 569 | | { |
| 0 | 570 | | configuration.ApplyInWorldAvatarColor(normalizedDayTime, directionalLight.gameObject); |
| 0 | 571 | | } |
| | 572 | | else |
| | 573 | | { |
| 0 | 574 | | configuration.ApplyEditorAvatarColor(); |
| | 575 | | } |
| 0 | 576 | | } |
| | 577 | |
|
| | 578 | | private double ClampDouble(double timeOfTheDay, double min, float max) |
| | 579 | | { |
| 0 | 580 | | double result = timeOfTheDay; |
| 0 | 581 | | if (timeOfTheDay < min) |
| | 582 | | { |
| 0 | 583 | | result = min; |
| | 584 | | } |
| | 585 | |
|
| 0 | 586 | | if (timeOfTheDay > max) |
| | 587 | | { |
| 0 | 588 | | result = max; |
| | 589 | | } |
| | 590 | |
|
| 0 | 591 | | return result; |
| | 592 | | } |
| | 593 | |
|
| | 594 | | } |
| | 595 | | } |