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