Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primitives attributes が有ったり無かったりする場合に対応(例えばNORMAL) #644

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 83 additions & 29 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,21 @@ public MeshContext(string name, int meshIndex)
m_name = name;
}

void FillZero<T>(IList<T> list)
/// <summary>
/// 指定した長さを0で埋める
/// </summary>
/// <param name="list"></param>
/// <param name="fillLength"></param>
/// <typeparam name="T"></typeparam>
static void FillZero<T>(IList<T> list, int fillLength)
{
if (list.Count != m_positions.Count)
if (list.Count > fillLength)
{
throw new NotImplementedException();
throw new Exception("ありえない");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もうちょっと詳しく書いたほうが良さそう、UniGLTFの部分なのでエラーメッセージは英語にしてあげたほうがいいかも

}
while (list.Count < fillLength)
{
list.Add(default);
}
}

Expand All @@ -105,64 +115,96 @@ public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltf
var indexBuffer = prim.indices;

// position は必ずある
var positionCount = m_positions.Count;
m_positions.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector3>(prim.attributes.POSITION).Select(x => x.ReverseZ()));
positionCount = m_positions.Count - positionCount;
var positions = ctx.GLTF.GetArrayFromAccessor<Vector3>(prim.attributes.POSITION);
var fillLength = m_positions.Count;
m_positions.AddRange(positions.Select(x => x.ReverseZ()));

// normal
if (prim.attributes.NORMAL != -1)
{
FillZero(m_normals);
m_normals.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector3>(prim.attributes.NORMAL).Select(x => x.ReverseZ()));
var normals = ctx.GLTF.GetArrayFromAccessor<Vector3>(prim.attributes.NORMAL);
if (normals.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(m_normals, fillLength);
m_normals.AddRange(normals.Select(x => x.ReverseZ()));
}

#if false
if (prim.attributes.TANGENT != -1)
{
FillZero(tangetns);
tangents.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector4>(prim.attributes.TANGENT).Select(x => x.ReverseZ()));
var tangents = ctx.GLTF.GetArrayFromAccessor<Vector4>(prim.attributes.TANGENT);
if (tangents.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(tangetns, fillLength);
tangents.AddRange(.Select(x => x.ReverseZ()));
}
#endif

// uv
if (prim.attributes.TEXCOORD_0 != -1)
{
FillZero(m_uv);
var uvs = ctx.GLTF.GetArrayFromAccessor<Vector2>(prim.attributes.TEXCOORD_0);
if (uvs.Length != positions.Length)
{
throw new Exception("different length");
}
if (ctx.IsGeneratedUniGLTFAndOlder(1, 16))
{
#pragma warning disable 0612
// backward compatibility
m_uv.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector2>(prim.attributes.TEXCOORD_0).Select(x => x.ReverseY()));
FillZero(m_uv, fillLength);
m_uv.AddRange(uvs.Select(x => x.ReverseY()));
#pragma warning restore 0612
}
else
{
m_uv.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector2>(prim.attributes.TEXCOORD_0).Select(x => x.ReverseUV()));
FillZero(m_uv, fillLength);
m_uv.AddRange(uvs.Select(x => x.ReverseUV()));
}
}

// uv2
if (prim.attributes.TEXCOORD_1 != -1)
{
FillZero(m_uv2);
m_uv2.AddRange(ctx.GLTF.GetArrayFromAccessor<Vector2>(prim.attributes.TEXCOORD_1).Select(x => x.ReverseUV()));
var uvs = ctx.GLTF.GetArrayFromAccessor<Vector2>(prim.attributes.TEXCOORD_1);
if (uvs.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(m_uv2, fillLength);
m_uv2.AddRange(uvs.Select(x => x.ReverseUV()));
}

// color
if (prim.attributes.COLOR_0 != -1)
{
FillZero(m_colors);
m_colors.AddRange(ctx.GLTF.GetArrayFromAccessor<Color>(prim.attributes.COLOR_0));
var colors = ctx.GLTF.GetArrayFromAccessor<Color>(prim.attributes.COLOR_0);
if (colors.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(m_colors, fillLength);
m_colors.AddRange(colors);
}

// skin
if (prim.attributes.JOINTS_0 != -1 && prim.attributes.WEIGHTS_0 != -1)
{
FillZero(m_boneWeights);

var joints0 = ctx.GLTF.GetArrayFromAccessor<UShort4>(prim.attributes.JOINTS_0); // uint4
var weights0 = ctx.GLTF.GetArrayFromAccessor<Float4>(prim.attributes.WEIGHTS_0).Select(x => x.One()).ToArray();

if (joints0.Length != positions.Length)
{
throw new Exception("different length");
}
if (weights0.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(m_boneWeights, fillLength);
for (int j = 0; j < joints0.Length; ++j)
{
var bw = new BoneWeight();
Expand Down Expand Up @@ -192,21 +234,33 @@ public void ImportMeshIndependentVertexBuffer(ImporterContext ctx, glTFMesh gltf
var blendShape = new BlendShape(i.ToString());
if (primTarget.POSITION != -1)
{
FillZero(blendShape.Positions);
blendShape.Positions.AddRange(
ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.POSITION).Select(x => x.ReverseZ()).ToArray());
var array = ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.POSITION);
if (array.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(blendShape.Positions, fillLength);
blendShape.Positions.AddRange(array.Select(x => x.ReverseZ()).ToArray());
}
if (primTarget.NORMAL != -1)
{
FillZero(blendShape.Normals);
blendShape.Normals.AddRange(
ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.NORMAL).Select(x => x.ReverseZ()).ToArray());
var array = ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.NORMAL);
if (array.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(blendShape.Normals, fillLength);
blendShape.Normals.AddRange(array.Select(x => x.ReverseZ()).ToArray());
}
if (primTarget.TANGENT != -1)
{
FillZero(blendShape.Tangents);
blendShape.Tangents.AddRange(
ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.TANGENT).Select(x => x.ReverseZ()).ToArray());
var array = ctx.GLTF.GetArrayFromAccessor<Vector3>(primTarget.TANGENT);
if (array.Length != positions.Length)
{
throw new Exception("different length");
}
FillZero(blendShape.Tangents, fillLength);
blendShape.Tangents.AddRange(array.Select(x => x.ReverseZ()).ToArray());
}
m_blendShapes.Add(blendShape);
}
Expand Down