Ejemplo n.º 1
0
        int AddViewTo(ExportingGltfData data, int bufferIndex,
                      int offset = 0, int count = 0)
        {
            var stride = Stride;

            if (count == 0)
            {
                count = Count;
            }
            var slice = Bytes.GetSubArray(offset * stride, count * stride);

            return(data.AppendToBuffer(slice));
        }
Ejemplo n.º 2
0
        public int AddAccessorTo(ExportingGltfData data, int bufferIndex,
                                 // GltfBufferTargetType targetType,
                                 bool useSparse,
                                 Action <NativeArray <byte>, glTFAccessor> minMax = null,
                                 int offset = 0, int count = 0)
        {
            if (ComponentType == AccessorValueType.FLOAT &&
                AccessorType == AccessorVectorType.VEC3
                )
            {
                var values = 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
                    using (var sparseIndexBin = new NativeArray <byte>(sparseValuesWithIndex.Count * 4, Allocator.Persistent))
                        using (var sparseValueBin = new NativeArray <byte>(sparseValuesWithIndex.Count * 12, Allocator.Persistent))
                        {
                            var sparseIndexSpan = sparseIndexBin.Reinterpret <Int32>(1);
                            var sparseValueSpan = sparseValueBin.Reinterpret <Vector3>(1);

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

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

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

            var viewIndex = AddViewTo(data, bufferIndex, offset, count);

            return(AddAccessorTo(data, viewIndex, minMax, 0, count));
        }