diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index fb6107190..aedfe0391 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -68,7 +68,12 @@ namespace UniGLTF /// IStorage _storage; - Dictionary> _dataUriCache = new Dictionary>(); + /// + /// uri = data: base64デコード + /// uri = 相対パス。File.ReadAllBytes + /// + /// + Dictionary> _UriCache = new Dictionary>(); public GltfData(string targetPath, string json, glTF gltf, IReadOnlyList chunks, IStorage storage, MigrationFlags migrationFlags) { @@ -101,39 +106,37 @@ namespace UniGLTF ); } - public ArraySegment GetBytesFromUri(string uri) + ArraySegment GetBytesFromUri(string uri) { if (string.IsNullOrEmpty(uri)) { throw new ArgumentNullException(); } + + if (_UriCache.TryGetValue(uri, out ArraySegment data)) + { + // return cache + return data; + } + if (uri.StartsWith("data:", StringComparison.Ordinal)) { - if (_dataUriCache.TryGetValue(uri, out ArraySegment data)) - { - return data; - } data = new ArraySegment(UriByteBuffer.ReadEmbedded(uri)); - _dataUriCache.Add(uri, data); - return data; } else { - return _storage.Get(uri); + data = _storage.Get(uri); } + _UriCache.Add(uri, data); + return data; } public ArraySegment GetBytesFromBuffer(int bufferIndex) { var buffer = GLTF.buffers[bufferIndex]; - if (bufferIndex == 0 && string.IsNullOrEmpty(buffer.uri)) + if (bufferIndex == 0 && Bin.Array != null) { // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#glb-stored-buffer - // this buffer is reference bin chunk - if (Bin.Array == null) - { - throw new NullReferenceException(); - } return Bin; } else