< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SkyboxConfiguration()0%2100%
ApplyOnMaterial(...)0%30500%
ApplyInWorldAvatarColor(...)0%72800%
ApplyEditorAvatarColor()0%2100%
ApplyAllSlots(...)0%20400%
GetActiveLayer(...)0%90900%
CheckAndFireTimelineEvents(...)0%2401500%
CycleResets()0%20400%
ApplyDLIntensity(...)0%20400%
ApplyDLDirection(...)0%56700%
ResetMaterial(...)0%6200%
ResetSlot(...)0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using DCL.Helpers;
 6
 7namespace DCL.Skybox
 8{
 9    [CreateAssetMenu(fileName = "Skybox Configuration", menuName = "ScriptableObjects/SkyboxConfiguration", order = 1)]
 10    public class SkyboxConfiguration : ScriptableObject
 11    {
 12        public delegate void TimelineEvents(string tag, bool enable, bool trigger);
 13        public event TimelineEvents OnTimelineEvent;
 14
 15        // ID
 16        public string skyboxID;
 17
 18        // Background Color
 019        public Gradient skyColor = new Gradient();
 020        public Gradient horizonColor = new Gradient();
 021        public Gradient groundColor = new Gradient();
 022        public List<TransitioningFloat> horizonWidth = new List<TransitioningFloat>();
 023        public List<TransitioningFloat> horizonHeight = new List<TransitioningFloat>();
 24
 25        // Horizon Layer
 26        public Texture2D horizonMask;
 027        public Vector2 horizonMaskTiling = new Vector2(0, 0);
 028        public Vector2 horizonMaskOffset = new Vector2(0, 0);
 29
 30        // Horizon Plane Layer
 31        public Texture2D horizonPlaneTexture;
 032        public Vector2 horizonPlaneTiling = new Vector2(0, 0);
 033        public Vector2 horizonPlaneOffset = new Vector2(0, 0);
 034        public Gradient horizonPlaneColor = new Gradient();
 035        public List<TransitioningFloat> horizonPlaneHeight = new List<TransitioningFloat>();
 036        public Vector2 horizonPlaneSmoothRange = new Vector2(0, 0);
 37        public float horizonLightIntensity = 0;
 38
 39        // Ambient Color
 040        public bool ambientTrilight = true;
 041        public Gradient ambientSkyColor = new Gradient();
 042        public Gradient ambientEquatorColor = new Gradient();
 043        public Gradient ambientGroundColor = new Gradient();
 44
 45        // Avatar Color
 046        public bool useAvatarGradient = true;
 047        public Gradient avatarTintGradient = new Gradient();
 048        public Color avatarTintColor = Color.white;
 049        public bool useAvatarRealtimeDLDirection = true;
 050        public Vector3 avatarLightConstantDir = new Vector3(-18f, 144f, -72f);
 051        public bool useAvatarRealtimeLightColor = true;
 052        public Gradient avatarLightColorGradient = new Gradient();
 53
 54        // Avatar Editor Color
 055        public Color avatarEditorTintColor = Color.white;
 056        public Vector3 avatarEditorLightDir = new Vector3(-18f, 144f, -72f);
 057        public Color avatarEditorLightColor = Color.white;
 58
 59        // Fog Properties
 60        public bool useFog = false;
 061        public FogMode fogMode = FogMode.ExponentialSquared;
 062        public Gradient fogColor = new Gradient();
 063        public float fogDensity = 0.05f;
 64        public float fogStartDistance = 0;
 065        public float fogEndDistance = 300;
 66
 67        // DirectionalLight Properties
 068        public bool useDirectionalLight = true;
 069        public DirectionalLightAttributes directionalLightLayer = new DirectionalLightAttributes();
 70
 71        // Layers
 072        public List<TextureLayer> layers = new List<TextureLayer>();
 73
 74        // Timeline Tags
 075        public List<TimelineTagsDuration> timelineTags = new List<TimelineTagsDuration>();
 76
 77        // 3D Components
 078        public List<Config3DDome> additional3Dconfig = new List<Config3DDome>();
 079        public List<Config3DSatellite> satelliteLayers = new List<Config3DSatellite>();
 080        public List<Config3DPlanar> planarLayers = new List<Config3DPlanar>();
 81
 082        private float cycleTime = 24;
 83
 84        public void ApplyOnMaterial(Material selectedMat, float dayTime, float normalizedDayTime, int slotCount = 5, Lig
 85        {
 086            float percentage = normalizedDayTime * 100;
 87
 88            // General Values
 089            selectedMat.SetColor(SkyboxShaderUtils.LightTint, directionalLightLayer.tintColor.Evaluate(normalizedDayTime
 090            selectedMat.SetVector(SkyboxShaderUtils.LightDirection, directionalLightGO.transform.rotation.eulerAngles);
 91
 92            // Apply Base Values
 093            selectedMat.SetColor(SkyboxShaderUtils.SkyColor, skyColor.Evaluate(normalizedDayTime));
 094            selectedMat.SetColor(SkyboxShaderUtils.GroundColor, groundColor.Evaluate(normalizedDayTime));
 95
 96            // Apply Horizon Values
 097            selectedMat.SetColor(SkyboxShaderUtils.HorizonColor, horizonColor.Evaluate(normalizedDayTime));
 098            selectedMat.SetFloat(SkyboxShaderUtils.HorizonHeight, TransitioningValues.GetTransitionValue(horizonHeight, 
 099            selectedMat.SetFloat(SkyboxShaderUtils.HorizonWidth, TransitioningValues.GetTransitionValue(horizonWidth, pe
 0100            selectedMat.SetTexture(SkyboxShaderUtils.HorizonMask, horizonMask);
 0101            selectedMat.SetVector(SkyboxShaderUtils.HorizonMaskValues, new Vector4(horizonMaskTiling.x, horizonMaskTilin
 0102            selectedMat.SetTexture(SkyboxShaderUtils.HorizonPlane, horizonPlaneTexture);
 0103            selectedMat.SetVector(SkyboxShaderUtils.HorizonPlaneValues, new Vector4(horizonPlaneTiling.x, horizonPlaneTi
 0104            selectedMat.SetColor(SkyboxShaderUtils.HorizonPlaneColor, horizonPlaneColor.Evaluate(normalizedDayTime));
 0105            selectedMat.SetFloat(SkyboxShaderUtils.HorizonPlaneHeight, TransitioningValues.GetTransitionValue(horizonPla
 0106            selectedMat.SetVector(SkyboxShaderUtils.PlaneSmoothRange, horizonPlaneSmoothRange);
 0107            selectedMat.SetFloat(SkyboxShaderUtils.HorizonLightIntensity, horizonLightIntensity);
 108
 109
 110            // Apply Ambient colors to the rendering settings
 0111            if (ambientTrilight)
 112            {
 0113                RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Trilight;
 0114                RenderSettings.ambientSkyColor = ambientSkyColor.Evaluate(normalizedDayTime);
 0115                RenderSettings.ambientEquatorColor = ambientEquatorColor.Evaluate(normalizedDayTime);
 0116                RenderSettings.ambientGroundColor = ambientGroundColor.Evaluate(normalizedDayTime);
 0117            }
 118            else
 119            {
 0120                RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox;
 121            }
 122
 123            //Fog Values
 0124            RenderSettings.fog = useFog;
 0125            if (useFog)
 126            {
 0127                RenderSettings.fogColor = fogColor.Evaluate(normalizedDayTime);
 0128                RenderSettings.fogMode = fogMode;
 0129                switch (fogMode)
 130                {
 131                    case FogMode.Linear:
 0132                        RenderSettings.fogStartDistance = fogStartDistance;
 0133                        RenderSettings.fogEndDistance = fogEndDistance;
 0134                        break;
 135                    default:
 0136                        RenderSettings.fogDensity = fogDensity;
 137                        break;
 138                }
 139            }
 140
 141            //Directional List
 0142            if (useDirectionalLight)
 143            {
 144                // Apply Direction light color
 0145                directionalLightGO.color = directionalLightLayer.lightColor.Evaluate(normalizedDayTime);
 146
 147                // Apply direction
 0148                ApplyDLDirection(normalizedDayTime, directionalLightGO);
 149
 150                // Apply Intensity
 0151                ApplyDLIntensity(normalizedDayTime, directionalLightGO);
 0152            }
 153            else
 154            {
 0155                directionalLightGO.gameObject.SetActive(false);
 156            }
 157
 158            // Check and Fire timeline events
 0159            CheckAndFireTimelineEvents(dayTime);
 160
 0161            ApplyAllSlots(selectedMat, layers, dayTime, normalizedDayTime, slotCount, cycleTime);
 0162        }
 163
 164        public void ApplyInWorldAvatarColor(float normalizedDayTime, GameObject directionalLightGO)
 165        {
 0166            if (useAvatarGradient)
 167            {
 0168                Shader.SetGlobalColor(ShaderUtils.TintColor, avatarTintGradient.Evaluate(normalizedDayTime));
 0169            }
 170            else
 171            {
 0172                Shader.SetGlobalColor(ShaderUtils.TintColor, avatarTintColor);
 173            }
 174
 175            // Apply Avatar Light Direction
 0176            if (!useAvatarRealtimeDLDirection || directionalLightGO == null || !useDirectionalLight)
 177            {
 0178                Shader.SetGlobalVector(ShaderUtils.LightDir, avatarLightConstantDir / 180);
 0179            }
 180            else
 181            {
 0182                Vector3 tempDir = directionalLightGO.transform.rotation.eulerAngles;
 0183                Shader.SetGlobalVector(ShaderUtils.LightDir, tempDir / 180);
 184            }
 185
 186            // Apply Avatar Light Color
 0187            if (!useAvatarRealtimeLightColor || directionalLightGO == null || !useDirectionalLight)
 188            {
 0189                Shader.SetGlobalColor(ShaderUtils.LightColor, avatarLightColorGradient.Evaluate(normalizedDayTime));
 0190            }
 191            else
 192            {
 0193                Shader.SetGlobalColor(ShaderUtils.LightColor, directionalLightLayer.lightColor.Evaluate(normalizedDayTim
 194            }
 0195        }
 196
 197        public void ApplyEditorAvatarColor()
 198        {
 0199            Shader.SetGlobalColor(ShaderUtils.TintColor, avatarEditorTintColor);
 0200            Shader.SetGlobalVector(ShaderUtils.LightDir, avatarEditorLightDir / 180);
 0201            Shader.SetGlobalColor(ShaderUtils.LightColor, avatarEditorLightColor);
 0202        }
 203
 204        void ApplyAllSlots(Material selectedMat, List<TextureLayer> layers, float dayTime, float normalizedDayTime, int 
 205        {
 0206            for (int i = 0; i < slotCount; i++)
 207            {
 0208                TextureLayer layer = GetActiveLayer(layers, dayTime, i);
 209
 0210                if (layer == null || !layer.enabled)
 211                {
 0212                    ResetSlot(selectedMat, i);
 0213                    continue;
 214                }
 215
 0216                TextureLayerFunctionality.ApplyTextureLayer(selectedMat, dayTime, normalizedDayTime, i, layer, cycleTime
 217            }
 0218        }
 219
 220        public TextureLayer GetActiveLayer(List<TextureLayer> layers, float currentTime, int slotID)
 221        {
 0222            TextureLayer temp = null;
 223
 0224            for (int i = 0; i < layers.Count; i++)
 225            {
 0226                if (layers[i].slotID != slotID)
 227                {
 228                    continue;
 229                }
 230
 0231                float endTimeEdited = layers[i].timeSpan_End;
 0232                float dayTimeEdited = currentTime;
 233
 0234                if (layers[i].timeSpan_End < layers[i].timeSpan_start)
 235                {
 0236                    endTimeEdited = cycleTime + layers[i].timeSpan_End;
 0237                    if (currentTime < layers[i].timeSpan_start)
 238                    {
 0239                        dayTimeEdited = cycleTime + currentTime;
 240                    }
 241                }
 242
 0243                if (dayTimeEdited >= layers[i].timeSpan_start && dayTimeEdited <= endTimeEdited)
 244                {
 0245                    if (layers[i].enabled)
 246                    {
 0247                        if (temp == null)
 248                        {
 0249                            layers[i].renderType = LayerRenderType.Rendering;
 0250                            temp = layers[i];
 0251                        }
 252                        else
 253                        {
 0254                            temp.renderType = LayerRenderType.Conflict_Playing;
 0255                            layers[i].renderType = LayerRenderType.Conflict_NotPlaying;
 256                        }
 0257                    }
 258                    else
 259                    {
 0260                        layers[i].renderType = LayerRenderType.NotRendering;
 261                    }
 0262                }
 263                else
 264                {
 0265                    layers[i].renderType = LayerRenderType.NotRendering;
 266                }
 267
 268
 269            }
 0270            return temp;
 271        }
 272
 273        #region Timeline Events
 274
 275        /// <summary>
 276        /// Check if any event has to be fired
 277        /// TODO:: Can be optimised.
 278        /// </summary>
 279        /// <param name="dayTime">Current day time</param>
 280        private void CheckAndFireTimelineEvents(float dayTime)
 281        {
 0282            float endTimeEdited = 0;
 0283            float dayTimeEdited = dayTime;
 0284            for (int i = 0; i < timelineTags.Count; i++)
 285            {
 286                // If current event is a trigger event, fire trigger event. It's boolean will be disable when cycle rese
 0287                if (timelineTags[i].isTrigger)
 288                {
 0289                    if (dayTime > timelineTags[i].startTime && !timelineTags[i].startEventExecuted)
 290                    {
 0291                        OnTimelineEvent?.Invoke(timelineTags[i].tag, true, true);
 0292                        timelineTags[i].startEventExecuted = true;
 293                    }
 0294                    continue;
 295                }
 296
 297
 0298                endTimeEdited = timelineTags[i].endTime;
 0299                dayTimeEdited = dayTime;
 300
 301                // Change time if this is over a day scenario
 0302                if (timelineTags[i].endTime < timelineTags[i].startTime)
 303                {
 0304                    endTimeEdited = cycleTime + timelineTags[i].endTime;
 305
 0306                    if (dayTime < timelineTags[i].startTime)
 307                    {
 0308                        dayTimeEdited = cycleTime + dayTime;
 309                    }
 310                }
 311
 312                // If current time is between start and end time and start event is not executed, Fire start event
 0313                if (dayTimeEdited >= timelineTags[i].startTime && dayTimeEdited < endTimeEdited && !timelineTags[i].star
 314                {
 0315                    OnTimelineEvent?.Invoke(timelineTags[i].tag, true, false);
 0316                    timelineTags[i].startEventExecuted = true;
 317                }
 318
 319                // If current time is greater than end time and start event is executed, fire end event
 0320                if (dayTimeEdited >= endTimeEdited && timelineTags[i].startEventExecuted)
 321                {
 0322                    OnTimelineEvent?.Invoke(timelineTags[i].tag, false, false);
 0323                    timelineTags[i].startEventExecuted = false;
 324                }
 325
 326            }
 0327        }
 328
 329        public void CycleResets()
 330        {
 331            // Resets all timeline triggers and normal flow events
 0332            for (int i = 0; i < timelineTags.Count; i++)
 333            {
 0334                if (timelineTags[i].isTrigger)
 335                {
 0336                    timelineTags[i].startEventExecuted = false;
 0337                    continue;
 338                }
 339
 0340                if (timelineTags[i].endTime > timelineTags[i].startTime)
 341                {
 0342                    timelineTags[i].startEventExecuted = false;
 343                }
 344            }
 0345        }
 346
 347        #endregion
 348
 349        #region Directional Light
 350
 351        void ApplyDLIntensity(float normalizedDayTime, Light lightGO)
 352        {
 0353            if (lightGO == null)
 354            {
 0355                Debug.LogError("Directional Light option is on, with light reference");
 0356                return;
 357            }
 358
 0359            if (directionalLightLayer.intensity.Count == 0)
 360            {
 0361                Debug.Log("Directional Light option is on with no intensity data");
 0362                return;
 363            }
 364
 0365            if (directionalLightLayer.intensity.Count == 1)
 366            {
 0367                lightGO.intensity = directionalLightLayer.intensity[0].value;
 0368                return;
 369            }
 370
 371
 0372            lightGO.intensity = TransitioningValues.GetTransitionValue(directionalLightLayer.intensity, normalizedDayTim
 0373        }
 374
 375        void ApplyDLDirection(float normalizedDayTime, Light lightGO)
 376        {
 0377            if (lightGO == null)
 378            {
 0379                return;
 380            }
 381
 0382            if (directionalLightLayer.lightDirection.Count == 0)
 383            {
 0384                return;
 385            }
 386
 0387            if (directionalLightLayer.lightDirection.Count == 1)
 388            {
 0389                lightGO.transform.rotation = directionalLightLayer.lightDirection[0].value;
 0390                return;
 391            }
 392
 0393            float percentage = normalizedDayTime * 100;
 0394            TransitioningQuaternion min = directionalLightLayer.lightDirection[0], max = directionalLightLayer.lightDire
 395
 396            // Apply Direction
 0397            for (int i = 0; i < directionalLightLayer.lightDirection.Count; i++)
 398            {
 0399                if (percentage <= directionalLightLayer.lightDirection[i].percentage)
 400                {
 0401                    max = directionalLightLayer.lightDirection[i];
 402
 0403                    if ((i - 1) > 0)
 404                    {
 0405                        min = directionalLightLayer.lightDirection[i - 1];
 406                    }
 407
 0408                    break;
 409                }
 410            }
 411
 0412            float t = Mathf.InverseLerp(min.percentage, max.percentage, percentage);
 0413            lightGO.transform.rotation = Quaternion.Lerp(min.value, max.value, t);
 0414        }
 415
 416        #endregion
 417
 418        public void ResetMaterial(Material selectedMat, int slotCount)
 419        {
 0420            for (int i = 0; i < slotCount; i++)
 421            {
 0422                ResetSlot(selectedMat, i);
 423            }
 0424        }
 425
 426        public void ResetSlot(Material selectedMat, int slotCount)
 427        {
 0428            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_layerType_" + slotCount), 0);
 0429            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_tex_" + slotCount), null);
 0430            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_cubemap_" + slotCount), null);
 0431            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_normals_" + slotCount), null);
 0432            selectedMat.SetColor(SkyboxShaderUtils.GetLayerProperty("_color_" + slotCount), new Color(1, 1, 1, 0));
 0433            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_tilingAndOffset_" + slotCount), new Vector4(1, 1,
 0434            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_speedAndRotation_" + slotCount), new Vector4(0, 0
 0435            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_timeFrame_" + slotCount), new Vector4(0, 0));
 0436            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_fadeTime_" + slotCount), 1);
 0437            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_normalIntensity_" + slotCount), 0);
 0438            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_lightIntensity_" + slotCount), 0);
 0439            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_RenderDistance_" + slotCount), 3.4f);
 0440            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_distortIntAndSize_" + slotCount), new Vector2(0, 
 0441            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_distortSpeedAndSharp_" + slotCount), new Vector4(
 0442            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_rowAndCollumns_" + slotCount), new Vector4(1, 1))
 0443            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_particlesMainParameters_" + slotCount), new Vecto
 0444            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_particlesSecondaryParameters_" + slotCount), new 
 0445        }
 446
 447    }
 448}