| | 1 | | using DCL.Configuration; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityGLTF; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class Asset_GLTF : Asset_WithPoolableContainer |
| | 10 | | { |
| | 11 | | private const string CONTAINER_GO_NAME = "Asset_GLTF Container"; |
| | 12 | |
|
| | 13 | | public string name; |
| 145 | 14 | | public bool visible = true; |
| | 15 | |
|
| 2345 | 16 | | public override GameObject container { get; set; } |
| | 17 | |
|
| 145 | 18 | | public HashSet<Mesh> meshes = new HashSet<Mesh>(); |
| 145 | 19 | | public Dictionary<Mesh, int> meshToTriangleCount = new Dictionary<Mesh, int>(); |
| 145 | 20 | | public HashSet<Renderer> renderers = new HashSet<Renderer>(); |
| 145 | 21 | | public HashSet<Material> materials = new HashSet<Material>(); |
| 145 | 22 | | public HashSet<Texture> textures = new HashSet<Texture>(); |
| 145 | 23 | | public HashSet<AnimationClip> animationClips = new HashSet<AnimationClip>(); |
| | 24 | | public int totalTriangleCount = 0; |
| | 25 | | public long animationClipSize = 0; |
| | 26 | | public long meshDataSize = 0; |
| | 27 | |
|
| | 28 | | Coroutine showCoroutine; |
| | 29 | |
|
| 145 | 30 | | public Asset_GLTF() |
| | 31 | | { |
| 145 | 32 | | container = new GameObject(CONTAINER_GO_NAME); |
| | 33 | | // Hide gameobject until it's been correctly processed, otherwise it flashes at 0,0,0 |
| 145 | 34 | | container.transform.position = EnvironmentSettings.MORDOR; |
| 145 | 35 | | } |
| | 36 | |
|
| | 37 | | public override object Clone() |
| | 38 | | { |
| 152 | 39 | | Asset_GLTF result = this.MemberwiseClone() as Asset_GLTF; |
| 152 | 40 | | result.visible = true; |
| 152 | 41 | | result.meshes = new HashSet<Mesh>(meshes); |
| 152 | 42 | | result.renderers = new HashSet<Renderer>(renderers); |
| 152 | 43 | | result.materials = new HashSet<Material>(materials); |
| 152 | 44 | | result.textures = new HashSet<Texture>(textures); |
| 152 | 45 | | result.meshToTriangleCount = new Dictionary<Mesh, int>(meshToTriangleCount); |
| 152 | 46 | | result.animationClips = new HashSet<AnimationClip>(animationClips); |
| | 47 | |
|
| 152 | 48 | | return result; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public override void Cleanup() |
| | 52 | | { |
| | 53 | | #if UNITY_STANDALONE || UNITY_EDITOR |
| 189 | 54 | | if (DataStore.i.common.isApplicationQuitting.Get()) |
| 0 | 55 | | return; |
| | 56 | | #endif |
| | 57 | |
|
| 189 | 58 | | Object.Destroy(container); |
| 189 | 59 | | } |
| | 60 | |
|
| | 61 | | public void Hide() |
| | 62 | | { |
| 3 | 63 | | if (container != null) |
| | 64 | | { |
| 3 | 65 | | container.transform.parent = null; |
| 3 | 66 | | container.transform.position = EnvironmentSettings.MORDOR; |
| | 67 | | } |
| | 68 | |
|
| 3 | 69 | | visible = false; |
| 3 | 70 | | } |
| | 71 | |
|
| | 72 | | public void CancelShow() |
| | 73 | | { |
| 28 | 74 | | if (showCoroutine != null) |
| 1 | 75 | | CoroutineStarter.Stop(showCoroutine); |
| 28 | 76 | | } |
| | 77 | |
|
| | 78 | | public void Show(bool useMaterialTransition, System.Action OnFinish) |
| | 79 | | { |
| 35 | 80 | | if (showCoroutine != null) |
| 0 | 81 | | CoroutineStarter.Stop(showCoroutine); |
| | 82 | |
|
| 35 | 83 | | if (!visible) |
| | 84 | | { |
| 0 | 85 | | OnFinish?.Invoke(); |
| 0 | 86 | | return; |
| | 87 | | } |
| | 88 | |
|
| 35 | 89 | | bool renderingEnabled = CommonScriptableObjects.rendererState.Get(); |
| | 90 | |
|
| 35 | 91 | | if (!renderingEnabled || !useMaterialTransition) |
| | 92 | | { |
| 12 | 93 | | container.SetActive(true); |
| 12 | 94 | | OnFinish?.Invoke(); |
| 12 | 95 | | return; |
| | 96 | | } |
| | 97 | |
|
| 23 | 98 | | showCoroutine = CoroutineStarter.Start(ShowCoroutine(OnFinish)); |
| 23 | 99 | | } |
| | 100 | |
|
| | 101 | | public IEnumerator ShowCoroutine(System.Action OnFinish) |
| | 102 | | { |
| | 103 | | // NOTE(Brian): This fixes seeing the object in the scene 0,0 for a frame |
| 23 | 104 | | yield return new WaitForSeconds(Random.Range(0, 0.05f)); |
| | 105 | |
|
| | 106 | | // NOTE(Brian): This GameObject can be removed by distance after the delay |
| 22 | 107 | | if (container == null) |
| | 108 | | { |
| 0 | 109 | | OnFinish?.Invoke(); |
| 0 | 110 | | yield break; |
| | 111 | | } |
| | 112 | |
|
| 22 | 113 | | container?.SetActive(true); |
| 22 | 114 | | OnFinish?.Invoke(); |
| 22 | 115 | | } |
| | 116 | | } |
| | 117 | | } |