diff --git a/GLTFUtility.asmdef b/GLTFUtility.asmdef index 113f0d8..35b938d 100644 --- a/GLTFUtility.asmdef +++ b/GLTFUtility.asmdef @@ -1,14 +1,16 @@ { "name": "Siccity.GLTFUtility", "references": [ - "Dracodec" + "Dracodec", + "Ktx" ], - "optionalUnityReferences": [], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [] + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false } \ No newline at end of file diff --git a/Scripts/Spec/GLTFImage.cs b/Scripts/Spec/GLTFImage.cs index 3d2c1e7..c078764 100644 --- a/Scripts/Spec/GLTFImage.cs +++ b/Scripts/Spec/GLTFImage.cs @@ -4,7 +4,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using KtxUnity; using Newtonsoft.Json; +using Unity.Collections; using UnityEngine; using UnityEngine.Networking; using UnityEngine.Scripting; @@ -27,13 +29,15 @@ [Preserve] public class GLTFImage { public class ImportResult { public byte[] bytes; public string path; + public string mimeType; - public ImportResult(byte[] bytes, string path = null) { + public ImportResult(byte[] bytes, string mimeType, string path = null) { this.bytes = bytes; this.path = path; + this.mimeType = mimeType; } - public IEnumerator CreateTextureAsync(bool linear, Action onFinish, Action onProgress = null) { + public IEnumerator CreateTextureAsync(bool linear, Action onFinish, string mimeType, Action onProgress = null) { if (!string.IsNullOrEmpty(path)) { #if UNITY_EDITOR // Load textures from asset database if we can @@ -62,7 +66,7 @@ public IEnumerator CreateTextureAsync(bool linear, Action onFinish, A if (onProgress != null) onProgress(1f); if (uwr.isNetworkError || uwr.isHttpError) { - Debug.LogError("GLTFImage.cs ToTexture2D() ERROR: " + uwr.error); + Debug.LogError("GLTFImage.cs ToTexture2D() ERROR: " + uwr.error+"- "+path); } else { Texture2D tex = DownloadHandlerTexture.GetContent(uwr); tex.name = Path.GetFileNameWithoutExtension(path); @@ -71,14 +75,27 @@ public IEnumerator CreateTextureAsync(bool linear, Action onFinish, A uwr.Dispose(); } } else { - Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, true, linear); - if (!tex.LoadImage(bytes)) { - Debug.Log("mimeType not supported"); - yield break; - } else onFinish(tex); + if(mimeType=="image/png"||mimeType=="image/jpg"||mimeType=="image/jpeg") { + Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, true, linear); + if (!tex.LoadImage(bytes)) { + yield break; + } else onFinish(tex); + } else if(mimeType=="image/basis") { + var nativeArrayBytes = new NativeArray(bytes,KtxNativeInstance.defaultAllocator); + var basisTex = new BasisUniversalTexture(); + basisTex.onTextureLoaded += delegate(Texture2D tex) { + onFinish(tex); + }; + IEnumerator en = basisTex.LoadBytesRoutine(nativeArrayBytes,false); + while (en.MoveNext()) { yield return null; }; + nativeArrayBytes.Dispose(); + } + else { + Debug.LogError("mimeType not supported"); + } } } - } + } public class ImportTask : Importer.ImportTask { public ImportTask(List images, string directoryRoot, GLTFBufferView.ImportTask bufferViewTask) : base(bufferViewTask) { @@ -93,19 +110,19 @@ public ImportTask(List images, string directoryRoot, GLTFBufferView.I if (File.Exists(fullUri)) { // If the file is found at fullUri, read it byte[] bytes = File.ReadAllBytes(fullUri); - Result[i] = new ImportResult(bytes, fullUri); + Result[i] = new ImportResult(bytes, fullUri,images[i].mimeType); } else if (images[i].uri.StartsWith("data:")) { // If the image is embedded, find its Base64 content and save as byte array string content = images[i].uri.Split(',').Last(); byte[] imageBytes = Convert.FromBase64String(content); - Result[i] = new ImportResult(imageBytes); + Result[i] = new ImportResult(imageBytes,images[i].mimeType); } } else if (images[i].bufferView.HasValue && !string.IsNullOrEmpty(images[i].mimeType)) { GLTFBufferView.ImportResult view = bufferViewTask.Result[images[i].bufferView.Value]; byte[] bytes = new byte[view.byteLength]; view.stream.Position = view.byteOffset; view.stream.Read(bytes, 0, view.byteLength); - Result[i] = new ImportResult(bytes); + Result[i] = new ImportResult(bytes,images[i].mimeType); } else { Debug.Log("Couldn't find texture at " + fullUri); } diff --git a/Scripts/Spec/GLTFTexture.cs b/Scripts/Spec/GLTFTexture.cs index 5ecfdff..1968d15 100644 --- a/Scripts/Spec/GLTFTexture.cs +++ b/Scripts/Spec/GLTFTexture.cs @@ -25,7 +25,7 @@ public ImportResult(GLTFImage.ImportResult image) { /// Create or return cached texture public IEnumerator GetTextureCached(bool linear, Action onFinish, Action onProgress = null) { if (cache == null) { - IEnumerator en = image.CreateTextureAsync(linear, x => cache = x, onProgress); + IEnumerator en = image.CreateTextureAsync(linear, x => cache = x, image.mimeType, onProgress); while (en.MoveNext()) { yield return null; }; } onFinish(cache);