< 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:237
Coverable lines:237
Total lines:581
Line coverage:0% (0 of 237)
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%90900%
ApplyConfig()0%30500%
GetTimeFromTheServer(...)0%2100%
SelectSkyboxConfiguration()0%72800%
Configuration_OnTimelineEvent(...)0%6200%
Update()0%90900%
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
 47        // Report to kernel
 048        private ITimeReporter timeReporter { get; set; } = new TimeReporter();
 49
 050        public SkyboxController()
 51        {
 052            i = this;
 53
 54            // Find and delete test directional light obj if any
 055            Light[] testDirectionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.name == "The Sun_Temp").To
 056            for (int i = 0; i < testDirectionalLight.Length; i++)
 57            {
 058                GameObject.DestroyImmediate(testDirectionalLight[i].gameObject);
 59            }
 60
 61            // Get or Create new Directional Light Object
 062            directionalLight = GameObject.FindObjectsOfType<Light>().Where(s => s.type == LightType.Directional).FirstOr
 63
 064            if (directionalLight == null)
 65            {
 066                GameObject temp = new GameObject("The Sun");
 67                // Add the light component
 068                directionalLight = temp.AddComponent<Light>();
 069                directionalLight.type = LightType.Directional;
 70            }
 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.configToLoad.Set(current.proceduralSkyboxConfig.configToLoad);
 0205            DataStore.i.skyboxConfig.lifecycleDuration.Set(current.proceduralSkyboxConfig.lifecycleDuration);
 0206            DataStore.i.skyboxConfig.jumpToTime.Set(current.proceduralSkyboxConfig.fixedTime);
 0207            DataStore.i.skyboxConfig.updateReflectionTime.Set(current.proceduralSkyboxConfig.updateReflectionTime);
 0208            DataStore.i.skyboxConfig.disableReflection.Set(current.proceduralSkyboxConfig.disableReflection);
 209
 210            // Call update on skybox config which will call Update config in this class.
 0211            DataStore.i.skyboxConfig.objectUpdated.Set(true, true);
 0212        }
 213
 214        /// <summary>
 215        /// Called whenever any change in skyboxConfig is observed
 216        /// </summary>
 217        /// <param name="current"></param>
 218        /// <param name="previous"></param>
 219        public void UpdateConfig(bool current = true, bool previous = false)
 220        {
 0221            if (overrideByEditor)
 222            {
 0223                return;
 224            }
 225
 226            // Reset Object Update value without notifying
 0227            DataStore.i.skyboxConfig.objectUpdated.Set(false, false);
 228
 0229            if (!DataStore.i.skyboxConfig.useDynamicSkybox.Get())
 230            {
 0231                return;
 232            }
 233
 0234            if (loadedConfig != DataStore.i.skyboxConfig.configToLoad.Get())
 235            {
 236                // Apply configuration
 0237                overrideDefaultSkybox = true;
 0238                overrideSkyboxID = DataStore.i.skyboxConfig.configToLoad.Get();
 239            }
 240
 241            // Apply time
 0242            lifecycleDuration = DataStore.i.skyboxConfig.lifecycleDuration.Get();
 243
 0244            ApplyConfig();
 245
 246            // if Paused
 0247            if (DataStore.i.skyboxConfig.jumpToTime.Get() >= 0)
 248            {
 0249                PauseTime(true, DataStore.i.skyboxConfig.jumpToTime.Get());
 0250            }
 251            else
 252            {
 0253                ResumeTime();
 254            }
 255
 256            // Update reflection time
 0257            if (DataStore.i.skyboxConfig.disableReflection.Get())
 258            {
 0259                if (skyboxProbe != null)
 260                {
 0261                    skyboxProbe.gameObject.SetActive(false);
 262                }
 263
 0264                RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox;
 0265                RenderSettings.customReflection = null;
 0266            }
 0267            else if (runtimeReflectionObj != null)
 268            {
 269                // If reflection update time is -1 then calculate time based on the cycle time, else assign same
 0270                if (DataStore.i.skyboxConfig.updateReflectionTime.Get() >= 0)
 271                {
 0272                    reflectionUpdateTime = DataStore.i.skyboxConfig.updateReflectionTime.Get();
 0273                }
 274                else
 275                {
 276                    // Evaluate with the cycle time
 0277                    reflectionUpdateTime = 1;               // Default for an hour is 1 min
 278                    // get cycle time in hours
 0279                    reflectionUpdateTime = (DataStore.i.skyboxConfig.lifecycleDuration.Get() / 60);
 280                }
 0281                runtimeReflectionObj.updateAfter = Mathf.Clamp(reflectionUpdateTime * 60, 5, 86400);
 282            }
 0283        }
 284
 285        /// <summary>
 286        /// Apply changed configuration
 287        /// </summary>
 288        bool ApplyConfig()
 289        {
 0290            if (overrideByEditor)
 291            {
 0292                return false;
 293            }
 294
 0295            if (!SelectSkyboxConfiguration())
 296            {
 0297                return false;
 298            }
 299
 0300            if (!configuration.useDirectionalLight)
 301            {
 0302                directionalLight.gameObject.SetActive(false);
 303            }
 304
 305            // Calculate time factor
 0306            if (lifecycleDuration <= 0)
 307            {
 0308                lifecycleDuration = 0.01f;
 309            }
 310
 311            // Convert minutes in seconds and then normalize with cycle time
 0312            timeNormalizationFactor = lifecycleDuration * 60 / cycleTime;
 0313            timeReporter.Configure(timeNormalizationFactor, cycleTime);
 314
 0315            GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime());
 0316            return true;
 317        }
 318
 319        void GetTimeFromTheServer(DateTime serverTime)
 320        {
 0321            DateTime serverTimeNoTicks = new DateTime(serverTime.Year, serverTime.Month, serverTime.Day, serverTime.Hour
 0322            long elapsedTicks = serverTime.Ticks - serverTimeNoTicks.Ticks;
 323
 0324            float miliseconds = serverTime.Millisecond + (elapsedTicks / 10000);
 325
 326            // Convert miliseconds to seconds
 0327            float seconds = serverTime.Second + (miliseconds / 1000);
 328
 329            // Convert seconds to minutes
 0330            float minutes = serverTime.Minute + (seconds / 60);
 331
 332            // Convert minutes to hour (in float format)
 0333            float totalTimeInMins = serverTime.Hour * 60 + minutes;
 334
 0335            float timeInCycle = (totalTimeInMins / lifecycleDuration) + 1;
 0336            float percentageSkyboxtime = timeInCycle - (int)timeInCycle;
 337
 0338            timeOfTheDay = percentageSkyboxtime * cycleTime;
 0339        }
 340
 341        /// <summary>
 342        /// Select Configuration to load.
 343        /// </summary>
 344        private bool SelectSkyboxConfiguration()
 345        {
 0346            bool tempConfigLoaded = true;
 347
 0348            string configToLoad = loadedConfig;
 0349            if (string.IsNullOrEmpty(loadedConfig))
 350            {
 0351                configToLoad = DEFAULT_SKYBOX_ID;
 352            }
 353
 354
 0355            if (overrideDefaultSkybox)
 356            {
 0357                configToLoad = overrideSkyboxID;
 0358                overrideDefaultSkybox = false;
 359            }
 360
 361            // config already loaded, return
 0362            if (configToLoad.Equals(loadedConfig))
 363            {
 0364                return tempConfigLoaded;
 365            }
 366
 0367            SkyboxConfiguration newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + config
 368
 0369            if (newConfiguration == null)
 370            {
 371#if UNITY_EDITOR || DEVELOPMENT_BUILD
 0372                Debug.LogError(configToLoad + " configuration not found in Resources. Trying to load Default config: " +
 373#endif
 374                // Try to load default config
 0375                configToLoad = DEFAULT_SKYBOX_ID;
 0376                newConfiguration = Resources.Load<SkyboxConfiguration>("Skybox Configurations/" + configToLoad);
 377
 0378                if (newConfiguration == null)
 379                {
 380#if UNITY_EDITOR || DEVELOPMENT_BUILD
 0381                    Debug.LogError("Default configuration not found in Resources. Shifting to old skybox. (Default path 
 382#endif
 0383                    tempConfigLoaded = false;
 0384                    return tempConfigLoaded;
 385                }
 386            }
 387
 388            // Register to timelineEvents
 0389            if (configuration != null)
 390            {
 0391                configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 392            }
 0393            newConfiguration.OnTimelineEvent += Configuration_OnTimelineEvent;
 0394            configuration = newConfiguration;
 395
 396            // Apply material as per number of Slots.
 0397            MaterialReferenceContainer.Mat_Layer matLayer = MaterialReferenceContainer.i.GetMat_LayerForLayers(5);
 0398            if (matLayer == null)
 399            {
 0400                matLayer = MaterialReferenceContainer.i.materials[0];
 401            }
 402
 0403            configuration.ResetMaterial(matLayer.material, matLayer.numberOfSlots);
 0404            selectedMat = matLayer.material;
 0405            slotCount = matLayer.numberOfSlots;
 406
 0407            RenderSettings.skybox = selectedMat;
 408
 409            // Update loaded config
 0410            loadedConfig = configToLoad;
 411
 0412            return tempConfigLoaded;
 413        }
 414
 0415        private void Configuration_OnTimelineEvent(string tag, bool enable, bool trigger) { OnTimelineEvent?.Invoke(tag,
 416
 417        // Update is called once per frame
 418        public void Update()
 419        {
 0420            if (!DataStore.i.skyboxConfig.disableReflection.Get() && skyboxProbe != null && !probeParented)
 421            {
 0422                AssignCameraInstancetoProbe();
 423            }
 424
 0425            if (configuration == null || isPaused)
 426            {
 0427                return;
 428            }
 429
 430            // Control is in editor tool
 0431            if (overrideByEditor)
 432            {
 0433                return;
 434            }
 435
 0436            timeOfTheDay += Time.deltaTime / timeNormalizationFactor;
 437
 0438            syncCounter++;
 439
 0440            if (syncCounter >= syncAfterCount)
 441            {
 0442                GetTimeFromTheServer(DataStore.i.worldTimer.GetCurrentTime());
 0443                syncCounter = 0;
 444            }
 445
 0446            timeOfTheDay = Mathf.Clamp(timeOfTheDay, 0.01f, cycleTime);
 0447            DataStore.i.skyboxConfig.currentVirtualTime.Set(timeOfTheDay);
 0448            timeReporter.ReportTime(timeOfTheDay);
 449
 0450            float normalizedDayTime = GetNormalizedDayTime();
 0451            configuration.ApplyOnMaterial(selectedMat, timeOfTheDay, normalizedDayTime, slotCount, directionalLight, cyc
 0452            ApplyAvatarColor(normalizedDayTime);
 453
 454            // Cycle resets
 0455            if (timeOfTheDay >= cycleTime)
 456            {
 0457                timeOfTheDay = 0.01f;
 0458                configuration.CycleResets();
 459            }
 460
 0461        }
 462
 463        public void Dispose()
 464        {
 465            // set skyboxConfig to false
 0466            DataStore.i.skyboxConfig.objectUpdated.OnChange -= UpdateConfig;
 467
 0468            DataStore.i.worldTimer.OnTimeChanged -= GetTimeFromTheServer;
 0469            configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 0470            KernelConfig.i.OnChange -= KernelConfig_OnChange;
 0471            DCL.Environment.i.platform.updateEventHandler.RemoveListener(IUpdateEventHandler.EventType.Update, Update);
 0472            DataStore.i.skyboxConfig.useDynamicSkybox.OnChange -= UseDynamicSkybox_OnChange;
 0473            DataStore.i.skyboxConfig.fixedTime.OnChange -= FixedTime_OnChange;
 0474            DataStore.i.skyboxConfig.reflectionResolution.OnChange -= ReflectionResolution_OnChange;
 475
 0476            timeReporter.Dispose();
 0477        }
 478
 479        public void PauseTime(bool overrideTime = false, float newTime = 0)
 480        {
 0481            isPaused = true;
 0482            if (overrideTime)
 483            {
 0484                timeOfTheDay = Mathf.Clamp(newTime, 0, 24);
 0485                configuration.ApplyOnMaterial(selectedMat, (float)timeOfTheDay, GetNormalizedDayTime(), slotCount, direc
 0486                ApplyAvatarColor(GetNormalizedDayTime());
 487            }
 0488            timeReporter.ReportTime(timeOfTheDay);
 0489        }
 490
 491        public void ResumeTime(bool overrideTime = false, float newTime = 0)
 492        {
 0493            isPaused = false;
 0494            if (overrideTime)
 495            {
 0496                timeOfTheDay = newTime;
 497            }
 0498        }
 499
 0500        public bool IsPaused() { return isPaused; }
 501
 502        private float GetNormalizedDayTime()
 503        {
 0504            double tTime = 0;
 505
 0506            tTime = timeOfTheDay / cycleTime;
 507
 0508            tTime = ClampDouble(tTime, 0, 1);
 509
 0510            return (float)tTime;
 511        }
 512
 0513        public SkyboxConfiguration GetCurrentConfiguration() { return configuration; }
 514
 0515        public float GetCurrentTimeOfTheDay() { return (float)timeOfTheDay; }
 516
 517        public bool SetOverrideController(bool editorOveride)
 518        {
 0519            overrideByEditor = editorOveride;
 0520            return overrideByEditor;
 521        }
 522
 523        // Whenever Skybox editor closed at runtime control returns back to controller with the values in the editor
 524        public bool GetControlBackFromEditor(string currentConfig, float timeOfTheday, float lifecycleDuration, bool isP
 525        {
 0526            overrideByEditor = false;
 527
 0528            DataStore.i.skyboxConfig.configToLoad.Set(currentConfig);
 0529            DataStore.i.skyboxConfig.lifecycleDuration.Set(lifecycleDuration);
 530
 0531            if (isPaused)
 532            {
 0533                PauseTime(true, timeOfTheday);
 0534            }
 535            else
 536            {
 0537                ResumeTime(true, timeOfTheday);
 538            }
 539
 540            // Call update on skybox config which will call Update config in this class.
 0541            DataStore.i.skyboxConfig.objectUpdated.Set(true, true);
 542
 0543            return overrideByEditor;
 544        }
 545
 546        public void UpdateConfigurationTimelineEvent(SkyboxConfiguration newConfig)
 547        {
 0548            configuration.OnTimelineEvent -= Configuration_OnTimelineEvent;
 0549            newConfig.OnTimelineEvent += Configuration_OnTimelineEvent;
 0550        }
 551
 552        public void ApplyAvatarColor(float normalizedDayTime)
 553        {
 0554            if (DataStore.i.skyboxConfig.avatarMatProfile.Get() == AvatarMaterialProfile.InWorld)
 555            {
 0556                configuration.ApplyInWorldAvatarColor(normalizedDayTime, directionalLight.gameObject);
 0557            }
 558            else
 559            {
 0560                configuration.ApplyEditorAvatarColor();
 561            }
 0562        }
 563
 564        private double ClampDouble(double timeOfTheDay, double min, float max)
 565        {
 0566            double result = timeOfTheDay;
 0567            if (timeOfTheDay < min)
 568            {
 0569                result = min;
 570            }
 571
 0572            if (timeOfTheDay > max)
 573            {
 0574                result = max;
 575            }
 576
 0577            return result;
 578        }
 579
 580    }
 581}