From c89e5d77f0ee42d29c559903d7c55eebfd2bda0b Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 14 Mar 2024 19:07:31 +0900 Subject: [PATCH 1/2] Can import basisu texture at runtime import --- .../Importer/UnityTextureDeserializer.cs | 29 ++++++++++++++++++- .../Runtime/VRMShaders.GLTF.IO.Runtime.asmdef | 13 +++++++-- Packages/manifest.json | 4 ++- Packages/packages-lock.json | 18 +++++++++++- 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs index 14ce16d55..0306fa60a 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs @@ -1,6 +1,10 @@ using System; using System.Threading.Tasks; +using Unity.Collections; using UnityEngine; +#if USE_COM_UNITY_CLOUD_KTX +using KtxUnity; +#endif namespace VRMShaders { @@ -11,12 +15,32 @@ namespace VRMShaders { public async Task LoadTextureAsync(DeserializingTextureInfo textureInfo, IAwaitCaller awaitCaller) { + Texture2D texture = null; switch (textureInfo.DataMimeType) { case "image/png": break; case "image/jpeg": break; +#if USE_COM_UNITY_CLOUD_KTX + case "image/ktx": + var ktxTexture = new KtxTexture(); + var nativeBytes = new NativeArray(textureInfo.ImageData, Allocator.Temp); + try + { + var nativeSlice = new NativeSlice(nativeBytes); + var result = await ktxTexture.LoadFromBytes(nativeSlice, textureInfo.ColorSpace == ColorSpace.Linear); + if (result != null && result.errorCode == ErrorCode.Success) + { + texture = result.texture; + } + break; + } + finally + { + nativeBytes.Dispose(); + } +#endif default: if (string.IsNullOrEmpty(textureInfo.DataMimeType)) { @@ -29,7 +53,10 @@ namespace VRMShaders break; } - var texture = new Texture2D(2, 2, TextureFormat.ARGB32, textureInfo.UseMipmap, textureInfo.ColorSpace == ColorSpace.Linear); + if (texture == null) + { + texture = new Texture2D(2, 2, TextureFormat.ARGB32, textureInfo.UseMipmap, textureInfo.ColorSpace == ColorSpace.Linear); + } if (textureInfo.ImageData != null) { texture.LoadImage(textureInfo.ImageData); diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/VRMShaders.GLTF.IO.Runtime.asmdef b/Assets/VRMShaders/GLTF/IO/Runtime/VRMShaders.GLTF.IO.Runtime.asmdef index 54472abde..36313b162 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/VRMShaders.GLTF.IO.Runtime.asmdef +++ b/Assets/VRMShaders/GLTF/IO/Runtime/VRMShaders.GLTF.IO.Runtime.asmdef @@ -1,6 +1,9 @@ { "name": "VRMShaders.GLTF.IO.Runtime", - "references": [], + "rootNamespace": "", + "references": [ + "GUID:3d354272d3f2f4c3387dbccbaebd0f60" + ], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, @@ -8,6 +11,12 @@ "precompiledReferences": [], "autoReferenced": true, "defineConstraints": [], - "versionDefines": [], + "versionDefines": [ + { + "name": "com.unity.cloud.ktx", + "expression": "3.0.0", + "define": "USE_COM_UNITY_CLOUD_KTX" + } + ], "noEngineReferences": false } \ No newline at end of file diff --git a/Packages/manifest.json b/Packages/manifest.json index 0c08c7ad7..75c2d10b0 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -1,7 +1,9 @@ { "dependencies": { "com.unity.burst": "1.6.6", - "com.unity.ide.rider": "3.0.21", + "com.unity.cloud.ktx": "3.3.0", + "com.unity.cloud.ktx.webgl-2021": "1.0.1", + "com.unity.ide.rider": "3.0.24", "com.unity.ide.visualstudio": "2.0.18", "com.unity.ide.vscode": "1.2.5", "com.unity.postprocessing": "3.2.2", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index c31d5ce2f..5f17263d4 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -9,6 +9,22 @@ }, "url": "https://packages.unity.com" }, + "com.unity.cloud.ktx": { + "version": "3.3.0", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.modules.unitywebrequest": "1.0.0" + }, + "url": "https://packages.unity.com" + }, + "com.unity.cloud.ktx.webgl-2021": { + "version": "1.0.1", + "depth": 0, + "source": "registry", + "dependencies": {}, + "url": "https://packages.unity.com" + }, "com.unity.ext.nunit": { "version": "1.0.6", "depth": 1, @@ -17,7 +33,7 @@ "url": "https://packages.unity.com" }, "com.unity.ide.rider": { - "version": "3.0.21", + "version": "3.0.24", "depth": 0, "source": "registry", "dependencies": { From eff6d6edd01ce05bf5c751f06ed11da30f61d460 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 14 Mar 2024 19:11:48 +0900 Subject: [PATCH 2/2] fix --- .../Texture/Importer/UnityTextureDeserializer.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs index 0306fa60a..16fcdedb1 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/UnityTextureDeserializer.cs @@ -19,8 +19,13 @@ namespace VRMShaders switch (textureInfo.DataMimeType) { case "image/png": - break; case "image/jpeg": + texture = new Texture2D(2, 2, TextureFormat.ARGB32, textureInfo.UseMipmap, textureInfo.ColorSpace == ColorSpace.Linear); + if (textureInfo.ImageData != null) + { + texture.LoadImage(textureInfo.ImageData); + await awaitCaller.NextFrame(); + } break; #if USE_COM_UNITY_CLOUD_KTX case "image/ktx": @@ -53,19 +58,12 @@ namespace VRMShaders break; } - if (texture == null) + if (texture != null) { - texture = new Texture2D(2, 2, TextureFormat.ARGB32, textureInfo.UseMipmap, textureInfo.ColorSpace == ColorSpace.Linear); - } - if (textureInfo.ImageData != null) - { - texture.LoadImage(textureInfo.ImageData); texture.wrapModeU = textureInfo.WrapModeU; texture.wrapModeV = textureInfo.WrapModeV; texture.filterMode = textureInfo.FilterMode; - await awaitCaller.NextFrame(); } - return texture; } }