From 5a20825c41ff1844cdb20e694ec91d35bbb886d7 Mon Sep 17 00:00:00 2001 From: Mike Nisbet Date: Wed, 4 Dec 2024 16:49:43 +0000 Subject: [PATCH] Fix null bufferview with zero'd data --- Runtime/Scripts/GltfImport.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Runtime/Scripts/GltfImport.cs b/Runtime/Scripts/GltfImport.cs index 828edcad..f4478ee1 100755 --- a/Runtime/Scripts/GltfImport.cs +++ b/Runtime/Scripts/GltfImport.cs @@ -1872,6 +1872,21 @@ async Task LoadGltfBinaryBuffer(byte[] bytes, Uri uri = null) return true; } + unsafe void* GetZeroBuffer(int bytes) + { + byte[] zeroBuffer = new byte[bytes]; + // Fill with zeros + for (int i = 0; i < bytes; i++) + { + zeroBuffer[i] = 0; + } + + fixed (byte* bufferAddress = &zeroBuffer[0]) + { + return bufferAddress; + } + } + byte[] GetBuffer(int index) { return m_Buffers[index]; @@ -4105,8 +4120,14 @@ unsafe void IGltfBuffers.GetAccessor(int index, out AccessorBase accessor, out v accessor = Root.Accessors[index]; if (accessor.bufferView < 0 || accessor.bufferView >= Root.BufferViews.Count) { - data = null; - byteStride = 0; + if (accessor.IsSparse) + { + data = null; + byteStride = 0; + return; + } + data = GetZeroBuffer(accessor.ByteSize); + byteStride = AccessorBase.GetComponentTypeSize(accessor.componentType); return; } var bufferView = Root.BufferViews[accessor.bufferView];