Skip to content

Commit

Permalink
Fixed issue with jpeg texture formats and added more aggressive exit …
Browse files Browse the repository at this point in the history
…paths.
  • Loading branch information
Kinerius committed Mar 15, 2023
1 parent a8972d5 commit 267f68e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using GLTFast;
using GLTFast.Logging;
using GLTFast.Materials;
using System.Threading;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
Expand Down Expand Up @@ -212,11 +211,11 @@ private async Task<bool> ImportAllGltf()

foreach (GltfImportSettings gltf in gltfToWait)
{
var gltfUrl = gltf.url;
var gltfImport = gltf.import;

try
{
var gltfUrl = gltf.url;
var gltfImport = gltf.import;

EditorUtility.DisplayProgressBar("Asset Bundle Converter", $"Loading GLTF {gltfUrl}",
loadedGltf / (float)totalGltfToLoad);

Expand Down Expand Up @@ -287,6 +286,7 @@ private async Task<bool> ImportAllGltf()
if (importedGameObject == null)
{
var message = "Fatal error when importing this object, check previous error messages";
log.Error(message);
errorReporter.ReportError(message, settings);
Utils.Exit((int)ErrorCodes.GLTFAST_CRITICAL_ERROR);
break;
Expand All @@ -297,7 +297,6 @@ private async Task<bool> ImportAllGltf()
var message = $"Failed to get the gltf importer for {gltfUrl} \nPath: {relativePath}";
log.Error(message);
errorReporter.ReportError(message, settings);

EditorUtility.ClearProgressBar();
continue;
}
Expand Down Expand Up @@ -326,9 +325,11 @@ private async Task<bool> ImportAllGltf()
}
catch (Exception e)
{
log.Exception(e.ToString());
log.Error("UNCAUGHT FATAL: Failed to load GLTF " + gltf.AssetPath.hash);
Debug.LogException(e);
errorReporter.ReportException(new ConversionException(ConversionStep.Import, settings, e));
continue;
Utils.Exit((int)ErrorCodes.GLTFAST_CRITICAL_ERROR);
break;
}
}

Expand Down Expand Up @@ -394,7 +395,13 @@ private void CreateMaterialsForThisGltf(List<Texture2D> textures, GltfImportSett
else
{
if (prevTexture == null)
log.Error($"Failed to set texture \"{texName}\" to material \"{matName}\". This will cause white materials");
{
var message = $"Failed to set texture \"{texName}\" to material \"{matName}\". This will cause white materials";
log.Error(message);
errorReporter.ReportError(message, settings);
Utils.Exit((int)ErrorCodes.GLTFAST_CRITICAL_ERROR);
return;
}
}
}

Expand Down Expand Up @@ -426,36 +433,45 @@ private List<Texture2D> CreateTextureAssets(List<Texture2D> textures, string fol
for (int i = 0; i < textures.Count; i++)
{
var tex = textures[i];
var ext = ".png";
string texName = tex.name;
texName = Utils.NicifyName(texName);

var texPath = string.Concat(texturesRoot, texName);

// some textures might already contain the extension, adding it a second time confuses unity into an exception
if (!texPath.EndsWith(".png"))
texPath = string.Concat(texPath, ext);

texPath = texPath.Replace('\\', '/');

var absolutePath = $"{Application.dataPath}/../{texPath}";
var texturePath = AssetDatabase.GetAssetPath(tex);

if (File.Exists(absolutePath))
{
newTextures.Add(AssetDatabase.LoadAssetAtPath<Texture2D>(PathUtils.GetRelativePathTo(Application.dataPath, absolutePath)));
continue;
Texture2D loadedAsset = AssetDatabase.LoadAssetAtPath<Texture2D>(PathUtils.GetRelativePathTo(Application.dataPath, absolutePath));

if (loadedAsset != null)
{
newTextures.Add(loadedAsset);
continue;
}
}

if (!string.IsNullOrEmpty(texturePath))
{
newTextures.Add(AssetDatabase.LoadAssetAtPath<Texture2D>(texPath));
continue;
Texture2D loadedAsset = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);

if (loadedAsset != null)
{
newTextures.Add(loadedAsset);
continue;
}
}

if (!Directory.Exists(texturesRoot))
Directory.CreateDirectory(texturesRoot);

// We are always encoding to PNG
if (!Path.HasExtension(texPath))
texPath += ".png";

if (tex.isReadable) { File.WriteAllBytes(texPath, tex.EncodeToPNG()); }
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ public override void OnImportAsset(AssetImportContext ctx)
SetupCustomMaterialGenerator(new AssetBundleConverterMaterialGenerator());

try { base.OnImportAsset(ctx); }
catch (Exception e) { Debug.LogException(e); }
catch (Exception e)
{
Debug.LogError("UNCAUGHT FATAL: Failed to Import GLTF " + hash);
Debug.LogException(e);
Utils.Exit((int)DCL.ABConverter.AssetBundleConverter.ErrorCodes.GLTFAST_CRITICAL_ERROR);
}
}

protected override void CreateMaterialAssets(AssetImportContext ctx)
Expand Down Expand Up @@ -166,15 +171,17 @@ private void FixTextureReferences(List<Texture2D> textures, string folderName, L
string texPath = AssetDatabase.GetAssetPath(tex);

if (string.IsNullOrEmpty(texPath))
{
texPath = $"{folderName}{separator}Textures{separator}{tex.name}";

texPath = texPath.Replace(separator, '/');
texPath = texPath.Replace(separator, '/');

//remove all whitespaces
texPath = Regex.Replace(texPath, @"\s+", "");
//remove all whitespaces
texPath = Regex.Replace(texPath, @"\s+", "");

if (!texPath.EndsWith(".png"))
texPath += ".png";
if (!Path.HasExtension(texPath))
texPath += ".png";
}

//var importedTex = AssetDatabase.LoadAssetAtPath<Texture2D>(texPath);
var importer = GetAtPath(texPath);
Expand Down Expand Up @@ -205,7 +212,10 @@ private void FixTextureReferences(List<Texture2D> textures, string folderName, L
EditorUtility.SetDirty(tImporter);
tImporter.SaveAndReimport();
}
else { Debug.LogError($"GLTFImporter: Unable to import texture at path: {texPath}"); }
else
{
throw new Exception($"GLTFImporter: Unable to import texture at path: {texPath}");
}
}
}
}
Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion asset-bundle-converter/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "b562146d739c6f99a76e2b4147a7e034e4344fa8"
"hash": "77a57521b9ee47969dfcdc529c01dc9077a69223"
},
"com.unity.modules.ai": {
"version": "1.0.0",
Expand Down

0 comments on commit 267f68e

Please sign in to comment.