| | 1 | | using DCL.Helpers; |
| | 2 | | using UnityGLTF; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | public class AssetPromise_GLTF : AssetPromise<Asset_GLTF> |
| | 7 | | { |
| 122 | 8 | | public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering(); |
| | 9 | | protected string assetDirectoryPath; |
| | 10 | |
|
| | 11 | | protected ContentProvider provider = null; |
| 0 | 12 | | public string url { get; private set; } |
| | 13 | |
|
| | 14 | | GLTFComponent gltfComponent = null; |
| | 15 | | object id = null; |
| | 16 | |
|
| 122 | 17 | | public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null) |
| | 18 | | { |
| 122 | 19 | | this.provider = provider; |
| 122 | 20 | | this.url = url.Substring(url.LastIndexOf('/') + 1); |
| 122 | 21 | | this.id = hash ?? url; |
| | 22 | | // We separate the directory path of the GLB and its file name, to be able to use the directory path when |
| | 23 | | // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call |
| 122 | 24 | | assetDirectoryPath = URIHelper.GetDirectoryName(url); |
| 122 | 25 | | } |
| | 26 | |
|
| | 27 | | protected override void OnBeforeLoadOrReuse() |
| | 28 | | { |
| | 29 | | #if UNITY_EDITOR |
| 169 | 30 | | asset.container.name = "GLTF: " + this.id; |
| | 31 | | #endif |
| 169 | 32 | | settings.ApplyBeforeLoad(asset.container.transform); |
| 169 | 33 | | } |
| | 34 | |
|
| 168 | 35 | | protected override void OnAfterLoadOrReuse() { settings.ApplyAfterLoad(asset.container.transform); } |
| | 36 | |
|
| 732 | 37 | | public override object GetId() { return id; } |
| | 38 | |
|
| | 39 | | protected override void OnLoad(System.Action OnSuccess, System.Action OnFail) |
| | 40 | | { |
| 85 | 41 | | gltfComponent = asset.container.AddComponent<GLTFComponent>(); |
| 85 | 42 | | gltfComponent.Initialize(DCL.Environment.i.platform.webRequest); |
| | 43 | |
|
| 85 | 44 | | GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings() |
| | 45 | | { |
| | 46 | | useVisualFeedback = settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TR |
| | 47 | | initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE, |
| | 48 | | shaderOverride = settings.shaderOverride, |
| | 49 | | addMaterialsToPersistentCaching = (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) ! |
| | 50 | | }; |
| | 51 | |
|
| 85 | 52 | | tmpSettings.OnWebRequestStartEvent += ParseGLTFWebRequestedFile; |
| | 53 | |
|
| 85 | 54 | | gltfComponent.LoadAsset(url, GetId() as string, false, tmpSettings); |
| 85 | 55 | | gltfComponent.OnSuccess += OnSuccess; |
| 85 | 56 | | gltfComponent.OnFail += OnFail; |
| | 57 | |
|
| 85 | 58 | | asset.name = url; |
| 85 | 59 | | } |
| | 60 | |
|
| 188 | 61 | | void ParseGLTFWebRequestedFile(ref string requestedFileName) { provider.TryGetContentsUrl(assetDirectoryPath + r |
| | 62 | |
|
| | 63 | | protected override void OnReuse(System.Action OnSuccess) |
| | 64 | | { |
| | 65 | | //NOTE(Brian): Show the asset using the simple gradient feedback. |
| 26 | 66 | | asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS |
| 26 | 67 | | } |
| | 68 | |
|
| | 69 | | protected override bool AddToLibrary() |
| | 70 | | { |
| 60 | 71 | | if (!library.Add(asset)) |
| 0 | 72 | | return false; |
| | 73 | |
|
| 60 | 74 | | if (!asset.visible) |
| 2 | 75 | | return true; |
| | 76 | |
|
| | 77 | | //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately |
| | 78 | | // So it keeps being there. As master gltfs can't be in the world. |
| | 79 | | // |
| | 80 | | // ApplySettings will reposition the newly Get asset to the proper coordinates. |
| 58 | 81 | | if (settings.forceNewInstance) |
| | 82 | | { |
| 2 | 83 | | asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id); |
| 2 | 84 | | } |
| | 85 | | else |
| | 86 | | { |
| 56 | 87 | | asset = library.Get(asset.id); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | //NOTE(Brian): Call again this method because we are replacing the asset. |
| 58 | 91 | | OnBeforeLoadOrReuse(); |
| 58 | 92 | | return true; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | protected override void OnCancelLoading() |
| | 96 | | { |
| 27 | 97 | | if (asset != null) |
| 27 | 98 | | asset.CancelShow(); |
| 27 | 99 | | } |
| | 100 | |
|
| | 101 | | protected override Asset_GLTF GetAsset(object id) |
| | 102 | | { |
| 26 | 103 | | if (settings.forceNewInstance) |
| | 104 | | { |
| 9 | 105 | | return ((AssetLibrary_GLTF) library).GetCopyFromOriginal(id); |
| | 106 | | } |
| | 107 | | else |
| | 108 | | { |
| 17 | 109 | | return base.GetAsset(id); |
| | 110 | | } |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |