| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Skybox |
| | 7 | | { |
| | 8 | | public class DomeReferences |
| | 9 | | { |
| | 10 | | public GameObject domeGO; |
| | 11 | | public Material domeMat; |
| | 12 | | public bool domeInUse; |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class SkyboxDomeElements |
| | 16 | | { |
| | 17 | | private const string DOME_RESOURCES_PATH = "SkyboxPrefabs/Dome"; |
| | 18 | |
|
| | 19 | | private readonly GameObject domeElements; |
| | 20 | | private GameObject domePrefab; |
| | 21 | | private FollowBehaviour followBehavior; |
| | 22 | |
|
| 0 | 23 | | Queue<DomeReferences> domeObjects = new Queue<DomeReferences>(); |
| 0 | 24 | | Queue<DomeReferences> activeDomeObjects = new Queue<DomeReferences>(); |
| | 25 | |
|
| 0 | 26 | | public SkyboxDomeElements(GameObject domeElements) |
| | 27 | | { |
| 0 | 28 | | this.domeElements = domeElements; |
| 0 | 29 | | Initialize(); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | private void Initialize() |
| | 33 | | { |
| 0 | 34 | | if (domeElements == null) |
| | 35 | | { |
| 0 | 36 | | Debug.LogError("Dome Elements Container is null"); |
| 0 | 37 | | return; |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | followBehavior = domeElements.GetOrCreateComponent<FollowBehaviour>(); |
| 0 | 41 | | followBehavior.followPos = true; |
| | 42 | |
|
| 0 | 43 | | for (int i = 0; i < domeElements.transform.childCount; i++) |
| | 44 | | { |
| 0 | 45 | | DomeReferences dome = new DomeReferences(); |
| 0 | 46 | | dome.domeGO = domeElements.transform.GetChild(i).gameObject; |
| 0 | 47 | | dome.domeGO.GetComponent<Renderer>().material = Object.Instantiate(MaterialReferenceContainer.i.domeMat) |
| 0 | 48 | | dome.domeMat = dome.domeGO.GetComponent<Renderer>().sharedMaterial; |
| 0 | 49 | | dome.domeGO.SetActive(false); |
| 0 | 50 | | domeObjects.Enqueue(dome); |
| | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public void ResetObjects() |
| | 55 | | { |
| 0 | 56 | | while (activeDomeObjects.Count > 0) |
| | 57 | | { |
| 0 | 58 | | DomeReferences dome = activeDomeObjects.Dequeue(); |
| 0 | 59 | | dome.domeGO.SetActive(false); |
| 0 | 60 | | domeObjects.Enqueue(dome); |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private DomeReferences GetDomeReference() |
| | 65 | | { |
| 0 | 66 | | DomeReferences dome = null; |
| 0 | 67 | | if (domeObjects.Count <= 0) |
| | 68 | | { |
| 0 | 69 | | dome = InstantiateNewDome(); |
| 0 | 70 | | } |
| | 71 | | else |
| | 72 | | { |
| 0 | 73 | | dome = domeObjects.Dequeue(); |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | dome.domeGO.SetActive(true); |
| | 77 | |
|
| | 78 | | // Add to active objects |
| 0 | 79 | | activeDomeObjects.Enqueue(dome); |
| 0 | 80 | | return dome; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | private DomeReferences InstantiateNewDome() |
| | 84 | | { |
| 0 | 85 | | if (domePrefab == null) |
| | 86 | | { |
| 0 | 87 | | domePrefab = Resources.Load<GameObject>(DOME_RESOURCES_PATH); |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | GameObject obj = GameObject.Instantiate<GameObject>(domePrefab); |
| 0 | 91 | | obj.layer = LayerMask.NameToLayer("Skybox"); |
| 0 | 92 | | obj.name = "Dome"; |
| 0 | 93 | | obj.transform.parent = domeElements.transform; |
| 0 | 94 | | obj.transform.localPosition = new Vector3(0, -0.7f, 0); |
| 0 | 95 | | obj.GetComponent<Renderer>().material = Object.Instantiate<Material>(MaterialReferenceContainer.i.domeMat); |
| | 96 | |
|
| 0 | 97 | | DomeReferences dome = new DomeReferences(); |
| 0 | 98 | | dome.domeGO = obj; |
| 0 | 99 | | dome.domeMat = obj.GetComponent<Renderer>().sharedMaterial; |
| | 100 | |
|
| 0 | 101 | | return dome; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | private List<DomeReferences> GetOrderedGameobjectList(List<Config3DDome> configList) |
| | 105 | | { |
| 0 | 106 | | ResetObjects(); |
| 0 | 107 | | List<DomeReferences> orderedList = new List<DomeReferences>(); |
| | 108 | | // make a list of domes for each active dome config |
| | 109 | |
|
| 0 | 110 | | for (int i = 0; i < configList.Count; i++) |
| | 111 | | { |
| 0 | 112 | | if (!configList[i].enabled) |
| | 113 | | { |
| | 114 | | continue; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | DomeReferences dome = GetDomeReference(); |
| | 118 | |
|
| 0 | 119 | | orderedList.Insert(0, dome); |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | return orderedList; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | public void ApplyDomeConfigurations(SkyboxConfiguration config, float dayTime, float normalizedDayTime, Light di |
| | 126 | | { |
| 0 | 127 | | List<DomeReferences> domeReferences = GetOrderedGameobjectList(config.additional3Dconfig); |
| | 128 | |
|
| | 129 | | float percentage = normalizedDayTime * 100; |
| 0 | 130 | | int domeCount = 0; |
| | 131 | |
|
| 0 | 132 | | for (int i = 0; i < config.additional3Dconfig.Count; i++) |
| | 133 | | { |
| 0 | 134 | | if (!config.additional3Dconfig[i].enabled) |
| | 135 | | { |
| | 136 | | // Change all texture layer rendering to NotRendering |
| | 137 | | continue; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // If dome is not active due to time, Increment dome number, close dome GO and continue |
| 0 | 141 | | if (!config.additional3Dconfig[i].IsConfigActive(dayTime, cycleTime)) |
| | 142 | | { |
| 0 | 143 | | domeReferences[domeCount].domeGO.SetActive(false); |
| 0 | 144 | | domeCount++; |
| 0 | 145 | | continue; |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | domeReferences[domeCount].domeGO.SetActive(true); |
| | 149 | |
|
| | 150 | | // resize |
| 0 | 151 | | domeReferences[domeCount].domeGO.transform.localScale = config.additional3Dconfig[i].domeRadius * Vector |
| | 152 | |
|
| 0 | 153 | | domeReferences[domeCount].domeGO.name = config.additional3Dconfig[i].layers.nameInEditor; |
| | 154 | |
|
| | 155 | | //Apply config |
| | 156 | | //General Values |
| 0 | 157 | | domeReferences[domeCount].domeMat.SetColor(SkyboxShaderUtils.LightTint, config.directionalLightLayer.tin |
| 0 | 158 | | domeReferences[domeCount].domeMat.SetVector(SkyboxShaderUtils.LightDirection, directionalLightGO.transfo |
| | 159 | |
|
| | 160 | |
|
| 0 | 161 | | TextureLayerFunctionality.ApplyTextureLayer(domeReferences[domeCount].domeMat, dayTime, normalizedDayTim |
| 0 | 162 | | domeCount++; |
| | 163 | | } |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public void ResolveCameraDependency(Transform currentTransform) |
| | 167 | | { |
| 0 | 168 | | if (followBehavior != null) |
| | 169 | | { |
| 0 | 170 | | followBehavior.target = currentTransform.gameObject; |
| | 171 | | } |
| 0 | 172 | | } |
| | 173 | | } |
| | 174 | | } |