< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SkyboxPlanarElements(...)0%2100%
Initialize()0%2100%
ResolveCameraDependency(...)0%2100%
ApplyConfig(...)0%72800%
GetAllEnabledPlanarRefs(...)0%6200%
ChangeRenderingCamera(...)0%6200%
ParentPlanarLayer(...)0%6200%
ApplyParticleProperties(...)0%2100%
GetPlanarRef(...)0%20400%
InstantiateNewSatelliteReference(...)0%2100%
ResetPlanes()0%12300%
IsLayerActiveInCurrentTime(...)0%30500%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6namespace DCL.Skybox
 7{
 8    public class PlanarRefs
 9    {
 10        public GameObject prefab;
 11        public GameObject planarObject;
 12        public bool inUse = false;
 13    }
 14
 15    public class SkyboxPlanarElements
 16    {
 17        private const string LAYER = "Skybox";
 18
 19        private readonly GameObject planarElements;
 20        private Transform planarCameraFollowingElements;
 21        private Transform planarStaticElements;
 22        private FollowBehaviour followObj;
 23
 024        private Dictionary<GameObject, Queue<PlanarRefs>> planarReferences = new Dictionary<GameObject, Queue<PlanarRefs
 025        private List<PlanarRefs> usedPlanes = new List<PlanarRefs>();
 26
 027        public SkyboxPlanarElements(GameObject planarElements)
 28        {
 029            this.planarElements = planarElements;
 30            // Get or instantiate Skybox elements GameObject
 031            Initialize();
 032        }
 33
 34        private void Initialize()
 35        {
 036            planarCameraFollowingElements = new GameObject("Camera Following").transform;
 037            planarCameraFollowingElements.parent = planarElements.transform;
 038            planarCameraFollowingElements.gameObject.layer = LayerMask.NameToLayer(LAYER);
 39
 040            planarStaticElements = new GameObject("Static").transform;
 041            planarStaticElements.parent = planarElements.transform;
 042            planarStaticElements.gameObject.layer = LayerMask.NameToLayer(LAYER);
 43
 44            // Add follow script
 045            followObj = planarCameraFollowingElements.gameObject.AddComponent<FollowBehaviour>();
 046            followObj.ignoreYAxis = true;
 047            followObj.followPos = true;
 048        }
 49
 050        internal void ResolveCameraDependency(Transform cameraTransform) { followObj.target = cameraTransform.gameObject
 51
 52        internal void ApplyConfig(List<Config3DPlanar> planarLayers, float timeOfTheDay, float cycleTime, bool isEditor)
 53        {
 054            if (isEditor || (usedPlanes == null))
 55            {
 056                ResetPlanes();
 057                GetAllEnabledPlanarRefs(planarLayers);
 58            }
 59
 060            for (int i = 0; i < usedPlanes.Count; i++)
 61            {
 62                // If satellite is disabled, disable the 3D object too.
 063                if (!planarLayers[i].enabled)
 64                {
 065                    if (usedPlanes[i] != null)
 66                    {
 067                        usedPlanes[i].planarObject.SetActive(false);
 68                    }
 069                    continue;
 70                }
 71
 072                if (usedPlanes[i] == null)
 73                {
 74#if UNITY_EDITOR
 075                    Debug.LogWarning(planarLayers[i].nameInEditor + " Plane not working!, prefab not assigned");
 76#endif
 077                    continue;
 78                }
 79
 080                ApplyParticleProperties(planarLayers[i], usedPlanes[i]);
 81
 82                // Parent with the moving or static parent
 083                ParentPlanarLayer(planarLayers[i], usedPlanes[i]);
 84
 85                // Change layer mask
 086                ChangeRenderingCamera(planarLayers[i], usedPlanes[i]);
 87
 88                // if this layer is not active at present time, disable the object
 089                if (!IsLayerActiveInCurrentTime(timeOfTheDay, planarLayers[i], cycleTime))
 90                {
 091                    planarLayers[i].renderType = LayerRenderType.NotRendering;
 092                    usedPlanes[i].planarObject.SetActive(false);
 93                    continue;
 94                }
 95
 96            }
 097        }
 98
 99        public List<PlanarRefs> GetAllEnabledPlanarRefs(List<Config3DPlanar> planarLayers)
 100        {
 0101            for (int i = 0; i < planarLayers.Count; i++)
 102            {
 0103                GetPlanarRef(planarLayers[i]);
 104            }
 0105            return usedPlanes;
 106        }
 107
 108        private void ChangeRenderingCamera(Config3DPlanar config, PlanarRefs tempRef)
 109        {
 0110            if (config.renderWithMainCamera)
 111            {
 0112                tempRef.planarObject.layer = 0;
 113            }
 114            else
 115            {
 0116                tempRef.planarObject.layer = LayerMask.NameToLayer(LAYER);
 117            }
 0118        }
 119
 120        private void ParentPlanarLayer(Config3DPlanar config, PlanarRefs tempRef)
 121        {
 0122            if (config.followCamera)
 123            {
 0124                tempRef.planarObject.transform.parent = planarCameraFollowingElements;
 125            }
 126            else
 127            {
 0128                tempRef.planarObject.transform.parent = planarStaticElements;
 129            }
 0130        }
 131
 132        private void ApplyParticleProperties(Config3DPlanar config, PlanarRefs tempRef)
 133        {
 134            // Apply radius
 0135            ParticleSystem particle = tempRef.planarObject.GetComponent<ParticleSystem>();
 0136            var shape = particle.shape;
 0137            shape.radius = config.radius;
 138
 139            // Apply y-pos
 0140            Vector3 pos = tempRef.planarObject.transform.position;
 0141            pos.y = config.yPos;
 0142            tempRef.planarObject.transform.position = pos;
 0143        }
 144
 145        public PlanarRefs GetPlanarRef(Config3DPlanar config)
 146        {
 0147            PlanarRefs tempPlane = null;
 148
 0149            if (config.prefab == null)
 150            {
 0151                usedPlanes.Add(tempPlane);
 0152                return tempPlane;
 153            }
 154
 155            // Check if GO for this prefab is already in scene, else create new
 0156            if (planarReferences.ContainsKey(config.prefab))
 157            {
 158                // Check if there is any unused GO for the given prefab
 0159                if (planarReferences[config.prefab].Count > 0)
 160                {
 0161                    tempPlane = planarReferences[config.prefab].Dequeue();
 162                }
 163                else
 164                {
 0165                    tempPlane = InstantiateNewSatelliteReference(config);
 166                }
 167            }
 168            else
 169            {
 0170                planarReferences.Add(config.prefab, new Queue<PlanarRefs>());
 0171                tempPlane = InstantiateNewSatelliteReference(config);
 172            }
 0173            usedPlanes.Add(tempPlane);
 0174            tempPlane.planarObject.SetActive(true);
 175
 0176            return tempPlane;
 177        }
 178
 179        PlanarRefs InstantiateNewSatelliteReference(Config3DPlanar config)
 180        {
 0181            GameObject obj = GameObject.Instantiate<GameObject>(config.prefab);
 0182            obj.layer = LayerMask.NameToLayer(LAYER);
 0183            obj.name = "Planar Layer";
 0184            obj.transform.parent = planarElements.transform;
 0185            obj.transform.localPosition = Vector3.zero;
 186
 0187            PlanarRefs planar = new PlanarRefs();
 0188            planar.prefab = config.prefab;
 0189            planar.planarObject = obj;
 190
 0191            return planar;
 192        }
 193
 194        private void ResetPlanes()
 195        {
 0196            if (usedPlanes != null)
 197            {
 0198                for (int i = 0; i < usedPlanes.Count; i++)
 199                {
 0200                    PlanarRefs plane = usedPlanes[i];
 0201                    plane.inUse = false;
 0202                    plane.planarObject.SetActive(false);
 0203                    planarReferences[plane.prefab].Enqueue(plane);
 204                }
 0205                usedPlanes.Clear();
 206            }
 0207        }
 208
 209        private bool IsLayerActiveInCurrentTime(float timeOfTheDay, Config3DPlanar config, float cycleTime)
 210        {
 211            // Calculate edited time for the case of out time less than in time (over the day scenario)
 0212            float outTimeEdited = config.timeSpan_End;
 0213            float timeOfTheDayEdited = timeOfTheDay;
 214
 0215            if (config.timeSpan_End < config.timeSpan_start)
 216            {
 0217                outTimeEdited += cycleTime;
 218            }
 219
 0220            if (timeOfTheDay < config.timeSpan_start)
 221            {
 0222                timeOfTheDayEdited += cycleTime;
 223            }
 224
 0225            if (timeOfTheDayEdited >= config.timeSpan_start && timeOfTheDayEdited <= outTimeEdited)
 226            {
 0227                return true;
 228            }
 229            else
 230            {
 0231                return false;
 232            }
 233        }
 234
 235    }
 236}