< 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:196
Coverable lines:196
Total lines:446
Line coverage:0% (0 of 196)
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.Collections.Generic;
 2using DCL.Shaders;
 3using UnityEngine;
 4
 5namespace DCL.Skybox
 6{
 7    [CreateAssetMenu(fileName = "Skybox Configuration", menuName = "ScriptableObjects/SkyboxConfiguration", order = 1)]
 8    public class SkyboxConfiguration : ScriptableObject
 9    {
 10        public delegate void TimelineEvents(string tag, bool enable, bool trigger);
 11        public event TimelineEvents OnTimelineEvent;
 12
 13        // ID
 14        public string skyboxID;
 15
 16        // Background Color
 017        public Gradient skyColor = new Gradient();
 018        public Gradient horizonColor = new Gradient();
 019        public Gradient groundColor = new Gradient();
 020        public List<TransitioningFloat> horizonWidth = new List<TransitioningFloat>();
 021        public List<TransitioningFloat> horizonHeight = new List<TransitioningFloat>();
 22
 23        // Horizon Layer
 24        public Texture2D horizonMask;
 025        public Vector2 horizonMaskTiling = new Vector2(0, 0);
 026        public Vector2 horizonMaskOffset = new Vector2(0, 0);
 27
 28        // Horizon Plane Layer
 29        public Texture2D horizonPlaneTexture;
 030        public Vector2 horizonPlaneTiling = new Vector2(0, 0);
 031        public Vector2 horizonPlaneOffset = new Vector2(0, 0);
 032        public Gradient horizonPlaneColor = new Gradient();
 033        public List<TransitioningFloat> horizonPlaneHeight = new List<TransitioningFloat>();
 034        public Vector2 horizonPlaneSmoothRange = new Vector2(0, 0);
 35        public float horizonLightIntensity = 0;
 36
 37        // Ambient Color
 038        public bool ambientTrilight = true;
 039        public Gradient ambientSkyColor = new Gradient();
 040        public Gradient ambientEquatorColor = new Gradient();
 041        public Gradient ambientGroundColor = new Gradient();
 42
 43        // Avatar Color
 044        public bool useAvatarGradient = true;
 045        public Gradient avatarTintGradient = new Gradient();
 046        public Color avatarTintColor = Color.white;
 047        public bool useAvatarRealtimeDLDirection = true;
 048        public Vector3 avatarLightConstantDir = new Vector3(-18f, 144f, -72f);
 049        public bool useAvatarRealtimeLightColor = true;
 050        public Gradient avatarLightColorGradient = new Gradient();
 51
 52        // Avatar Editor Color
 053        public Color avatarEditorTintColor = Color.white;
 054        public Vector3 avatarEditorLightDir = new Vector3(-18f, 144f, -72f);
 055        public Color avatarEditorLightColor = Color.white;
 56
 57        // Fog Properties
 58        public bool useFog = false;
 059        public FogMode fogMode = FogMode.ExponentialSquared;
 060        public Gradient fogColor = new Gradient();
 061        public float fogDensity = 0.05f;
 62        public float fogStartDistance = 0;
 063        public float fogEndDistance = 300;
 64
 65        // DirectionalLight Properties
 066        public bool useDirectionalLight = true;
 067        public DirectionalLightAttributes directionalLightLayer = new DirectionalLightAttributes();
 68
 69        // Layers
 070        public List<TextureLayer> layers = new List<TextureLayer>();
 71
 72        // Timeline Tags
 073        public List<TimelineTagsDuration> timelineTags = new List<TimelineTagsDuration>();
 74
 75        // 3D Components
 076        public List<Config3DDome> additional3Dconfig = new List<Config3DDome>();
 077        public List<Config3DSatellite> satelliteLayers = new List<Config3DSatellite>();
 078        public List<Config3DPlanar> planarLayers = new List<Config3DPlanar>();
 79
 080        private float cycleTime = 24;
 81
 82        public void ApplyOnMaterial(Material selectedMat, float dayTime, float normalizedDayTime, int slotCount = 5, Lig
 83        {
 084            float percentage = normalizedDayTime * 100;
 85
 86            // General Values
 087            selectedMat.SetColor(SkyboxShaderUtils.LightTint, directionalLightLayer.tintColor.Evaluate(normalizedDayTime
 088            selectedMat.SetVector(SkyboxShaderUtils.LightDirection, directionalLightGO.transform.rotation.eulerAngles);
 89
 90            // Apply Base Values
 091            selectedMat.SetColor(SkyboxShaderUtils.SkyColor, skyColor.Evaluate(normalizedDayTime));
 092            selectedMat.SetColor(SkyboxShaderUtils.GroundColor, groundColor.Evaluate(normalizedDayTime));
 93
 94            // Apply Horizon Values
 095            selectedMat.SetColor(SkyboxShaderUtils.HorizonColor, horizonColor.Evaluate(normalizedDayTime));
 096            selectedMat.SetFloat(SkyboxShaderUtils.HorizonHeight, TransitioningValues.GetTransitionValue(horizonHeight, 
 097            selectedMat.SetFloat(SkyboxShaderUtils.HorizonWidth, TransitioningValues.GetTransitionValue(horizonWidth, pe
 098            selectedMat.SetTexture(SkyboxShaderUtils.HorizonMask, horizonMask);
 099            selectedMat.SetVector(SkyboxShaderUtils.HorizonMaskValues, new Vector4(horizonMaskTiling.x, horizonMaskTilin
 0100            selectedMat.SetTexture(SkyboxShaderUtils.HorizonPlane, horizonPlaneTexture);
 0101            selectedMat.SetVector(SkyboxShaderUtils.HorizonPlaneValues, new Vector4(horizonPlaneTiling.x, horizonPlaneTi
 0102            selectedMat.SetColor(SkyboxShaderUtils.HorizonPlaneColor, horizonPlaneColor.Evaluate(normalizedDayTime));
 0103            selectedMat.SetFloat(SkyboxShaderUtils.HorizonPlaneHeight, TransitioningValues.GetTransitionValue(horizonPla
 0104            selectedMat.SetVector(SkyboxShaderUtils.PlaneSmoothRange, horizonPlaneSmoothRange);
 0105            selectedMat.SetFloat(SkyboxShaderUtils.HorizonLightIntensity, horizonLightIntensity);
 106
 107
 108            // Apply Ambient colors to the rendering settings
 0109            if (ambientTrilight)
 110            {
 0111                RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Trilight;
 0112                RenderSettings.ambientSkyColor = ambientSkyColor.Evaluate(normalizedDayTime);
 0113                RenderSettings.ambientEquatorColor = ambientEquatorColor.Evaluate(normalizedDayTime);
 0114                RenderSettings.ambientGroundColor = ambientGroundColor.Evaluate(normalizedDayTime);
 115            }
 116            else
 117            {
 0118                RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Skybox;
 119            }
 120
 121            //Fog Values
 0122            RenderSettings.fog = useFog;
 0123            if (useFog)
 124            {
 0125                RenderSettings.fogColor = fogColor.Evaluate(normalizedDayTime);
 0126                RenderSettings.fogMode = fogMode;
 0127                switch (fogMode)
 128                {
 129                    case FogMode.Linear:
 0130                        RenderSettings.fogStartDistance = fogStartDistance;
 0131                        RenderSettings.fogEndDistance = fogEndDistance;
 0132                        break;
 133                    default:
 0134                        RenderSettings.fogDensity = fogDensity;
 135                        break;
 136                }
 137            }
 138
 139            //Directional List
 0140            if (useDirectionalLight)
 141            {
 142                // Apply Direction light color
 0143                directionalLightGO.color = directionalLightLayer.lightColor.Evaluate(normalizedDayTime);
 144
 145                // Apply direction
 0146                ApplyDLDirection(normalizedDayTime, directionalLightGO);
 147
 148                // Apply Intensity
 0149                ApplyDLIntensity(normalizedDayTime, directionalLightGO);
 150            }
 151            else
 152            {
 0153                directionalLightGO.gameObject.SetActive(false);
 154            }
 155
 156            // Check and Fire timeline events
 0157            CheckAndFireTimelineEvents(dayTime);
 158
 0159            ApplyAllSlots(selectedMat, layers, dayTime, normalizedDayTime, slotCount, cycleTime);
 0160        }
 161
 162        public void ApplyInWorldAvatarColor(float normalizedDayTime, GameObject directionalLightGO)
 163        {
 0164            if (useAvatarGradient)
 165            {
 0166                Shader.SetGlobalColor(ShaderUtils.TintColor, avatarTintGradient.Evaluate(normalizedDayTime));
 167            }
 168            else
 169            {
 0170                Shader.SetGlobalColor(ShaderUtils.TintColor, avatarTintColor);
 171            }
 172
 173            // Apply Avatar Light Direction
 0174            if (!useAvatarRealtimeDLDirection || directionalLightGO == null || !useDirectionalLight)
 175            {
 0176                Shader.SetGlobalVector(ShaderUtils.LightDir, avatarLightConstantDir / 180);
 177            }
 178            else
 179            {
 0180                Vector3 tempDir = directionalLightGO.transform.rotation.eulerAngles;
 0181                Shader.SetGlobalVector(ShaderUtils.LightDir, tempDir / 180);
 182            }
 183
 184            // Apply Avatar Light Color
 0185            if (!useAvatarRealtimeLightColor || directionalLightGO == null || !useDirectionalLight)
 186            {
 0187                Shader.SetGlobalColor(ShaderUtils.LightColor, avatarLightColorGradient.Evaluate(normalizedDayTime));
 188            }
 189            else
 190            {
 0191                Shader.SetGlobalColor(ShaderUtils.LightColor, directionalLightLayer.lightColor.Evaluate(normalizedDayTim
 192            }
 0193        }
 194
 195        public void ApplyEditorAvatarColor()
 196        {
 0197            Shader.SetGlobalColor(ShaderUtils.TintColor, avatarEditorTintColor);
 0198            Shader.SetGlobalVector(ShaderUtils.LightDir, avatarEditorLightDir / 180);
 0199            Shader.SetGlobalColor(ShaderUtils.LightColor, avatarEditorLightColor);
 0200        }
 201
 202        void ApplyAllSlots(Material selectedMat, List<TextureLayer> layers, float dayTime, float normalizedDayTime, int 
 203        {
 0204            for (int i = 0; i < slotCount; i++)
 205            {
 0206                TextureLayer layer = GetActiveLayer(layers, dayTime, i);
 207
 0208                if (layer == null || !layer.enabled)
 209                {
 0210                    ResetSlot(selectedMat, i);
 0211                    continue;
 212                }
 213
 0214                TextureLayerFunctionality.ApplyTextureLayer(selectedMat, dayTime, normalizedDayTime, i, layer, cycleTime
 215            }
 0216        }
 217
 218        public TextureLayer GetActiveLayer(List<TextureLayer> layers, float currentTime, int slotID)
 219        {
 0220            TextureLayer temp = null;
 221
 0222            for (int i = 0; i < layers.Count; i++)
 223            {
 0224                if (layers[i].slotID != slotID)
 225                {
 226                    continue;
 227                }
 228
 0229                float endTimeEdited = layers[i].timeSpan_End;
 0230                float dayTimeEdited = currentTime;
 231
 0232                if (layers[i].timeSpan_End < layers[i].timeSpan_start)
 233                {
 0234                    endTimeEdited = cycleTime + layers[i].timeSpan_End;
 0235                    if (currentTime < layers[i].timeSpan_start)
 236                    {
 0237                        dayTimeEdited = cycleTime + currentTime;
 238                    }
 239                }
 240
 0241                if (dayTimeEdited >= layers[i].timeSpan_start && dayTimeEdited <= endTimeEdited)
 242                {
 0243                    if (layers[i].enabled)
 244                    {
 0245                        if (temp == null)
 246                        {
 0247                            layers[i].renderType = LayerRenderType.Rendering;
 0248                            temp = layers[i];
 249                        }
 250                        else
 251                        {
 0252                            temp.renderType = LayerRenderType.Conflict_Playing;
 0253                            layers[i].renderType = LayerRenderType.Conflict_NotPlaying;
 254                        }
 255                    }
 256                    else
 257                    {
 0258                        layers[i].renderType = LayerRenderType.NotRendering;
 259                    }
 260                }
 261                else
 262                {
 0263                    layers[i].renderType = LayerRenderType.NotRendering;
 264                }
 265
 266
 267            }
 0268            return temp;
 269        }
 270
 271        #region Timeline Events
 272
 273        /// <summary>
 274        /// Check if any event has to be fired
 275        /// TODO:: Can be optimised.
 276        /// </summary>
 277        /// <param name="dayTime">Current day time</param>
 278        private void CheckAndFireTimelineEvents(float dayTime)
 279        {
 0280            float endTimeEdited = 0;
 0281            float dayTimeEdited = dayTime;
 0282            for (int i = 0; i < timelineTags.Count; i++)
 283            {
 284                // If current event is a trigger event, fire trigger event. It's boolean will be disable when cycle rese
 0285                if (timelineTags[i].isTrigger)
 286                {
 0287                    if (dayTime > timelineTags[i].startTime && !timelineTags[i].startEventExecuted)
 288                    {
 0289                        OnTimelineEvent?.Invoke(timelineTags[i].tag, true, true);
 0290                        timelineTags[i].startEventExecuted = true;
 291                    }
 0292                    continue;
 293                }
 294
 295
 0296                endTimeEdited = timelineTags[i].endTime;
 0297                dayTimeEdited = dayTime;
 298
 299                // Change time if this is over a day scenario
 0300                if (timelineTags[i].endTime < timelineTags[i].startTime)
 301                {
 0302                    endTimeEdited = cycleTime + timelineTags[i].endTime;
 303
 0304                    if (dayTime < timelineTags[i].startTime)
 305                    {
 0306                        dayTimeEdited = cycleTime + dayTime;
 307                    }
 308                }
 309
 310                // If current time is between start and end time and start event is not executed, Fire start event
 0311                if (dayTimeEdited >= timelineTags[i].startTime && dayTimeEdited < endTimeEdited && !timelineTags[i].star
 312                {
 0313                    OnTimelineEvent?.Invoke(timelineTags[i].tag, true, false);
 0314                    timelineTags[i].startEventExecuted = true;
 315                }
 316
 317                // If current time is greater than end time and start event is executed, fire end event
 0318                if (dayTimeEdited >= endTimeEdited && timelineTags[i].startEventExecuted)
 319                {
 0320                    OnTimelineEvent?.Invoke(timelineTags[i].tag, false, false);
 0321                    timelineTags[i].startEventExecuted = false;
 322                }
 323
 324            }
 0325        }
 326
 327        public void CycleResets()
 328        {
 329            // Resets all timeline triggers and normal flow events
 0330            for (int i = 0; i < timelineTags.Count; i++)
 331            {
 0332                if (timelineTags[i].isTrigger)
 333                {
 0334                    timelineTags[i].startEventExecuted = false;
 0335                    continue;
 336                }
 337
 0338                if (timelineTags[i].endTime > timelineTags[i].startTime)
 339                {
 0340                    timelineTags[i].startEventExecuted = false;
 341                }
 342            }
 0343        }
 344
 345        #endregion
 346
 347        #region Directional Light
 348
 349        void ApplyDLIntensity(float normalizedDayTime, Light lightGO)
 350        {
 0351            if (lightGO == null)
 352            {
 0353                Debug.LogError("Directional Light option is on, with light reference");
 0354                return;
 355            }
 356
 0357            if (directionalLightLayer.intensity.Count == 0)
 358            {
 0359                Debug.Log("Directional Light option is on with no intensity data");
 0360                return;
 361            }
 362
 0363            if (directionalLightLayer.intensity.Count == 1)
 364            {
 0365                lightGO.intensity = directionalLightLayer.intensity[0].value;
 0366                return;
 367            }
 368
 369
 0370            lightGO.intensity = TransitioningValues.GetTransitionValue(directionalLightLayer.intensity, normalizedDayTim
 0371        }
 372
 373        void ApplyDLDirection(float normalizedDayTime, Light lightGO)
 374        {
 0375            if (lightGO == null)
 376            {
 0377                return;
 378            }
 379
 0380            if (directionalLightLayer.lightDirection.Count == 0)
 381            {
 0382                return;
 383            }
 384
 0385            if (directionalLightLayer.lightDirection.Count == 1)
 386            {
 0387                lightGO.transform.rotation = directionalLightLayer.lightDirection[0].value;
 0388                return;
 389            }
 390
 0391            float percentage = normalizedDayTime * 100;
 0392            TransitioningQuaternion min = directionalLightLayer.lightDirection[0], max = directionalLightLayer.lightDire
 393
 394            // Apply Direction
 0395            for (int i = 0; i < directionalLightLayer.lightDirection.Count; i++)
 396            {
 0397                if (percentage <= directionalLightLayer.lightDirection[i].percentage)
 398                {
 0399                    max = directionalLightLayer.lightDirection[i];
 400
 0401                    if ((i - 1) > 0)
 402                    {
 0403                        min = directionalLightLayer.lightDirection[i - 1];
 404                    }
 405
 0406                    break;
 407                }
 408            }
 409
 0410            float t = Mathf.InverseLerp(min.percentage, max.percentage, percentage);
 0411            lightGO.transform.rotation = Quaternion.Lerp(min.value, max.value, t);
 0412        }
 413
 414        #endregion
 415
 416        public void ResetMaterial(Material selectedMat, int slotCount)
 417        {
 0418            for (int i = 0; i < slotCount; i++)
 419            {
 0420                ResetSlot(selectedMat, i);
 421            }
 0422        }
 423
 424        public void ResetSlot(Material selectedMat, int slotCount)
 425        {
 0426            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_layerType_" + slotCount), 0);
 0427            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_tex_" + slotCount), null);
 0428            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_cubemap_" + slotCount), null);
 0429            selectedMat.SetTexture(SkyboxShaderUtils.GetLayerProperty("_normals_" + slotCount), null);
 0430            selectedMat.SetColor(SkyboxShaderUtils.GetLayerProperty("_color_" + slotCount), new Color(1, 1, 1, 0));
 0431            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_tilingAndOffset_" + slotCount), new Vector4(1, 1,
 0432            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_speedAndRotation_" + slotCount), new Vector4(0, 0
 0433            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_timeFrame_" + slotCount), new Vector4(0, 0));
 0434            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_fadeTime_" + slotCount), 1);
 0435            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_normalIntensity_" + slotCount), 0);
 0436            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_lightIntensity_" + slotCount), 0);
 0437            selectedMat.SetFloat(SkyboxShaderUtils.GetLayerProperty("_RenderDistance_" + slotCount), 3.4f);
 0438            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_distortIntAndSize_" + slotCount), new Vector2(0, 
 0439            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_distortSpeedAndSharp_" + slotCount), new Vector4(
 0440            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_rowAndCollumns_" + slotCount), new Vector4(1, 1))
 0441            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_particlesMainParameters_" + slotCount), new Vecto
 0442            selectedMat.SetVector(SkyboxShaderUtils.GetLayerProperty("_particlesSecondaryParameters_" + slotCount), new 
 0443        }
 444
 445    }
 446}