Skip to content

Commit

Permalink
Merge pull request #1753 from ichi-23/ichi/patch_to_0_101_1
Browse files Browse the repository at this point in the history
v0.101.1
  • Loading branch information
ichi-23 authored Jul 25, 2022
2 parents af98903 + 663271c commit 87b0111
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 36 deletions.
5 changes: 5 additions & 0 deletions Assets/UniGLTF/Runtime/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#if UNITY_EDITOR
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UniGLTF.Tests")]
#endif
11 changes: 11 additions & 0 deletions Assets/UniGLTF/Runtime/AssemblyInfo.cs.meta

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

43 changes: 21 additions & 22 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class MeshData : IDisposable
private NativeArray<MeshVertex> _vertices;
public NativeArray<MeshVertex> Vertices => _vertices.GetSubArray(0, _currentVertexCount);
int _currentVertexCount = 0;

private NativeArray<SkinnedMeshVertex> _skinnedMeshVertices;
public NativeArray<SkinnedMeshVertex> SkinnedMeshVertices => _skinnedMeshVertices.GetSubArray(0, _currentSkinCount);
int _currentSkinCount = 0;
Expand All @@ -23,14 +24,16 @@ internal class MeshData : IDisposable

private readonly List<SubMeshDescriptor> _subMeshes = new List<SubMeshDescriptor>();
public IReadOnlyList<SubMeshDescriptor> SubMeshes => _subMeshes;

private readonly List<int> _materialIndices = new List<int>();
public IReadOnlyList<int> MaterialIndices => _materialIndices;

private readonly List<BlendShape> _blendShapes = new List<BlendShape>();
public IReadOnlyList<BlendShape> BlendShapes => _blendShapes;

public bool HasNormal { get; private set; } = true;
public bool HasNormal { get; private set; }
public string Name { get; private set; }
public bool AssignBoneWeight { get; private set; }
public bool ShouldSetRendererNodeAsBone { get; private set; }

public MeshData(int vertexCapacity, int indexCapacity)
{
Expand All @@ -56,19 +59,20 @@ void Clear()
_blendShapes.Clear();
Name = null;
HasNormal = false;
AssignBoneWeight = false;
ShouldSetRendererNodeAsBone = false;
}

