Skip to content

Commit

Permalink
fix remapping issues, fix handling of non-loadable extension textures
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Jul 16, 2023
1 parent ebd3fbc commit ece55e1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
49 changes: 27 additions & 22 deletions UnityGLTF/Assets/UnityGLTF/Editor/Scripts/GLTFImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,28 +614,31 @@ string GetUniqueName(string desiredName)
m_HasMaterialData = importer.Root.Materials != null && importer.Root.Materials.Count > 0;
m_HasTextureData = importer.Root.Textures != null && importer.Root.Textures.Count > 0;
m_HasAnimationData = importer.Root.Animations != null && importer.Root.Animations.Count > 0;
var newAnimations = new AnimationClipImportInfo[animations.Length];
for (var i = 0; i < animations.Length; ++i)
{
// get previous import info if it exists
// TODO won't work if there are multiple animations with the same name in the source
// We need to find the source instance index (e.g. 3rd time a clip is named "DoSomething")
// And then find the matching name index (also the 3rd occurrence of "DoSomething" in m_Animations)
var prev = Array.Find(m_Animations, x => x.name == animations[i].name);
if (prev != null)
{
newAnimations[i] = prev;
}
else
{
var newClipInfo = new AnimationClipImportInfo();
newClipInfo.name = animations[i].name;
// newClipInfo.startTime = 0;
// newClipInfo.endTime = animations[i].length;
// newClipInfo.loopTime = true;
newAnimations[i] = newClipInfo;
}
}
var newAnimations = new AnimationClipImportInfo[animations != null ? animations.Length : 0];
if (animations != null)
{
for (var i = 0; i < animations.Length; ++i)
{
// get previous import info if it exists
// TODO won't work if there are multiple animations with the same name in the source
// We need to find the source instance index (e.g. 3rd time a clip is named "DoSomething")
// And then find the matching name index (also the 3rd occurrence of "DoSomething" in m_Animations)
var prev = Array.Find(m_Animations, x => x.name == animations[i].name);
if (prev != null)
{
newAnimations[i] = prev;
}
else
{
var newClipInfo = new AnimationClipImportInfo();
newClipInfo.name = animations[i].name;
// newClipInfo.startTime = 0;
// newClipInfo.endTime = animations[i].length;
// newClipInfo.loopTime = true;
newAnimations[i] = newClipInfo;
}
}
}
m_Animations = newAnimations;

#if !UNITY_2022_1_OR_NEWER
Expand Down Expand Up @@ -824,6 +827,8 @@ public override bool SupportsRemappedAssetType(Type type)
{
if (type == typeof(Material))
return true;
if (type == typeof(Texture))
return true;
return base.SupportsRemappedAssetType(type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn
#endif
Texture2D texture = new Texture2D(4, 4, TextureFormat.RGBA32, GenerateMipMapsForTextures, isLinear);
texture.name = string.IsNullOrEmpty(image.Name) ? Path.GetFileNameWithoutExtension(image.Uri) : image.Name;
var newTextureObject = texture;

#if UNITY_EDITOR
var haveRemappedTexture = false;
Expand All @@ -185,8 +186,15 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn
externalObjects.TryGetValue(sourceIdentifier, out var o);
if (o is Texture2D remappedTexture)
{
texture = remappedTexture;
haveRemappedTexture = true;
if (remappedTexture)
{
texture = remappedTexture;
haveRemappedTexture = true;
}
else
{
Context.SourceImporter.RemoveRemap(sourceIdentifier);
}
}
}

Expand All @@ -204,11 +212,8 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn
// TODO we should still set it to null here, and save the importer definition names for remapping instead.
// This way here we'll get into weird code for Runtime import, as we would still import mock textures...
// Or we add another option to avoid that.

_assetCache.InvalidImageCache[imageCacheIndex] = texture;
texture = null;

UnityEngine.Debug.LogError("Buffer file " + invalidStream.RelativeFilePath + " not found in " + invalidStream.RootDirectory + ", complete path: " + invalidStream.AbsoluteFilePath);
UnityEngine.Debug.LogError("Buffer file " + invalidStream.RelativeFilePath + " not found in path: " + invalidStream.AbsoluteFilePath);

}
else if (stream is MemoryStream)
Expand All @@ -234,6 +239,9 @@ protected virtual async Task ConstructUnityTexture(Stream stream, bool markGpuOn
texture = await CheckMimeTypeAndLoadImage(image, texture, buffer, markGpuOnly);
}

if (!texture)
_assetCache.InvalidImageCache[imageCacheIndex] = newTextureObject;

if (_assetCache.ImageCache[imageCacheIndex] != null) Debug.Log(LogType.Assert, "ImageCache should not be loaded multiple times");
if (texture)
{
Expand Down

0 comments on commit ece55e1

Please sign in to comment.