Merge branch 'master' into fix/use_awaitcaller

This commit is contained in:
ousttrue 2024-12-09 15:22:09 +09:00 committed by GitHub
commit d4a8027144
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 141 additions and 59 deletions

View File

@ -172,7 +172,14 @@ namespace UniGLTF.SpringBoneJobs.InputPorts
for (int i = offset; i < end; ++i)
{
// mark velocity zero
#if UNITY_2022_2_OR_NEWER
currentTails.GetSubArray(offset, Logics.Length).AsSpan().Fill(new Vector3(float.NaN, float.NaN, float.NaN));
#else
var subArray = currentTails.GetSubArray(offset, Logics.Length);
var value = new Vector3(float.NaN, float.NaN, float.NaN);
for (int a = 0; a < subArray.Length; ++a)
subArray[a] = value;
#endif
}
}
}

View File

@ -75,6 +75,17 @@ namespace UniGLTF
[Serializable]
public class glTFPrimitives
{
public enum Mode
{
POINTS = 0,
LINES = 1,
LINE_LOOP = 2,
LINE_STRIP = 3,
TRIANGLES = 4,
TRIANGLE_STRIP = 5,
TRIANGLE_FAN = 6,
}
[JsonSchema(EnumValues = new object[] { 0, 1, 2, 3, 4, 5, 6 })]
public int mode;

View File

@ -43,22 +43,18 @@ namespace UniGLTF
public glTFBufferView Extend(IntPtr p, int bytesLength, int stride, glBufferTarget target)
{
var tmp = m_bytes;
// alignment
var padding = m_used % stride == 0 ? 0 : stride - m_used % stride;
var requiredLength = m_used + padding + bytesLength;
if (m_bytes == null || m_used + padding + bytesLength > m_bytes.Length)
if (m_bytes == null || requiredLength > m_bytes.Length)
{
// recreate buffer
m_bytes = new Byte[m_used + padding + bytesLength];
var newLength = Math.Max(requiredLength, m_bytes?.Length * 2 ?? 256);
var newBuffer = new Byte[newLength];
if (m_used > 0)
{
Buffer.BlockCopy(tmp, 0, m_bytes, 0, m_used);
}
}
if (m_used + padding + bytesLength > m_bytes.Length)
{
throw new ArgumentOutOfRangeException();
Buffer.BlockCopy(m_bytes, 0, newBuffer, 0, m_used);
m_bytes = newBuffer;
}
Marshal.Copy(p, m_bytes, m_used + padding, bytesLength);
@ -70,7 +66,7 @@ namespace UniGLTF
byteStride = stride,
target = target,
};
m_used = m_used + padding + bytesLength;
m_used = requiredLength;
return result;
}

View File

