< Summary

Class:DCL.Skybox.SkyboxController
Assembly:ProceduralSkybox
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/ToolProceduralSkybox/Scripts/SkyboxController.cs
Covered lines:0
Uncovered lines:243
Coverable lines:243
Total lines:595
Line coverage:0% (0 of 243)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SkyboxController()0%30500%
FixedTime_OnChange(...)0%12300%
UseDynamicSkybox_OnChange(...)0%12300%
GetOrCreateEnvironmentProbe()0%42600%
ReflectionResolution_OnChange(...)0%2100%
AssignCameraInstancetoProbe()0%12300%
KernelConfig_OnChange(...)0%6200%
UpdateConfig(...)0%1321100%
ApplyConfig()0%30500%
GetTimeFromTheServer(...)0%2100%
SelectSkyboxConfiguration()0%90900%
Configuration_OnTimelineEvent(...)0%6200%
Update()0%1101000%
Dispose()0%2100%
PauseTime(...)0%6200%
ResumeTime(...)0%6200%
IsPaused()0%2100%
GetNormalizedDayTime()0%2100%
GetCurrentConfiguration()0%2100%
GetCurrentTimeOfTheDay()0%2100%
SetOverrideController(...)0%2100%
GetControlBackFromEditor(...)0%6200%
UpdateConfigurationTimelineEvent(...)0%2100%
ApplyAvatarColor(...)0%6200%
ClampDouble(...)0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Rendering/ProceduralSkybox/ToolProceduralSkybox/Scripts/SkyboxController.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using UnityEngine;
 4using DCL.ServerTime;
 5
 6namespace 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
 017        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
 023        public float cycleTime = 24;
 024        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;
 040        private float reflectionUpdateTime = 1;                                 // In Mins
 41        private ReflectionProbeRuntime runtimeReflectionObj;
 42
 43        // Timer sync
 44        private int syncCounter = 0;
 045        private int syncAfterCount = 10;
 46
 047        public SkyboxController()
 48        {
 049            i = this;
 50
 51            // Find and delete test directional light obj if any
 052            Light[] testDirectionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.name == "The Sun_Temp").To
 053            for (int i = 0; i < testDirectionalLight.Length; i++)
 54            {
 055                GameObject.DestroyImmediate(testDirectionalLight[i].gameObject);
 56            }
 57
 58            // Get or Create new Directional Light Object
 059            directionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.type == LightType.Directional).FirstOr
 60
 061            if (directionalLight == null)
 62            {
 063                GameObject temp = new GameObject("The Sun");
 64                // Add the light component
 065                directionalLight = temp.AddComponent<Light>();
 066                directionalLight.type = LightType.Directional;
 67            }
 68
 069            CommonScriptableObjects.proceduralSkyboxDisabled.Set(false);
 070            CommonScriptableObjects.proceduralSkyboxEnabled.Set(true);
 71
 072            GetOrCreateEnvironmentProbe();
 73
 74            // Get current time from the server
 075            GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime());
 076            DataStore.i.worldTimer.OnTimeChanged += GetTimeFromTheServer;
 77
 78            // Update config whenever skybox config changed in data store. Can be used for both testing and runtime
 079            DataStore.i.skyboxConfig.objectUpdated.OnChange += UpdateConfig;
 80
 81            // Change as Kernel config is initialized or updated
 082            KernelConfig.i.EnsureConfigInitialized()
 83                        .Then(config =>
 84                        {
 085                            KernelConfig_OnChange(config, null);
 086                        });
 87
 088            KernelConfig.i.OnChange += KernelConfig_OnChange;
 89
 090            DCL.Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, Update);
 91
 92            // Register UI related events
 093            DataStore.i.skyboxConfig.useDynamicSkybox.OnChange += UseDynamicSkybox_OnChange;
 094            DataStore.i.skyboxConfig.fixedTime.OnChange += FixedTime_OnChange;
 095            DataStore.i.skyboxConfig.reflectionResolution.OnChange += ReflectionResolution_OnChange;
 096        }
 97
 98        private void FixedTime_OnChange(float current, float previous)
 99        {
 0100            if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get())
 101            {
 0102                PauseTime(true, current);
 103            }
 104
 0105            if (runtimeReflectionObj != null)
 106            {
 0107                runtimeReflectionObj.FixedSkyboxTimeChanged();
 108            }
 0109        }
 110
 111        private void UseDynamicSkybox_OnChange(bool current, bool previous)
 112        {
 0113            if (current)
 114            {
 115                // Get latest time from server
 0116                UpdateConfig();
 0117            }
 118            else
 119            {
 0120                PauseTime(true, DataStore.i.skyboxConfig.fixedTime.Get());
 121            }
 122
 0123            if (runtimeReflectionObj != null)
 124            {
 0125                runtimeReflectionObj.SkyboxModeChanged(current);
 126            }
 0127        }
 128
 129        private void GetOrCreateEnvironmentProbe()
 130        {
 131            // Get Reflection Probe Object
 0132            skyboxProbe = GameObject.FindObjectsOfType<ReflectionProbe>().Where(s => s.name == "SkyboxProbe").FirstOrDef
 133
 0134            if (DataStore.i.skyboxConfig.disableReflection.Get())
 135            {
 0136                if (skyboxProbe != null)
 137                {
 0138                    skyboxProbe.gameObject.SetActive(false);
 139                }
 140
 0141                RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox;
 0142                RenderSettings.customReflection = null;
 0143                Debug.Log("Procedural Skybox :: Reflection disabled from server: " + DataStore.i.skyboxConfig.disableRef
 0144                return;
 145            }
 146
 0147            if (skyboxProbe == null)
 148            {
 149                // Instantiate new probe from the resources
 0150                GameObject temp = Resources.Load<GameObject>("SkyboxReflectionProbe/SkyboxProbe");
 0151                GameObject probe = GameObject.Instantiate<GameObject>(temp);
 0152                probe.name = "SkyboxProbe";
 0153                skyboxProbe = probe.GetComponent<ReflectionProbe>();
 154
 155                // make probe a child of main camera
 0156                AssignCameraInstancetoProbe();
 157            }
 158
 159            // Update time in Reflection Probe
 0160            runtimeReflectionObj = skyboxProbe.GetComponent<ReflectionProbeRuntime>();
 0161            if (runtimeReflectionObj == null)
 162            {
 0163                runtimeReflectionObj = skyboxProbe.gameObject.AddComponent<ReflectionProbeRuntime>();
 164            }
 165
 166            // Update resolution
 0167            runtimeReflectionObj.UpdateResolution(DataStore.i.skyboxConfig.reflectionResolution.Get());
 168
 169            // Assign as seconds
 0170            runtimeReflectionObj.updateAfter = reflectionUpdateTime * 60;
 171
 0172            RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Custom;
 0173            RenderSettings.customReflection = null;
 0174        }
 175
 0176        private void ReflectionResolution_OnChange(int current, int previous) { runtimeReflectionObj.UpdateResolution(cu
 177
 178        private void AssignCameraInstancetoProbe()
 179        {
 0180            if (skyboxProbe == null)
 181            {
 182#if UNITY_EDITOR
 0183                Debug.LogError("Cannot parent the probe as probe is not instantiated");
 184#endif
 0185                return;
 186            }
 187
 188            // make probe a child of main camera
 0189            if (Camera.main != null)
 190            {
 0191                GameObject mainCam = Camera.main.gameObject;
 0192                runtimeReflectionObj.followTransform = mainCam.transform;
 0193                probeParented = true;
 194            }
 0195        }
 196
 197        private void KernelConfig_OnChange(KernelConfigModel current, KernelConfigModel previous)
 198        {
 0199            if (overrideByEditor)
 200            {
 0201                return;
 202            }
 203            // set skyboxConfig to true
 0204            DataStore.i.skyboxConfig.useProceduralSkybox.Set(true);
 0205            DataStore.i.skyboxConfig.configToLoad.Set(current.proceduralSkyboxConfig.configToLoad);
 0206            DataStore.i.skyboxConfig.lifecycleDuration.Set(current.proceduralSkyboxConfig.lifecycleDuration);
 0207            DataStore.i.skyboxConfig.jumpToTime.Set(current.proceduralSkyboxConfig.fixedTime);
 0208            DataStore.i.skyboxConfig.updateReflectionTime.Set(current.proceduralSkyboxConfig.updateReflectionTime);
 0209            DataStore.i.skyboxConfig.disableReflection.Set(current.proceduralSkyboxConfig.disableReflection);
 210
 211            // Call update on skybox config which will call Update config in this class.
 0212            DataStore.i.skyboxConfig.objectUpdated.Set(true, true);
 0213        }
 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        {
 0222            if (overrideByEditor)
 223            {
 0224                return;
 225            }
 226
 227            // Reset Object Update value without notifying
 0228            DataStore.i.skyboxConfig.objectUpdated.Set(false, false);
 229
 0230            if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get())
 231            {
 0232                return;
 233            }
 234
 0235            if (loadedConfig != DataStore.i.skyboxConfig.configToLoad.Get())
 236            {
 237                // Apply configuration
 0238                overrideDefaultSkybox = true;
 0239                overrideSkyboxID = DataStore.i.skyboxConfig.configToLoad.Get();
 240            }
 241
 242            // Apply time
 0243            lifecycleDuration = DataStore.i.skyboxConfig.lifecycleDuration.Get();
 244
 0245            if (DataStore.i.skyboxConfig.useProceduralSkybox.Get())
 246            {
 0247                if (!ApplyConfig())
 248                {
 0249                    RenderProfileManifest.i.currentProfile.Apply();
 250                }
 0251            }
 252            else
 253            {
 0254                RenderProfileManifest.i.currentProfile.Apply();
 255            }
 256
 257            // if Paused
 0258            if (DataStore.i.skyboxConfig.jumpToTime.Get() >= 0)
 259            {
 0260                PauseTime(true, DataStore.i.skyboxConfig.jumpToTime.Get());
 0261            }
 262            else
 263            {
 0264                ResumeTime();
 265            }
 266
 267            // Update reflection time
 0268            if (DataStore.i.skyboxConfig.disableReflection.Get())
 269            {
 0270                if (skyboxProbe != null)
 271                {
 0272                    skyboxProbe.gameObject.SetActive(false);
 273                }
 274
 0275                RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox;
 0276                RenderSettings.customReflection = null;
 0277            }
 0278            else if (runtimeReflectionObj != null)
 279            {
 280                // If reflection update time is -1 then calculate time based on the cycle time, else assign same
 0281                if (DataStore.i.skyboxConfig.updateReflectionTime.Get() >= 0)
 282                {
 0283                    reflectionUpdateTime = DataStore.i.skyboxConfig.updateReflectionTime.Get();
 0284                }
 285                else
 286                {
 287                    // Evaluate with the cycle time
 0288                    reflectionUpdateTime = 1;               // Default for an hour is 1 min
 289                    // get cycle time in hours
 0290                    reflectionUpdateTime = (DataStore.i.skyboxConfig.lifecycleDuration.Get() / 60);
 291                }
 0292                runtimeReflectionObj.updateAfter = Mathf.Clamp(reflectionUpdateTime * 60, 5, 86400);
 293            }
 0294        }
 295
 296        /// <summary>
 297        /// Apply changed configuration
 298        /// </summary>
 299        bool ApplyConfig()
 300        {
 0301            if (overrideByEditor)
 302            {
 0303                return false;
 304            }
 305
 0306            if (!SelectSkyboxConfiguration())
 307            {
 0308                return false;
 309            }
 310
 0311            if (!configuration.useDirectionalLight)
 312            {
 0313                directionalLight.gameObject.SetActive(false);
 314            }
 315
 316            // Calculate time factor
 0317            if (lifecycleDuration <= 0)
 318            {
 0319                lifecycleDuration = 0.01f;
 320            }
 321
 322            // Convert minutes in seconds and then normalize with cycle time
 0323            timeNormalizationFactor = lifecycleDuration * 60 / cycleTime;
 324
 0325            GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime());
 0326            return true;
 327        }
 328
 329        void GetTimeFromTheServer(DateTime serverTime)
 330        {
 0331            DateTime serverTimeNoTicks = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, serverTime.Hour
 0332            long elapsedTicks = serverTime.Ticks - serverTimeNoTicks.Ticks;
 333
 0334            float miliseconds = serverTime.Millisecond + (elapsedTicks / 10000);
 335
 336            // Convert miliseconds to seconds
 0337            float seconds = serverTime.Second + (miliseconds / 1000);
 338
 339            // Convert seconds to minutes
 0340            float minutes = serverTime.Minute + (seconds / 60);
 341
 342            // Convert minutes to hour (in float format)
 0343            float totalTimeInMins = serverTime.Hour * 60 + minutes;
 344
 0345            float timeInCycle = (totalTimeInMins / lifecycleDuration) + 1;
 0346            float percentageSkyboxtime = timeInCycle - (int)timeInCycle;
 347
 0348            timeOfTheDay = percentageSkyboxtime * cycleTime;
 0349        }
 350
 351        /// <summary>
 352        /// Select Configuration to load.
 353        /// </summary>
 354        private bool SelectSkyboxConfiguration()
 355        {
 0356            bool tempConfigLoaded = true;
 357
 0358            string configToLoad = loadedConfig;
 0359            if (string.IsNullOrEmpty(loadedConfig))
 360            {
 0361                configToLoad = DEFAULT_SKYBOX_ID;
 362            }
 363
 364
 0365            if (overrideDefaultSkybox)
 366            {
 0367                configToLoad = overrideSkyboxID;
 0368                overrideDefaultSkybox = false;
 369            }
 370
 371            // config already loaded, return
 0372            if (configToLoad.Equals(loadedConfig))
 373            {
 0374                return tempConfigLoaded;
 375            }
 376
 0377            SkyboxConfiguration newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + config
 378
 0379            if (newConfiguration == null)
 380            {
 381#if UNITY_EDITOR || DEVELOPMENT_BUILD
 0382                Debug.LogError(configToLoad + " configuration not found in Resources. Trying to load Default config: " +
 383#endif
 384                // Try to load default config
 0385                configToLoad = DEFAULT_SKYBOX_ID;
 0386                newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + configToLoad);
 387
 0388                if (newConfiguration == null)
 389                {
 390#if UNITY_EDITOR || DEVELOPMENT_BUILD
 0391                    Debug.LogError("Default configuration not found in Resources. Shifting to old skybox. (Default path 
 392#endif
 0393                    tempConfigLoaded = false;
 0394                    return tempConfigLoaded;
 395                }
 396            }
 397
 398            // Register to timelineEvents
 0399            if (configuration != null)
 400            {
 0401                configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 402            }
 0403            newConfiguration.OnTimelineEvent += Configuration_OnTimelineEvent;
 0404            configuration = newConfiguration;
 405
 406            // Apply material as per number of Slots.
 0407            MaterialReferenceContainer.Mat_Layer matLayer = MaterialReferenceContainer.i.GetMat_LayerForLayers(5);
 0408            if (matLayer == null)
 409            {
 0410                matLayer = MaterialReferenceContainer.i.materials[0];
 411            }
 412
 0413            configuration.ResetMaterial(matLayer.material, matLayer.numberOfSlots);
 0414            selectedMat = matLayer.material;
 0415            slotCount = matLayer.numberOfSlots;
 416
 0417            if (DataStore.i.skyboxConfig.useProceduralSkybox.Get())
 418            {
 0419                RenderSettings.skybox = selectedMat;
 420            }
 421
 422            // Update loaded config
 0423            loadedConfig = configToLoad;
 424
 0425            return tempConfigLoaded;
 426        }
 427
 0428        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        {
 0433            if (!DataStore.i.skyboxConfig.disableReflection.Get() && skyboxProbe != null && !probeParented)
 434            {
 0435                AssignCameraInstancetoProbe();
 436            }
 437
 0438            if (configuration == null || isPaused || !DataStore.i.skyboxConfig.useProceduralSkybox.Get())
 439            {
 0440                return;
 441            }
 442
 443            // Control is in editor tool
 0444            if (overrideByEditor)
 445            {
 0446                return;
 447            }
 448
 0449            timeOfTheDay += Time.deltaTime / timeNormalizationFactor;
 450
 0451            syncCounter++;
 452
 0453            if (syncCounter >= syncAfterCount)
 454            {
 0455                GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime());
 0456                syncCounter = 0;
 457            }
 458
 0459            timeOfTheDay = Mathf.Clamp(timeOfTheDay, 0.01f, cycleTime);
 0460            DataStore.i.skyboxConfig.currentVirtualTime.Set(timeOfTheDay);
 461
 0462            float normalizedDayTime = GetNormalizedDayTime();
 0463            configuration.ApplyOnMaterial(selectedMat, timeOfTheDay, normalizedDayTime, slotCount, directionalLight, cyc
 0464            ApplyAvatarColor(normalizedDayTime);
 465
 466            // Cycle resets
 0467            if (timeOfTheDay >= cycleTime)
 468            {
 0469                timeOfTheDay = 0.01f;
 0470                configuration.CycleResets();
 471            }
 472
 0473        }
 474
 475        public void Dispose()
 476        {
 477
 0478            CommonScriptableObjects.proceduralSkyboxDisabled.Set(true);
 0479            CommonScriptableObjects.proceduralSkyboxEnabled.Set(false);
 480
 481            // set skyboxConfig to false
 0482            DataStore.i.skyboxConfig.useProceduralSkybox.Set(false);
 0483            DataStore.i.skyboxConfig.objectUpdated.OnChange -= UpdateConfig;
 484
 0485            DataStore.i.worldTimer.OnTimeChanged -= GetTimeFromTheServer;
 0486            configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 0487            KernelConfig.i.OnChange -= KernelConfig_OnChange;
 0488            DCL.Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update);
 0489            DataStore.i.skyboxConfig.useDynamicSkybox.OnChange -= UseDynamicSkybox_OnChange;
 0490            DataStore.i.skyboxConfig.fixedTime.OnChange -= FixedTime_OnChange;
 0491            DataStore.i.skyboxConfig.reflectionResolution.OnChange -= ReflectionResolution_OnChange;
 0492        }
 493
 494        public void PauseTime(bool overrideTime = false, float newTime = 0)
 495        {
 0496            isPaused = true;
 0497            if (overrideTime)
 498            {
 0499                timeOfTheDay = Mathf.Clamp(newTime, 0, 24);
 0500                configuration.ApplyOnMaterial(selectedMat, (float)timeOfTheDay, GetNormalizedDayTime(), slotCount, direc
 0501                ApplyAvatarColor(GetNormalizedDayTime());
 502            }
 0503        }
 504
 505        public void ResumeTime(bool overrideTime = false, float newTime = 0)
 506        {
 0507            isPaused = false;
 0508            if (overrideTime)
 509            {
 0510                timeOfTheDay = newTime;
 511            }
 0512        }
 513
 0514        public bool IsPaused() { return isPaused; }
 515
 516        private float GetNormalizedDayTime()
 517        {
 0518            double tTime = 0;
 519
 0520            tTime = timeOfTheDay / cycleTime;
 521
 0522            tTime = ClampDouble(tTime, 0, 1);
 523
 0524            return (float)tTime;
 525        }
 526
 0527        public SkyboxConfiguration GetCurrentConfiguration() { return configuration; }
 528
 0529        public float GetCurrentTimeOfTheDay() { return (float)timeOfTheDay; }
 530
 531        public bool SetOverrideController(bool editorOveride)
 532        {
 0533            overrideByEditor = editorOveride;
 0534            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        {
 0540            overrideByEditor = false;
 541
 0542            DataStore.i.skyboxConfig.configToLoad.Set(currentConfig);
 0543            DataStore.i.skyboxConfig.lifecycleDuration.Set(lifecycleDuration);
 544
 0545            if (isPaused)
 546            {
 0547                PauseTime(true, timeOfTheday);
 0548            }
 549            else
 550            {
 0551                ResumeTime(true, timeOfTheday);
 552            }
 553
 554            // Call update on skybox config which will call Update config in this class.
 0555            DataStore.i.skyboxConfig.objectUpdated.Set(true, true);
 556
 0557            return overrideByEditor;
 558        }
 559
 560        public void UpdateConfigurationTimelineEvent(SkyboxConfiguration newConfig)
 561        {
 0562            configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 0563            newConfig.OnTimelineEvent += Configuration_OnTimelineEvent;
 0564        }
 565
 566        public void ApplyAvatarColor(float normalizedDayTime)
 567        {
 0568            if (DataStore.i.skyboxConfig.avatarMatProfile.Get() == AvatarMaterialProfile.InWorld)
 569            {
 0570                configuration.ApplyInWorldAvatarColor(normalizedDayTime, directionalLight.gameObject);
 0571            }
 572            else
 573            {
 0574                configuration.ApplyEditorAvatarColor();
 575            }
 0576        }
 577
 578        private double ClampDouble(double timeOfTheDay, double min, float max)
 579        {
 0580            double result = timeOfTheDay;
 0581            if (timeOfTheDay < min)
 582            {
 0583                result = min;
 584            }
 585
 0586            if (timeOfTheDay > max)
 587            {
 0588                result = max;
 589            }
 590
 0591            return result;
 592        }
 593
 594    }
 595}