Skip to content

Commit

Permalink
feat: basisu support
Browse files Browse the repository at this point in the history
  • Loading branch information
daverin committed Nov 12, 2020
1 parent 695ab4d commit 334f10b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
8 changes: 5 additions & 3 deletions GLTFUtility.asmdef
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 29 additions & 12 deletions Scripts/Spec/GLTFImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Texture2D> onFinish, Action<float> onProgress = null) {
public IEnumerator CreateTextureAsync(bool linear, Action<Texture2D> onFinish, string mimeType, Action<float> onProgress = null) {
if (!string.IsNullOrEmpty(path)) {
#if UNITY_EDITOR
// Load textures from asset database if we can
Expand Down Expand Up @@ -62,7 +66,7 @@ public IEnumerator CreateTextureAsync(bool linear, Action<Texture2D> 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);
Expand All @@ -71,14 +75,27 @@ public IEnumerator CreateTextureAsync(bool linear, Action<Texture2D> 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<byte>(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<ImportResult[]> {
public ImportTask(List<GLTFImage> images, string directoryRoot, GLTFBufferView.ImportTask bufferViewTask) : base(bufferViewTask) {
Expand All @@ -93,19 +110,19 @@ public ImportTask(List<GLTFImage> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Spec/GLTFTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ImportResult(GLTFImage.ImportResult image) {
/// <summary> Create or return cached texture </summary>
public IEnumerator GetTextureCached(bool linear, Action<Texture2D> onFinish, Action<float> 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);
Expand Down

0 comments on commit 334f10b

Please sign in to comment.