Skip to content

Commit

Permalink
Merge pull request #1741 from ousttrue/fix/has_normal
Browse files Browse the repository at this point in the history
[importer] 法線を保持するモデルで、不要な RecalculateNormals
  • Loading branch information
ousttrue committed Jul 19, 2022
2 parents 7dce5db + b9d6e40 commit c3988f2
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Assets/UniGLTF/Runtime/AssemblyInfo.cs
@@ -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
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
Expand Up @@ -138,7 +138,7 @@ private static void UploadMeshIndices(MeshData data, Mesh mesh)
{
Mesh = mesh,
Materials = data.MaterialIndices.Select(materialFromIndex).ToArray(),
ShouldSetRendererNodeAsBone = data.AssignBoneWeight,
ShouldSetRendererNodeAsBone = data.ShouldSetRendererNodeAsBone,
};
await awaitCaller.NextFrame();

Expand Down
210 changes: 210 additions & 0 deletions Assets/UniGLTF/Tests/UniGLTF/MeshDataTests.cs
@@ -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.

0 comments on commit c3988f2

Please sign in to comment.