Skip to content

Commit

Permalink
Add image bytes cache
Browse files Browse the repository at this point in the history
  • Loading branch information
marwie committed Jan 3, 2023
1 parent c4f2741 commit d131751
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
53 changes: 53 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/Cache/ExportCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using UnityEditor;
using UnityEngine;

namespace UnityGLTF.Cache
{
internal static class ExportCache
{
private static string CacheDirectory
{
get
{
var tempDirectory = Path.Combine(Application.temporaryCachePath, "UnityGLTF");
return tempDirectory;
}
}

public static void Clear()
{
var dir = CacheDirectory;
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
}

public static bool TryGetBytes(Object asset, string seed, out byte[] bytes)
{
#if UNITY_EDITOR
var path = CacheDirectory + "/" + GlobalObjectId.GetGlobalObjectIdSlow(asset) + seed;
if (File.Exists(path))
{
bytes = File.ReadAllBytes(path);
return true;
}
#endif

bytes = null;
return false;
}

public static void AddBytes(Object asset, string seed, byte[] bytes)
{
#if UNITY_EDITOR
var dir = CacheDirectory;
Directory.CreateDirectory(dir);
var path = dir + "/" + GlobalObjectId.GetGlobalObjectIdSlow(asset) + seed;
Debug.Log("Writing to cache: " + path);
File.WriteAllBytes(path, bytes);
#endif
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GLTF.Schema;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityGLTF.Cache;
using UnityGLTF.Extensions;
using Object = UnityEngine.Object;
using WrapMode = GLTF.Schema.WrapMode;
Expand Down Expand Up @@ -138,7 +139,6 @@ private void WriteRenderTextureToDiskAndRelease(RenderTexture destRenderTexture,
Object.Destroy(exportTexture);
}


public TextureInfo ExportTextureInfo(Texture texture, string textureSlot, TextureExportSettings exportSettings = default)
{
var info = new TextureInfo();
Expand Down Expand Up @@ -591,7 +591,7 @@ private ImageId ExportImageInternalBuffer(UniqueTexture uniqueTexture, string te
exportTexture.Apply();

var canExportAsJpeg = !textureHasAlpha && settings.UseTextureFileTypeHeuristic;
var imageData = canExportAsJpeg ? exportTexture.EncodeToJPG(settings.DefaultJpegQuality) : exportTexture.EncodeToPNG();
var imageData = GetImageData(exportTexture, canExportAsJpeg);
image.MimeType = canExportAsJpeg ? JPEGMimeType : PNGMimeType;
_bufferWriter.Write(imageData);

Expand Down Expand Up @@ -632,6 +632,16 @@ private ImageId ExportImageInternalBuffer(UniqueTexture uniqueTexture, string te
return id;
}

private byte[] GetImageData(Texture2D texture, bool exportAsJpg)
{
var seed = texture.imageContentsHash.ToString();
if(ExportCache.TryGetBytes(texture, seed, out var bytes))
return bytes;
bytes = exportAsJpg ? texture.EncodeToJPG(settings.DefaultJpegQuality) : texture.EncodeToPNG();
ExportCache.AddBytes(texture, seed, bytes);
return bytes;
}

private SamplerId ExportSampler(Texture texture)
{
var samplerId = GetSamplerId(_root, texture);
Expand Down

0 comments on commit d131751

Please sign in to comment.