Merge pull request #1748 from ousttrue/fix/negative_index

-1 を index の無効値に使っている場合の対策
This commit is contained in:
ousttrue 2022-07-19 19:03:36 +09:00 committed by GitHub
commit abb3d384d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 3 deletions

View File

@ -0,0 +1,19 @@
namespace UniGLTF
{
public static class IndexExtensions
{
public static bool HasValidIndex(this int? self)
{
if (!self.HasValue)
{
return false;
}
if (self.Value < 0)
{
// 古いモデルで index の無効値に -1 を使っている場合がある
return false;
}
return true;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e0989fbb259f59149ab310387ea69835
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -318,7 +318,6 @@ namespace UniGLTF
componentType = (glComponentType)ComponentType,
type = AccessorType.ToString(),
count = Count,
byteOffset = -1,
sparse = new glTFSparse
{
count = sparseValuesWithIndex.Count,

View File

@ -218,7 +218,7 @@ namespace UniGLTF
if (vertexAccessor.count <= 0) return NativeArrayManager.CreateNativeArray<T>(0);
var result = (vertexAccessor.bufferView.HasValue)
var result = (vertexAccessor.bufferView.HasValidIndex())
? GetTypedFromAccessor<T>(vertexAccessor, GLTF.bufferViews[vertexAccessor.bufferView.Value])
: NativeArrayManager.CreateNativeArray<T>(vertexAccessor.count)
;
@ -277,7 +277,7 @@ namespace UniGLTF
var bufferCount = vertexAccessor.count * vertexAccessor.TypeCount;
NativeArray<float> result = default;
if (vertexAccessor.bufferView.HasValue)
if (vertexAccessor.bufferView.HasValidIndex())
{
var view = GLTF.bufferViews[vertexAccessor.bufferView.Value];
var segment = GetBytesFromBuffer(view.buffer);