Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fixed gltf loading in editor (#15)
Browse files Browse the repository at this point in the history
# XRTK - Mixed Reality Toolkit Pull Request

## Overview
<!-- Please provide a clear and concise description of the pull request. -->
Fixed gltf loading in editor

## Changes
<!-- Brief list of the targeted features that are being changed. -->

- Fixes: #14
  • Loading branch information
StephenHodgson authored Sep 4, 2021
1 parent 5bbe8a5 commit 18e3ac6
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 57 deletions.
8 changes: 0 additions & 8 deletions XRTK.glTF/Assets/XRTK.Generated.meta

This file was deleted.

8 changes: 0 additions & 8 deletions XRTK.glTF/Assets/XRTK.Generated/BuildInfo.meta

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.IO;
using UnityEditor;
using UnityEngine;
using XRTK.Utilities.Gltf.Schema;

namespace XRTK.Utilities.Gltf.Serialization.Importers
{
Expand All @@ -33,11 +32,11 @@ public static async void OnImportGltfAsset(AssetImportContext context)
context.SetMainObject(importedObject.GameObjectReference);
context.AddObjectToAsset("glTF data", gltfAsset);

bool reImport = false;
var reImport = false;

for (var i = 0; i < gltfAsset.GltfObject.textures?.Length; i++)
{
GltfTexture gltfTexture = gltfAsset.GltfObject.textures[i];
var gltfTexture = gltfAsset.GltfObject.textures[i];

if (gltfTexture == null) { continue; }

Expand All @@ -57,53 +56,47 @@ public static async void OnImportGltfAsset(AssetImportContext context)
}
else
{
if (!gltfTexture.Texture.isReadable)
if (gltfTexture.Texture.isReadable) { continue; }

var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
Debug.Assert(textureImporter != null);
textureImporter.isReadable = true;
textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings
{
var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
Debug.Assert(textureImporter != null);
textureImporter.isReadable = true;
textureImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings { format = TextureImporterFormat.RGBA32 });
textureImporter.SaveAndReimport();
reImport = true;
}
format = TextureImporterFormat.RGBA32
});
textureImporter.SaveAndReimport();
reImport = true;
}
}

if (reImport)
{
var importer = AssetImporter.GetAtPath(context.assetPath);
importer.SaveAndReimport();
AssetDatabase.SaveAssets();
return;
}

for (var i = 0; i < gltfAsset.GltfObject.meshes?.Length; i++)
{
GltfMesh gltfMesh = gltfAsset.GltfObject.meshes[i];
var gltfMesh = gltfAsset.GltfObject.meshes[i];

string meshName = string.IsNullOrWhiteSpace(gltfMesh.name) ? $"Mesh_{i}" : gltfMesh.name;
var meshName = string.IsNullOrWhiteSpace(gltfMesh.name) ? $"Mesh_{i}" : gltfMesh.name;

gltfMesh.Mesh.name = meshName;
context.AddObjectToAsset($"{meshName}", gltfMesh.Mesh);
}

if (gltfAsset.GltfObject.materials != null)
{
foreach (GltfMaterial gltfMaterial in gltfAsset.GltfObject.materials)
foreach (var gltfMaterial in gltfAsset.GltfObject.materials)
{
if (context.assetPath.EndsWith(".glb"))
{
context.AddObjectToAsset(gltfMaterial.name, gltfMaterial.Material);
}
else
{
var path = Path.GetFullPath(Path.GetDirectoryName(context.assetPath));
path = path.Replace("\\", "/").Replace(Application.dataPath, "Assets");
path = $"{path}/{gltfMaterial.name}.mat";
AssetDatabase.CreateAsset(gltfMaterial.Material, path);
gltfMaterial.Material = AssetDatabase.LoadAssetAtPath<Material>(path);
}
context.AddObjectToAsset(gltfMaterial.name, gltfMaterial.Material);
}
}

