This commit is contained in:
左右看
2023-06-28 00:35:46 +08:00
parent cb5bab5ce9
commit 0d848e4d07
524 changed files with 78452 additions and 12 deletions
@@ -0,0 +1,27 @@
#if UNITY_ANIMATION || !UNITY_2019_1_OR_NEWER
using GLTF;
using GLTF.Schema;
namespace UnityGLTF.Cache
{
public struct AnimationSamplerCacheData
{
public AttributeAccessor Input;
public AttributeAccessor Output;
public InterpolationType Interpolation;
}
public class AnimationCacheData
{
public UnityEngine.AnimationClip LoadedAnimationClip { get; set; }
public AnimationSamplerCacheData[] Samplers { get; set; }
public AnimationCacheData(int samplerCount)
{
Samplers = new AnimationSamplerCacheData[samplerCount];
}
}
}
#endif
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5eb5ec69ffa30384786436879a3167a3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using GLTF.Schema;
namespace UnityGLTF.Cache
{
/// <summary>
/// Caches data in order to construct a unity object
/// </summary>
public class AssetCache : IDisposable
{
/// <summary>
/// Streams to the images to be loaded
/// </summary>
public Stream[] ImageStreamCache { get; private set; }
/// <summary>
/// Loaded raw texture data
/// </summary>
public Texture2D[] ImageCache { get; private set; }
/// <summary>
/// Textures to be used for assets. Textures from image cache with samplers applied
/// </summary>
public TextureCacheData[] TextureCache { get; private set; }
/// <summary>
/// Cache for materials to be applied to the meshes
/// </summary>
public MaterialCacheData[] MaterialCache { get; private set; }
/// <summary>
/// Byte buffers that represent the binary contents that get parsed
/// </summary>
public BufferCacheData[] BufferCache { get; private set; }
/// <summary>
/// Cache of loaded meshes
/// </summary>
public MeshCacheData[] MeshCache { get; private set; }
#if UNITY_ANIMATION || !UNITY_2019_1_OR_NEWER
/// <summary>
/// Cache of loaded animations
/// </summary>
public AnimationCacheData[] AnimationCache { get; private set; }
#endif
/// <summary>
/// Cache of loaded node objects
/// </summary>
public GameObject[] NodeCache { get; private set; }
/// <summary>
/// Creates an asset cache which caches objects used in scene
/// </summary>
/// <param name="root">A glTF root whose assets will eventually be cached here</param>
public AssetCache(GLTFRoot root)
{
ImageCache = new Texture2D[root.Images?.Count ?? 0];
ImageStreamCache = new Stream[ImageCache.Length];
TextureCache = new TextureCacheData[root.Textures?.Count ?? 0];
MaterialCache = new MaterialCacheData[root.Materials?.Count ?? 0];
BufferCache = new BufferCacheData[root.Buffers?.Count ?? 0];
MeshCache = new MeshCacheData[root.Meshes?.Count ?? 0];
NodeCache = new GameObject[root.Nodes?.Count ?? 0];
#if UNITY_ANIMATION || !UNITY_2019_1_OR_NEWER
AnimationCache = new AnimationCacheData[root.Animations?.Count ?? 0];
#endif
}
public void Dispose()
{
if (ImageStreamCache != null)
{
foreach (var stream in ImageStreamCache)
{
stream?.Dispose();
}
ImageStreamCache = null;
}
ImageCache = null;
TextureCache = null;
MaterialCache = null;
if (BufferCache != null)
{
foreach (BufferCacheData bufferCacheData in BufferCache)
{
if (bufferCacheData != null)
{
if (bufferCacheData.Stream != null)
{
#if !WINDOWS_UWP
bufferCacheData.Stream.Close();
#else
bufferCacheData.Stream.Dispose();
#endif
}
bufferCacheData.Dispose();
}
}
BufferCache = null;
}
MeshCache = null;
#if UNITY_ANIMATION || !UNITY_2019_1_OR_NEWER
AnimationCache = null;
#endif
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1bb782a30ee69ac43949c7b27404bb0a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,19 @@
using System;
namespace UnityGLTF.Cache
{
public class BufferCacheData : IDisposable
{
public uint ChunkOffset { get; set; }
public System.IO.Stream Stream { get; set; }
public void Dispose()
{
if (Stream != null)
{
Stream.Dispose();
Stream = null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3ede042e9106e314a9c2fc3727c636af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,110 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace UnityGLTF.Cache
{
internal static class ExportCache
{
public const int DefaultCacheSize = 1024;
#if UNITY_EDITOR
[InitializeOnLoadMethod]
private static void Init()
{
// Keep some files in cache that were last exported
EditorApplication.quitting += () => Shrink(DefaultCacheSize);
}
public static void OpenCacheDirectory()
{
var dir = CacheDirectory;
if(Directory.Exists(dir)) EditorUtility.RevealInFinder(dir);
else Debug.LogWarning($"Cache directory does not exist: {dir}");
}
#endif
public static string CacheDirectory
{
get
{
var tempDirectory = Path.Combine(Application.temporaryCachePath, "UnityGLTF");
return tempDirectory;
}
}
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 {bytes.Length} bytes to cache: {path}");
File.WriteAllBytes(path, bytes);
#endif
}
public static void Clear()
{
var dir = CacheDirectory;
if (Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
}
public static void Shrink(int maxCacheSizeInMB)
{
if (maxCacheSizeInMB <= 0)
{
Clear();
return;
}
var maxCacheSize = maxCacheSizeInMB * 1024 * 1024;
var files = new List<FileInfo>();
var currentSize = CalculateCacheSize(files);
if(currentSize <= maxCacheSizeInMB)
return;
var filesSortedByLastAccess = files.OrderBy(f => f.LastAccessTimeUtc).ToList();
foreach (var file in filesSortedByLastAccess)
{
file.Delete();
currentSize -= (int)file.Length;
if (currentSize <= maxCacheSize)
break;
}
}
public static long CalculateCacheSize(ICollection<FileInfo> files = null)
{
var dir = CacheDirectory;
if (!Directory.Exists(dir)) return 0;
var filePaths = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);
long size = 0;
foreach (var file in filePaths)
{
var info = new FileInfo(file);
files?.Add(info);
size += (long) info.Length;
}
return size;
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b67adc482fa84697bf30de7918e7d995
timeCreated: 1672777985
@@ -0,0 +1,36 @@
using GLTF.Schema;
using System;
using UnityEngine;
namespace UnityGLTF.Cache
{
public class MaterialCacheData : IDisposable
{
public Material UnityMaterial { get; set; }
public Material UnityMaterialWithVertexColor { get; set; }
public GLTFMaterial GLTFMaterial { get; set; }
public Material GetContents(bool useVertexColors)
{
return useVertexColors ? UnityMaterialWithVertexColor : UnityMaterial;
}
/// <summary>
/// Unloads the materials in this cache.
/// </summary>
public void Dispose()
{
if (UnityMaterial != null)
{
UnityEngine.Object.Destroy(UnityMaterial);
UnityMaterial = null;
}
if (UnityMaterialWithVertexColor != null)
{
UnityEngine.Object.Destroy(UnityMaterialWithVertexColor);
UnityMaterialWithVertexColor = null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3046febe680d4eb44b6851e256d7feca
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
using GLTF;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityGLTF.Cache
{
public class MeshCacheData : IDisposable
{
public class PrimitiveCacheData
{
public Dictionary<string, AttributeAccessor> Attributes = new Dictionary<string, AttributeAccessor>(4);
public List<Dictionary<string, AttributeAccessor>> Targets = new List<Dictionary<string, AttributeAccessor>>(4);
}
public List<PrimitiveCacheData> Primitives = new List<PrimitiveCacheData>(5);
public Mesh LoadedMesh { get; set; }
/// <summary>
/// Unloads the meshes in this cache.
/// </summary>
public void Dispose()
{
if (LoadedMesh != null)
{
UnityEngine.Object.Destroy(LoadedMesh);
LoadedMesh = null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7356479b6b440d943adc853e80ead936
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace UnityGLTF.Cache
{
/// <summary>
/// A ref-counted cache data object containing lists of Unity objects that were created for the sake of a GLTF scene/node.
/// This supports counting the amount of refcounts that will dispose of itself
/// </summary>
public class RefCountedCacheData
{
private bool _isDisposed = false;
/// <summary>
/// Ref count for this cache data.
/// </summary>
/// <remarks>
/// Initialized to 0. When assigning the cache data to an instantiated GLTF
/// object the count will increase.
/// </remarks>
private int _refCount = 0;
private readonly object _refCountLock = new object();
/// <summary>
/// Meshes used by this GLTF node.
/// </summary>
public MeshCacheData[] MeshCache { get; private set; }
/// <summary>
/// Materials used by this GLTF node.
/// </summary>
public MaterialCacheData[] MaterialCache { get; private set; }
/// <summary>
/// Textures used by this GLTF node.
/// </summary>
public TextureCacheData[] TextureCache { get; private set; }
/// <summary>
/// Textures from the AssetCache that might need to be cleaned up
/// </summary>
public Texture2D[] ImageCache { get; private set; }
public RefCountedCacheData(MaterialCacheData[] materialCache, MeshCacheData[] meshCache, TextureCacheData[] textureCache, Texture2D[] imageCache)
{
MaterialCache = materialCache;
MeshCache = meshCache;
TextureCache = textureCache;
ImageCache = imageCache;
}
public void IncreaseRefCount()
{
if (_isDisposed)
{
throw new InvalidOperationException("Cannot inscrease the ref count on disposed cache data.");
}
lock (_refCountLock)
{
_refCount++;
}
}
public void DecreaseRefCount()
{
if (_isDisposed)
{
throw new InvalidOperationException("Cannot decrease the ref count on disposed cache data.");
}
lock (_refCountLock)
{
if (_refCount <= 0)
{
throw new InvalidOperationException("Cannot decrease the cache data ref count below zero.");
}
_refCount--;
}
if (_refCount <= 0)
{
DestroyCachedData();
}
}
private void DestroyCachedData()
{
// Destroy the cached meshes
for (int i = 0; i < MeshCache.Length; i++)
{
MeshCache[i]?.Dispose();
MeshCache[i] = null;
}
// Destroy the cached textures
for (int i = 0; i < TextureCache.Length; i++)
{
TextureCache[i]?.Dispose();
TextureCache[i] = null;
}
// Destroy the cached materials
for (int i = 0; i < MaterialCache.Length; i++)
{
MaterialCache[i]?.Dispose();
MaterialCache[i] = null;
}
// Destroy the cached images
for (int i = 0; i < ImageCache.Length; i++)
{
if (ImageCache[i] != null)
{
UnityEngine.Object.Destroy(ImageCache[i]);
ImageCache[i] = null;
}
}
_isDisposed = true;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6acba157038c72d4db902024947d8f94
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,25 @@
using GLTF.Schema;
using System;
using UnityEngine;
namespace UnityGLTF.Cache
{
public class TextureCacheData : IDisposable
{
public GLTFTexture TextureDefinition;
public Texture2D Texture;
public bool IsLinear;
/// <summary>
/// Unloads the textures in this cache.
/// </summary>
public void Dispose()
{
if (Texture != null)
{
UnityEngine.Object.Destroy(Texture);
Texture = null;
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2d62214ca8670eb49aa365a9c8a3abd2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: