| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace 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 | |
|
| 0 | 24 | | private Dictionary<GameObject, Queue<PlanarRefs>> planarReferences = new Dictionary<GameObject, Queue<PlanarRefs |
| 0 | 25 | | private List<PlanarRefs> usedPlanes = new List<PlanarRefs>(); |
| | 26 | |
|
| 0 | 27 | | public SkyboxPlanarElements(GameObject planarElements) |
| | 28 | | { |
| 0 | 29 | | this.planarElements = planarElements; |
| | 30 | | // Get or instantiate Skybox elements GameObject |
| 0 | 31 | | Initialize(); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | private void Initialize() |
| | 35 | | { |
| 0 | 36 | | planarCameraFollowingElements = new GameObject("Camera Following").transform; |
| 0 | 37 | | planarCameraFollowingElements.parent = planarElements.transform; |
| 0 | 38 | | planarCameraFollowingElements.gameObject.layer = LayerMask.NameToLayer(LAYER); |
| | 39 | |
|
| 0 | 40 | | planarStaticElements = new GameObject("Static").transform; |
| 0 | 41 | | planarStaticElements.parent = planarElements.transform; |
| 0 | 42 | | planarStaticElements.gameObject.layer = LayerMask.NameToLayer(LAYER); |
| | 43 | |
|
| | 44 | | // Add follow script |
| 0 | 45 | | followObj = planarCameraFollowingElements.gameObject.AddComponent<FollowBehaviour>(); |
| 0 | 46 | | followObj.ignoreYAxis = true; |
| 0 | 47 | | followObj.followPos = true; |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | internal void ResolveCameraDependency(Transform cameraTransform) { followObj.target = cameraTransform.gameObject |
| | 51 | |
|
| | 52 | | internal void ApplyConfig(List<Config3DPlanar> planarLayers, float timeOfTheDay, float cycleTime, bool isEditor) |
| | 53 | | { |
| 0 | 54 | | if (isEditor || (usedPlanes == null)) |
| | 55 | | { |
| 0 | 56 | | ResetPlanes(); |
| 0 | 57 | | GetAllEnabledPlanarRefs(planarLayers); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | for (int i = 0; i < usedPlanes.Count; i++) |
| | 61 | | { |
| | 62 | | // If satellite is disabled, disable the 3D object too. |
| 0 | 63 | | if (!planarLayers[i].enabled) |
| | 64 | | { |
| 0 | 65 | | if (usedPlanes[i] != null) |
| | 66 | | { |
| 0 | 67 | | usedPlanes[i].planarObject.SetActive(false); |
| | 68 | | } |
| 0 | 69 | | continue; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | if (usedPlanes[i] == null) |
| | 73 | | { |
| | 74 | | #if UNITY_EDITOR |
| 0 | 75 | | Debug.LogWarning(planarLayers[i].nameInEditor + " Plane not working!, prefab not assigned"); |
| | 76 | | #endif |
| 0 | 77 | | continue; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | ApplyParticleProperties(planarLayers[i], usedPlanes[i]); |
| | 81 | |
|
| | 82 | | // Parent with the moving or static parent |
| 0 | 83 | | ParentPlanarLayer(planarLayers[i], usedPlanes[i]); |
| | 84 | |
|
| | 85 | | // Change layer mask |
| 0 | 86 | | ChangeRenderingCamera(planarLayers[i], usedPlanes[i]); |
| | 87 | |
|
| | 88 | | // if this layer is not active at present time, disable the object |
| 0 | 89 | | if (!IsLayerActiveInCurrentTime(timeOfTheDay, planarLayers[i], cycleTime)) |
| | 90 | | { |
| 0 | 91 | | planarLayers[i].renderType = LayerRenderType.NotRendering; |
| 0 | 92 | | usedPlanes[i].planarObject.SetActive(false); |
| | 93 | | continue; |
| | 94 | | } |
| | 95 | |
|
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public List<PlanarRefs> GetAllEnabledPlanarRefs(List<Config3DPlanar> planarLayers) |
| | 100 | | { |
| 0 | 101 | | for (int i = 0; i < planarLayers.Count; i++) |
| | 102 | | { |
| 0 | 103 | | GetPlanarRef(planarLayers[i]); |
| | 104 | | } |
| 0 | 105 | | return usedPlanes; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | private void ChangeRenderingCamera(Config3DPlanar config, PlanarRefs tempRef) |
| | 109 | | { |
| 0 | 110 | | if (config.renderWithMainCamera) |
| | 111 | | { |
| 0 | 112 | | tempRef.planarObject.layer = 0; |
| 0 | 113 | | } |
| | 114 | | else |
| | 115 | | { |
| 0 | 116 | | tempRef.planarObject.layer = LayerMask.NameToLayer(LAYER); |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void ParentPlanarLayer(Config3DPlanar config, PlanarRefs tempRef) |
| | 121 | | { |
| 0 | 122 | | if (config.followCamera) |
| | 123 | | { |
| 0 | 124 | | tempRef.planarObject.transform.parent = planarCameraFollowingElements; |
| 0 | 125 | | } |
| | 126 | | else |
| | 127 | | { |
| 0 | 128 | | tempRef.planarObject.transform.parent = planarStaticElements; |
| | 129 | | } |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private void ApplyParticleProperties(Config3DPlanar config, PlanarRefs tempRef) |
| | 133 | | { |
| | 134 | | // Apply radius |
| 0 | 135 | | ParticleSystem particle = tempRef.planarObject.GetComponent<ParticleSystem>(); |
| 0 | 136 | | var shape = particle.shape; |
| 0 | 137 | | shape.radius = config.radius; |
| | 138 | |
|
| | 139 | | // Apply y-pos |
| 0 | 140 | | Vector3 pos = tempRef.planarObject.transform.position; |
| 0 | 141 | | pos.y = config.yPos; |
| 0 | 142 | | tempRef.planarObject.transform.position = pos; |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | public PlanarRefs GetPlanarRef(Config3DPlanar config) |
| | 146 | | { |
| 0 | 147 | | PlanarRefs tempPlane = null; |
| | 148 | |
|
| 0 | 149 | | if (config.prefab == null) |
| | 150 | | { |
| 0 | 151 | | usedPlanes.Add(tempPlane); |
| 0 | 152 | | return tempPlane; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | // Check if GO for this prefab is already in scene, else create new |
| 0 | 156 | | if (planarReferences.ContainsKey(config.prefab)) |
| | 157 | | { |
| | 158 | | // Check if there is any unused GO for the given prefab |
| 0 | 159 | | if (planarReferences[config.prefab].Count > 0) |
| | 160 | | { |
| 0 | 161 | | tempPlane = planarReferences[config.prefab].Dequeue(); |
| 0 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 0 | 165 | | tempPlane = InstantiateNewSatelliteReference(config); |
| | 166 | | } |
| 0 | 167 | | } |
| | 168 | | else |
| | 169 | | { |
| 0 | 170 | | planarReferences.Add(config.prefab, new Queue<PlanarRefs>()); |
| 0 | 171 | | tempPlane = InstantiateNewSatelliteReference(config); |
| | 172 | | } |
| 0 | 173 | | usedPlanes.Add(tempPlane); |
| 0 | 174 | | tempPlane.planarObject.SetActive(true); |
| | 175 | |
|
| 0 | 176 | | return tempPlane; |
| | 177 | | } |
| | 178 | |
|
| | 179 | | PlanarRefs InstantiateNewSatelliteReference(Config3DPlanar config) |
| | 180 | | { |
| 0 | 181 | | GameObject obj = GameObject.Instantiate<GameObject>(config.prefab); |
| 0 | 182 | | obj.layer = LayerMask.NameToLayer(LAYER); |
| 0 | 183 | | obj.name = "Planar Layer"; |
| 0 | 184 | | obj.transform.parent = planarElements.transform; |
| 0 | 185 | | obj.transform.localPosition = Vector3.zero; |
| | 186 | |
|
| 0 | 187 | | PlanarRefs planar = new PlanarRefs(); |
| 0 | 188 | | planar.prefab = config.prefab; |
| 0 | 189 | | planar.planarObject = obj; |
| | 190 | |
|
| 0 | 191 | | return planar; |
| | 192 | | } |
| | 193 | |
|
| | 194 | | private void ResetPlanes() |
| | 195 | | { |
| 0 | 196 | | if (usedPlanes != null) |
| | 197 | | { |
| 0 | 198 | | for (int i = 0; i < usedPlanes.Count; i++) |
| | 199 | | { |
| 0 | 200 | | PlanarRefs plane = usedPlanes[i]; |
| 0 | 201 | | plane.inUse = false; |
| 0 | 202 | | plane.planarObject.SetActive(false); |
| 0 | 203 | | planarReferences[plane.prefab].Enqueue(plane); |
| | 204 | | } |
| 0 | 205 | | usedPlanes.Clear(); |
| | 206 | | } |
| 0 | 207 | | } |
| | 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) |
| 0 | 212 | | float outTimeEdited = config.timeSpan_End; |
| 0 | 213 | | float timeOfTheDayEdited = timeOfTheDay; |
| | 214 | |
|
| 0 | 215 | | if (config.timeSpan_End < config.timeSpan_start) |
| | 216 | | { |
| 0 | 217 | | outTimeEdited += cycleTime; |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | if (timeOfTheDay < config.timeSpan_start) |
| | 221 | | { |
| 0 | 222 | | timeOfTheDayEdited += cycleTime; |
| | 223 | | } |
| | 224 | |
|
| 0 | 225 | | if (timeOfTheDayEdited >= config.timeSpan_start && timeOfTheDayEdited <= outTimeEdited) |
| | 226 | | { |
| 0 | 227 | | return true; |
| | 228 | | } |
| | 229 | | else |
| | 230 | | { |
| 0 | 231 | | return false; |
| | 232 | | } |
| | 233 | | } |
| | 234 | |
|
| | 235 | | } |
| | 236 | | } |