Esempio n. 1
0
 public Item(glTFAccessor src)
 {
     Name   = src.name;
     View   = src.bufferView;
     Offset = src.byteOffset;
     Count  = src.count;
     Type   = src.type;
 }
Esempio n. 2
0
        static void Vec3MinMax(ArraySegment <byte> bytes, glTFAccessor accessor)
        {
            var positions = SpanLike.Wrap <Vector3>(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();
        }
Esempio n. 3
0
        int AddAccessor <T>(SpanLike <T> span) where T : struct
        {
            var bufferViewIndex = AddBuffer(span.Bytes);
            var accessor        = new glTFAccessor
            {
                bufferView    = bufferViewIndex,
                count         = span.Length,
                byteOffset    = 0,
                componentType = glTFExtensions.GetComponentType <T>(),
                type          = glTFExtensions.GetAccessorType <T>(),
            };
            var index = _accessors.Count;

            _accessors.Add(accessor);
            return(index);
        }
Esempio n. 4
0
        int AddAccessor <T>(NativeArray <T> span) where T : struct
        {
            var bufferViewIndex = AddBuffer(span.Reinterpret <byte>(Marshal.SizeOf <T>()));
            var accessor        = new glTFAccessor
            {
                bufferView    = bufferViewIndex,
                count         = span.Length,
                byteOffset    = 0,
                componentType = glTFExtensions.GetComponentType <T>(),
                type          = glTFExtensions.GetAccessorType <T>(),
            };
            var index = _accessors.Count;

            _accessors.Add(accessor);
            return(index);
        }
Esempio n. 5
0
        public static int AddAccessorTo(this VrmLib.BufferAccessor self,
                                        Vrm10Storage storage, int bufferIndex,
                                        // GltfBufferTargetType targetType,
                                        bool useSparse,
                                        Action <ArraySegment <byte>, glTFAccessor> minMax = null,
                                        int offset = 0, int count = 0)
        {
            if (self.ComponentType == VrmLib.AccessorValueType.FLOAT &&
                self.AccessorType == VrmLib.AccessorVectorType.VEC3
                )
            {
                var values = self.GetSpan <Vector3>();
                // 巨大ポリゴンのモデル対策にValueTupleの型をushort -> uint へ
                var sparseValuesWithIndex = new List <ValueTuple <int, Vector3> >();
                for (int i = 0; i < values.Length; ++i)
                {
                    var v = values[i];
                    if (v != Vector3.Zero)
                    {
                        sparseValuesWithIndex.Add((i, v));
                    }
                }

                //var status = $"{sparseIndices.Count * 14}/{values.Length * 12}";
                if (useSparse &&
                    sparseValuesWithIndex.Count > 0 && // avoid empty sparse
                    sparseValuesWithIndex.Count * 16 < values.Length * 12)
                {
                    // use sparse
                    var sparseIndexBin  = new ArraySegment <byte>(new byte[sparseValuesWithIndex.Count * 4]);
                    var sparseIndexSpan = SpanLike.Wrap <Int32>(sparseIndexBin);
                    var sparseValueBin  = new ArraySegment <byte>(new byte[sparseValuesWithIndex.Count * 12]);
                    var sparseValueSpan = SpanLike.Wrap <Vector3>(sparseValueBin);

                    for (int i = 0; i < sparseValuesWithIndex.Count; ++i)
                    {
                        var(index, value)  = sparseValuesWithIndex[i];
                        sparseIndexSpan[i] = index;
                        sparseValueSpan[i] = value;
                    }

                    var sparseIndexView = storage.AppendToBuffer(sparseIndexBin);
                    var sparseValueView = storage.AppendToBuffer(sparseValueBin);

                    var accessorIndex = storage.Gltf.accessors.Count;
                    var accessor      = new glTFAccessor
                    {
                        componentType = (glComponentType)self.ComponentType,
                        type          = self.AccessorType.ToString(),
                        count         = self.Count,
                        byteOffset    = -1,
                        sparse        = new glTFSparse
                        {
                            count   = sparseValuesWithIndex.Count,
                            indices = new glTFSparseIndices
                            {
                                componentType = (glComponentType)VrmLib.AccessorValueType.UNSIGNED_INT,
                                bufferView    = sparseIndexView,
                            },
                            values = new glTFSparseValues
                            {
                                bufferView = sparseValueView,
                            },
                        }
                    };
                    if (minMax != null)
                    {
                        minMax(sparseValueBin, accessor);
                    }
                    storage.Gltf.accessors.Add(accessor);
                    return(accessorIndex);
                }
            }

            var viewIndex = self.AddViewTo(storage, bufferIndex, offset, count);

            return(self.AddAccessorTo(storage, viewIndex, minMax, 0, count));
        }