/// <summary>
/// バッファ共有方式(vrm-0.x)の判定。
/// import の後方互換性のためで、vrm-1.0 export では使いません。
///
/// * バッファ共用方式は VertexBuffer が同じでSubMeshの index buffer がスライドしていく方式
/// バッファ共用方式は連結済みの VertexBuffer を共有して、SubMeshの index buffer による参照がスライドしていく方式
///
/// * バッファがひとつのとき
/// * すべての primitive の attribute が 同一の accessor を使用している時
///
/// </summary>
private static bool HasSharedVertexBuffer(glTFMesh gltfMesh)
public static bool HasSharedVertexBuffer(glTFMesh gltfMesh)
{
glTFAttributes lastAttributes = null;
foreach (var prim in gltfMesh.primitives)
Expand Down Expand Up @@ -196,15 +200,6 @@ public static (int VertexCapacity, int IndexCapacity) GetCapacity(GltfData data,
return (vertexCount, indexCount);
}

/// <summary>
/// 各種頂点属性が使われているかどうかをチェックし、使われていなかったらフラグを切る
/// MEMO: O(1)で検知する手段がありそう
/// </summary>
private void CheckAttributeUsages(glTFPrimitives primitives)
{
if (!primitives.HasNormal()) HasNormal = false;
}

private BlendShape GetOrCreateBlendShape(int i)
{
if (i < _blendShapes.Count && _blendShapes[i] != null)
Expand Down Expand Up @@ -317,16 +312,18 @@ private void ImportMeshIndependentVertexBuffer(GltfData data, glTFMesh gltfMesh,
var vertexOffset = _currentVertexCount;
var indexBufferCount = primitives.indices;

// position は必ずある
// position は必ず存在する。normal, texCoords, colors, skinning は無いかもしれない
var positions = primitives.GetPositions(data);
var normals = primitives.GetNormals(data, positions.Length);
if (normals.HasValue)
{
HasNormal = true;
}
var texCoords0 = primitives.GetTexCoords0(data, positions.Length);
var texCoords1 = primitives.GetTexCoords1(data, positions.Length);
var colors = primitives.GetColors(data, positions.Length);
var skinning = SkinningInfo.Create(data, gltfMesh, primitives);
AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone ;

CheckAttributeUsages(primitives);
ShouldSetRendererNodeAsBone = skinning.ShouldSetRendererNodeAsBone;

for (var i = 0; i < positions.Length; ++i)
{
Expand Down Expand Up @@ -447,18 +444,20 @@ private void ImportMeshSharingVertexBuffer(GltfData data, glTFMesh gltfMesh, IAx
var isOldVersion = data.GLTF.IsGeneratedUniGLTFAndOlder(1, 16);

{
// 同じVertexBufferを共有しているので先頭のモノを使う
// すべての primitives で連結済みの VertexBuffer を共有している。代表して先頭を使う
var primitives = gltfMesh.primitives.First();

var positions = primitives.GetPositions(data);
var normals = primitives.GetNormals(data, positions.Length);
if (normals.HasValue)
{
HasNormal = true;
}
var texCoords0 = primitives.GetTexCoords0(data, positions.Length);
var texCoords1 = primitives.GetTexCoords1(data, positions.Length);
var colors = primitives.GetColors(data, positions.Length);
var skinning = SkinningInfo.Create(data, gltfMesh, primitives);
AssignBoneWeight = skinning.ShouldSetRendererNodeAsBone ;

CheckAttributeUsages(primitives);
ShouldSetRendererNodeAsBone = skinning.ShouldSetRendererNodeAsBone;

for (var i = 0; i < positions.Length; ++i)
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static async Task<MeshWithMaterials> BuildMeshAndUploadAsync(
{
Mesh = mesh,
Materials = data.MaterialIndices.Select(materialFromIndex).ToArray(),
ShouldSetRendererNodeAsBone = data.AssignBoneWeight,
ShouldSetRendererNodeAsBone = data.ShouldSetRendererNodeAsBone,
};
await awaitCaller.NextFrame();

Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Runtime/UniGLTF/UniGLTFVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static partial class UniGLTFVersion
{
public const int MAJOR = 2;
public const int MINOR = 37;
public const int PATCH = 0;
public const string VERSION = "2.37.0";
public const int PATCH = 1;
public const string VERSION = "2.37.1";
}
}
210 changes: 210 additions & 0 deletions Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
using System;
using NUnit.Framework;
using UnityEngine;

namespace UniGLTF
{
public class MeshDataTests
{
/// <summary>
/// shared
/// 3 2
/// +-+
/// |/|
/// +-+
/// 0 1
///
/// divided
/// 2
/// +
/// /|
/// +-+
/// 0 1
/// 4 3
/// +-+
/// |/
/// +
/// 5
/// </summary>
static byte[] CreateTestData(bool shared, bool hasNormal)
{
var data = new ExportingGltfData();
data.Gltf.asset.version = "2.0";
var mesh = new glTFMesh();
data.Gltf.meshes.Add(mesh);

if (shared)
{
var positions = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
new Vector3(),
};
var normals = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
new Vector3(),
};

var position = data.ExtendBufferAndGetAccessorIndex(positions);
var normal = data.ExtendBufferAndGetAccessorIndex(normals);
{
var prim = new glTFPrimitives
{
attributes = new glTFAttributes
{
POSITION = position,
},
indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }),
};
mesh.primitives.Add(prim);
if (hasNormal)
{
prim.attributes.NORMAL = normal;
}
}
{
var prim = new glTFPrimitives
{
attributes = new glTFAttributes
{
POSITION = position,
},
indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 2, 3, 0 }),
};
mesh.primitives.Add(prim);
if (hasNormal)
{
prim.attributes.NORMAL = normal;
}
}
}
else
{
{
var positions = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
};
var position = data.ExtendBufferAndGetAccessorIndex(positions);
var prim = new glTFPrimitives
{
attributes = new glTFAttributes
{
POSITION = position,
},
indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }),
};
if (hasNormal)
{
var normals = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
};
var normal = data.ExtendBufferAndGetAccessorIndex(normals);
prim.attributes.NORMAL = normal;
}
mesh.primitives.Add(prim);
}
{
var positions = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
};
var position = data.ExtendBufferAndGetAccessorIndex(positions);
var prim = new glTFPrimitives
{
attributes = new glTFAttributes
{
POSITION = position,
},
indices = data.ExtendBufferAndGetAccessorIndex(new uint[] { 0, 1, 2 }),
};
if (hasNormal)
{
var normals = new Vector3[]
{
new Vector3(),
new Vector3(),
new Vector3(),
};
var normal = data.ExtendBufferAndGetAccessorIndex(normals);
prim.attributes.NORMAL = normal;
}
mesh.primitives.Add(prim);
}
}
return data.ToGlbBytes();
}

[Test]
public void SharedHasNormalTest()
{
var glb = CreateTestData(true, true);
using (var parsed = new GlbBinaryParser(glb, "test").Parse())
{
Assert.True(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0]));
using (var data = new MeshData(6, 6))
{
data.LoadFromGltf(parsed, 0, new ReverseZ());
Assert.True(data.HasNormal);
}
}
}

[Test]
public void SharedNotHasNormalTest()
{
var glb = CreateTestData(true, false);
using (var parsed = new GlbBinaryParser(glb, "test").Parse())
{
Assert.True(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0]));
using (var data = new MeshData(6, 6))
{
data.LoadFromGltf(parsed, 0, new ReverseZ());
Assert.False(data.HasNormal);
}
}
}

[Test]
public void DividedHasNormalTest()
{
var glb = CreateTestData(false, true);
using (var parsed = new GlbBinaryParser(glb, "test").Parse())
{
Assert.False(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0]));
using (var data = new MeshData(6, 6))
{
data.LoadFromGltf(parsed, 0, new ReverseZ());
Assert.True(data.HasNormal);
}
}
}

[Test]
public void DividedNotHasNormalTest()
{
var glb = CreateTestData(false, false);
using (var parsed = new GlbBinaryParser(glb, "test").Parse())
{
Assert.False(MeshData.HasSharedVertexBuffer(parsed.GLTF.meshes[0]));
using (var data = new MeshData(6, 6))
{
data.LoadFromGltf(parsed, 0, new ReverseZ());
Assert.False(data.HasNormal);
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs.meta

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

4 changes: 2 additions & 2 deletions Assets/UniGLTF/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.vrmc.gltf",
"version": "0.101.0",
"version": "0.101.1",
"displayName": "UniGLTF",
"description": "GLTF importer and exporter",
"unity": "2019.4",
Expand All @@ -11,6 +11,6 @@
"name": "VRM Consortium"
},
"dependencies": {
"com.vrmc.vrmshaders": "0.101.0"
"com.vrmc.vrmshaders": "0.101.1"
}
}
Loading

0 comments on commit 87b0111

Please sign in to comment.