AssetDatabase.SaveAssets();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Rendering;
Expand Down Expand Up @@ -96,8 +97,19 @@ private static async Task ConstructBufferView(this GltfObject gltfObject, GltfBu
!string.IsNullOrEmpty(bufferView.Buffer.uri))
{
var path = gltfObject.Uri.PathFromURI();
var loadTask = Rest.GetAsync($"{path}{bufferView.Buffer.uri}");
var response = gltfObject.LoadAsynchronously ? await loadTask : loadTask.Result;
var fullPath = $"{path}{bufferView.Buffer.uri}";

Response response;

if (gltfObject.LoadAsynchronously)
{
response = await Rest.GetAsync(fullPath);
}
else
{
var data = File.ReadAllBytes(fullPath);
response = new Response(true, null, data, 200);
}

if (response.Successful)
{
Expand All @@ -121,15 +133,18 @@ private static async Task ConstructTextureAsync(this GltfObject gltfObject, Gltf
!string.IsNullOrEmpty(gltfImage.uri))
{
var path = gltfObject.Uri.PathFromURI();
var textureLoadTask = Rest.DownloadTextureAsync($"{path}{gltfImage.uri}");

if (gltfObject.LoadAsynchronously)
{
texture = await textureLoadTask;
texture = await Rest.DownloadTextureAsync($"{path}{gltfImage.uri}");
}
else
{
texture = textureLoadTask.GetAwaiter().GetResult();
#if UNITY_EDITOR
texture = UnityEditor.AssetDatabase.LoadAssetAtPath<Texture2D>($"{path}{gltfImage.uri}");
#else
throw new NotImplementedException();
#endif
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task<GltfObject> ImportGltfObjectFromPathAsync(string uri, b
{
if (!SyncContextUtility.IsMainThread)
{
Debug.LogError("ImportGltfObjectFromPathAsync must be called from the main thread!");
Debug.LogError($"{nameof(ImportGltfObjectFromPathAsync)} must be called from the main thread!");
return null;
}

Expand Down Expand Up @@ -87,10 +87,25 @@ public static async Task<GltfObject> ImportGltfObjectFromPathAsync(string uri, b
return null;
}

var loadResourceTask = Rest.GetAsync(gltfPath);
var response = loadAsynchronously
? await loadResourceTask
: loadResourceTask.Result;
Response response;

if (loadAsynchronously)
{
response = await Rest.GetAsync(gltfPath);
}
else
{
try
{
var json = File.ReadAllText(gltfPath);
var data = File.ReadAllBytes(gltfPath);
response = new Response(true, json, data, 200);
}
catch (Exception e)
{
response = new Response(false, $"{e}", null, 0);
}
}

if (!response.Successful)
{
Expand Down
2 changes: 1 addition & 1 deletion XRTK.glTF/Packages/com.xrtk.gltf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"author": "XRTK Team (https://github.com/XRTK)",
"dependencies": {
"com.xrtk.core": "0.3.0-preview.10",
"com.xrtk.core": "0.3.0-preview.22",
"com.unity.nuget.newtonsoft-json": "2.0.1-preview.1"
}
}
4 changes: 2 additions & 2 deletions XRTK.glTF/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"com.unity.2d.tilemap": "1.0.0",
"com.unity.ide.rider": "1.2.1",
"com.unity.ide.visualstudio": "2.0.11",
"com.unity.ide.vscode": "1.2.3",
"com.unity.test-framework": "1.1.27",
"com.unity.ide.vscode": "1.2.4",
"com.unity.test-framework": "1.1.29",
"com.unity.textmeshpro": "2.1.4",
"com.unity.ugui": "1.0.0",
"com.unity.modules.ai": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions XRTK.glTF/ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 2019.4.28f1
m_EditorVersionWithRevision: 2019.4.28f1 (1381962e9d08)
m_EditorVersion: 2019.4.30f1
m_EditorVersionWithRevision: 2019.4.30f1 (e8c891080a1f)

0 comments on commit 18e3ac6

Please sign in to comment.