mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-24 23:18:04 -05:00
コード見直し。SparseBase 使ってなかった。
This commit is contained in:
parent
40be812e25
commit
ada991eb71
|
|
@ -5,21 +5,9 @@ using UnityEngine;
|
|||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public struct SparseBase
|
||||
{
|
||||
public readonly Vector3[] Positions;
|
||||
public readonly Vector3[] Normals;
|
||||
|
||||
public SparseBase(Vector3[] positions, Vector3[] normals)
|
||||
{
|
||||
Positions = positions;
|
||||
Normals = normals;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BlendShapeExporter
|
||||
{
|
||||
public static gltfMorphTarget Export(glTF gltf, int gltfBuffer, Vector3[] positions, Vector3[] normals, SparseBase? sparseBase)
|
||||
public static gltfMorphTarget Export(glTF gltf, int gltfBuffer, Vector3[] positions, Vector3[] normals, bool useSparse)
|
||||
{
|
||||
var accessorCount = positions.Length;
|
||||
if (normals != null && positions.Length != normals.Length)
|
||||
|
|
@ -27,21 +15,33 @@ namespace UniGLTF
|
|||
throw new Exception();
|
||||
}
|
||||
|
||||
bool useSparse = sparseBase.HasValue;
|
||||
if (sparseBase.HasValue)
|
||||
int[] sparseIndices = default;
|
||||
if (useSparse)
|
||||
{
|
||||
var sparseIndices = Enumerable.Range(0, positions.Length).Where(x => positions[x] != Vector3.zero).ToArray();
|
||||
sparseIndices = Enumerable.Range(0, positions.Length).Where(x => positions[x] != Vector3.zero).ToArray();
|
||||
if (sparseIndices.Length == 0)
|
||||
{
|
||||
// sparse 対象がすべて [0, 0, 0] の場合
|
||||
// new glTFSparse
|
||||
// {
|
||||
// count = 0,
|
||||
// }
|
||||
// のようになる。
|
||||
// たぶん、仕様的にはあり。
|
||||
// 解釈できない場合あり。
|
||||
useSparse = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (useSparse)
|
||||
{
|
||||
if (sparseIndices == null)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
// positions
|
||||
var positionAccessorIndex = -1;
|
||||
var sparseIndices = Enumerable.Range(0, positions.Length).Where(x => positions[x] != Vector3.zero).ToArray();
|
||||
if (sparseIndices.Length > 0)
|
||||
{
|
||||
Debug.LogFormat("Sparse {0}/{1}", sparseIndices.Length, positions.Length);
|
||||
|
|
|
|||
|
|
@ -30,12 +30,12 @@ namespace UniGLTF
|
|||
m_normals[index] = normal;
|
||||
}
|
||||
|
||||
public gltfMorphTarget ToGltf(glTF gltf, int gltfBuffer, bool useNormal, SparseBase? sparseBase)
|
||||
public gltfMorphTarget ToGltf(glTF gltf, int gltfBuffer, bool useNormal, bool useSparse)
|
||||
{
|
||||
return BlendShapeExporter.Export(gltf, gltfBuffer,
|
||||
m_positions,
|
||||
useNormal ? m_normals : null,
|
||||
sparseBase);
|
||||
useSparse);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,12 +103,13 @@ namespace UniGLTF
|
|||
m_weights.Add(new Vector4(boneWeight.weight0, boneWeight.weight1, boneWeight.weight2, boneWeight.weight3));
|
||||
}
|
||||
|
||||
public (glTFPrimitives, SparseBase) ToGltfPrimitive(glTF gltf, int bufferIndex, int materialIndex, IEnumerable<int> indices)
|
||||
public glTFPrimitives ToGltfPrimitive(glTF gltf, int bufferIndex, int materialIndex, IEnumerable<int> indices)
|
||||
{
|
||||
var sparseBase = new SparseBase(m_positions.ToArray(), m_normals.ToArray());
|
||||
var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices.Select(x => (uint)m_vertexIndexMap[x]).ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER);
|
||||
var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, sparseBase.Positions, glBufferTarget.ARRAY_BUFFER);
|
||||
var normalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, sparseBase.Normals, glBufferTarget.ARRAY_BUFFER);
|
||||
var positions = m_positions.ToArray();
|
||||
var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, positions, glBufferTarget.ARRAY_BUFFER);
|
||||
var normals = m_normals.ToArray();
|
||||
var normalAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, normals, glBufferTarget.ARRAY_BUFFER);
|
||||
var uvAccessorIndex0 = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, m_uv.ToArray(), glBufferTarget.ARRAY_BUFFER);
|
||||
|
||||
int? jointsAccessorIndex = default;
|
||||
|
|
@ -137,7 +138,7 @@ namespace UniGLTF
|
|||
mode = 4,
|
||||
};
|
||||
|
||||
return (primitive, sparseBase);
|
||||
return primitive;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ namespace UniGLTF
|
|||
flipped.Add(t1);
|
||||
flipped.Add(t0);
|
||||
}
|
||||
var (gltfPrimitive, sparseBase) = buffer.ToGltfPrimitive(gltf, gltfBuffer, materialIndex, flipped);
|
||||
var gltfPrimitive = buffer.ToGltfPrimitive(gltf, gltfBuffer, materialIndex, flipped);
|
||||
|
||||
// blendShape(morph target)
|
||||
for (int j = 0; j < mesh.blendShapeCount; ++j)
|
||||
|
|
@ -102,7 +102,7 @@ namespace UniGLTF
|
|||
}
|
||||
|
||||
gltfPrimitive.targets.Add(blendShape.ToGltf(gltf, gltfBuffer, !settings.ExportOnlyBlendShapePosition,
|
||||
settings.UseSparseAccessorForMorphTarget ? sparseBase : default));
|
||||
settings.UseSparseAccessorForMorphTarget));
|
||||
}
|
||||
|
||||
gltfMesh.primitives.Add(gltfPrimitive);
|
||||
|
|
|
|||
|
|
@ -231,20 +231,23 @@ namespace UniGLTF
|
|||
{
|
||||
blendShapeNormals[i] = axisInverter.InvertVector3(blendShapeNormals[i]);
|
||||
}
|
||||
var sparseBase = new SparseBase(mesh.vertices, mesh.normals);
|
||||
for (int i = 0; i < sparseBase.Positions.Length; ++i)
|
||||
|
||||
var positions = mesh.vertices;
|
||||
for (int i = 0; i < positions.Length; ++i)
|
||||
{
|
||||
sparseBase.Positions[i] = axisInverter.InvertVector3(sparseBase.Positions[i]);
|
||||
positions[i] = axisInverter.InvertVector3(positions[i]);
|
||||
}
|
||||
for (int i = 0; i < sparseBase.Normals.Length; ++i)
|
||||
|
||||
var normals = mesh.normals;
|
||||
for (int i = 0; i < normals.Length; ++i)
|
||||
{
|
||||
sparseBase.Normals[i] = axisInverter.InvertVector3(sparseBase.Normals[i]);
|
||||
normals[i] = axisInverter.InvertVector3(normals[i]);
|
||||
}
|
||||
|
||||
return BlendShapeExporter.Export(gltf, bufferIndex,
|
||||
blendShapeVertices,
|
||||
exportOnlyBlendShapePosition && useNormal ? null : blendShapeNormals,
|
||||
useSparseAccessorForMorphTarget ? sparseBase : default);
|
||||
useSparseAccessorForMorphTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ namespace UniVRM10
|
|||
}
|
||||
}
|
||||
var materialIndex = submesh.Material;
|
||||
var (gltfPrimitive, sparseBase) = buffer.ToGltfPrimitive(storage.Gltf, bufferIndex, materialIndex, indices);
|
||||
var gltfPrimitive = buffer.ToGltfPrimitive(storage.Gltf, bufferIndex, materialIndex, indices);
|
||||
|
||||
// blendShape
|
||||
for (int j = 0; j < mesh.MorphTargets.Count; ++j)
|
||||
|
|
@ -145,7 +145,7 @@ namespace UniVRM10
|
|||
);
|
||||
}
|
||||
|
||||
gltfPrimitive.targets.Add(blendShape.ToGltf(storage.Gltf, bufferIndex, !option.removeMorphNormal, option.sparse ? sparseBase : default));
|
||||
gltfPrimitive.targets.Add(blendShape.ToGltf(storage.Gltf, bufferIndex, !option.removeMorphNormal, option.sparse));
|
||||
}
|
||||
|
||||
yield return gltfPrimitive;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user