@ -114,52 +114,7 @@ namespace UniGLTF
attributes.JOINTS_0 = jointsAccessorIndex;
}
var gltfMesh = new glTFMesh(mesh.name);
var indices = new List<uint>();
for (int j = 0; j < mesh.subMeshCount; ++j)
{
indices.Clear();
var triangles = mesh.GetIndices(j);
if (triangles.Length == 0)
{
// https://github.com/vrm-c/UniVRM/issues/664
continue;
}
for (int i = 0; i < triangles.Length; i += 3)
{
var i0 = triangles[i];
var i1 = triangles[i + 1];
var i2 = triangles[i + 2];
// flip triangle
indices.Add((uint)i2);
indices.Add((uint)i1);
indices.Add((uint)i0);
}
var indicesAccessorIndex = data.ExtendBufferAndGetAccessorIndex(indices.ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER);
if (indicesAccessorIndex < 0)
{
// https://github.com/vrm-c/UniVRM/issues/664
throw new Exception();
}
if (j >= materials.Length)
{
Debug.LogWarningFormat("{0}.materials is not enough", unityMesh.Mesh.name);
break;
}
gltfMesh.primitives.Add(new glTFPrimitives
{
attributes = attributes,
indices = indicesAccessorIndex,
mode = 4, // triangles ?
material = unityMaterials.IndexOf(materials[j])
});
}
var gltfMesh = CreateGLTFMesh(attributes, data, unityMesh, unityMaterials);
var blendShapeIndexMap = new Dictionary<int, int>();
{
@ -201,6 +156,119 @@ namespace UniGLTF
return (gltfMesh, blendShapeIndexMap);
}
private static glTFMesh CreateGLTFMesh(glTFAttributes attributes, ExportingGltfData data, MeshExportInfo unityMesh, List<Material> unityMaterials)
{
var mesh = unityMesh.Mesh;
var materials = unityMesh.Materials;
var gltfMesh = new glTFMesh(mesh.name);
var indices = new List<uint>();
for (int j = 0; j < mesh.subMeshCount; ++j)
{
indices.Clear();
if (j >= materials.Length)
{
Debug.LogWarningFormat("{0}.materials is not enough", mesh.name);
continue;
}
var subMesh = mesh.GetSubMesh(j);
var topologyType = subMesh.topology;
var materialIndex = unityMaterials.IndexOf(materials[j]);
var submeshIndices = mesh.GetIndices(j);
if (submeshIndices.Length == 0)
{
// https://github.com/vrm-c/UniVRM/issues/664
break;
}
else if (submeshIndices.Length < 3)
{
Debug.LogWarningFormat("Invalid primitive of type {0} found", topologyType);
continue;
}
// Add indices considering the topology type
switch (topologyType)
{
case MeshTopology.Triangles:
if (submeshIndices.Length % 3 != 0)
Debug.LogWarningFormat("triangle indices is not multiple of 3");
GetTriangleIndices(indices, submeshIndices);
break;
case MeshTopology.Quads:
if (submeshIndices.Length % 4 != 0)
Debug.LogWarningFormat("quad indices is not multiple of 4");
GetQuadIndices(indices, submeshIndices);
break;
default:
case MeshTopology.Lines:
case MeshTopology.LineStrip:
case MeshTopology.Points:
Debug.LogWarningFormat("Mesh {0} has unsupported topology type {1}.", mesh.name, topologyType);
continue;
}
var primitive = CreatePrimitives(attributes, data, indices, materialIndex);
gltfMesh.primitives.Add(primitive);
}
return gltfMesh;
}
private static glTFPrimitives CreatePrimitives(glTFAttributes attributes, ExportingGltfData data, List<uint> indices, int materialIndex)
{
var indicesAccessorIndex = data.ExtendBufferAndGetAccessorIndex(indices.ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER);
if (indicesAccessorIndex < 0)
{
// https://github.com/vrm-c/UniVRM/issues/664
throw new Exception();
}
var primitive = new glTFPrimitives
{
attributes = attributes,
indices = indicesAccessorIndex,
mode = (int)glTFPrimitives.Mode.TRIANGLES, // triangles ?
material = materialIndex
};
return primitive;
}
private static void GetQuadIndices(List<uint> indices, int[] quadIndices)
{
for (int i = 0; i < quadIndices.Length - 3; i += 4)
{
var i0 = quadIndices[i];
var i1 = quadIndices[i + 1];
var i2 = quadIndices[i + 2];
var i3 = quadIndices[i + 3];
// flip triangles
indices.Add((uint)i2);
indices.Add((uint)i1);
indices.Add((uint)i0);
indices.Add((uint)i3);
indices.Add((uint)i2);
indices.Add((uint)i0);
}
}
private static void GetTriangleIndices(List<uint> indices, int[] triangleIndices)
{
for (int i = 0; i < triangleIndices.Length - 2; i += 3)
{
var i0 = triangleIndices[i];
var i1 = triangleIndices[i + 1];
var i2 = triangleIndices[i + 2];
// flip triangle
indices.Add((uint)i2);
indices.Add((uint)i1);
indices.Add((uint)i0);
}
}
static bool UseSparse(
bool usePosition, Vector3 position,
bool useNormal, Vector3 normal,

View File

@ -69,8 +69,8 @@ namespace UniGLTF
public NativeArray<T> CreateNativeArray<T>(ArraySegment<T> data) where T : struct
{
var array = CreateNativeArray<T>(data.Count);
// TODO: remove ToArray
array.CopyFrom(data.ToArray());
for (int i = 0; i < data.Count; i++)
array[i] = data.Array[data.Offset + i];
return array;
}