Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct .basisu image support #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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