using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using UniGLTF;
using VrmLib;
namespace UniVRM10
{
///
/// VrmLib.MeshGroup => GLTF
///
public static class MeshWriter
{
static void Vec3MinMax(ArraySegment bytes, glTFAccessor accessor)
{
var positions = SpanLike.Wrap(bytes);
var min = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
var max = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
foreach (var p in positions)
{
min = Vector3.Min(min, p);
max = Vector3.Max(max, p);
}
accessor.min = min.ToFloat3();
accessor.max = max.ToFloat3();
}
static int ExportIndices(Vrm10Storage storage, BufferAccessor x, int offset, int count, ExportArgs option)
{
if (x.Count <= ushort.MaxValue)
{
if (x.ComponentType == AccessorValueType.UNSIGNED_INT)
{
// ensure ushort
var src = x.GetSpan().Slice(offset, count);
var bytes = new byte[src.Length * 2];
var dst = SpanLike.Wrap(new ArraySegment(bytes));
for (int i = 0; i < src.Length; ++i)
{
dst[i] = (ushort)src[i];
}
var accessor = new BufferAccessor(new ArraySegment(bytes), AccessorValueType.UNSIGNED_SHORT, AccessorVectorType.SCALAR, count);
return accessor.AddAccessorTo(storage, 0, option.sparse, null, 0, count);
}
else
{
return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count);
}
}
else
{
return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count);
}
}
///
/// https://github.com/vrm-c/UniVRM/issues/800
///
/// SubMesh 単位に分割する。
/// SubMesh を Gltf の Primitive に対応させる。
///
///
///
///
///
///
static IEnumerable ExportMeshDivided(this VrmLib.Mesh mesh, List