Exemple #1
0
        public static T[] GetArrayFromAccessor <T>(this glTF self, int accessorIndex) where T : struct
        {
            var vertexAccessor = self.accessors[accessorIndex];

            if (vertexAccessor.count <= 0)
            {
                return new T[] { }
            }
            ;

            var result = (vertexAccessor.bufferView != -1)
                ? self.GetAttrib <T>(vertexAccessor, self.bufferViews[vertexAccessor.bufferView])
                : new T[vertexAccessor.count]
            ;

            var sparse = vertexAccessor.sparse;

            if (sparse != null && sparse.count > 0)
            {
                // override sparse values
                var indices = self._GetIndices(self.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType);

                var values = self.GetAttrib <T>(sparse.count, sparse.values.byteOffset, self.bufferViews[sparse.values.bufferView]);

                var it = indices.GetEnumerator();
                for (int i = 0; i < sparse.count; ++i)
                {
                    it.MoveNext();
                    result[it.Current] = values[i];
                }
            }
            return(result);
        }
Exemple #2
0
        public static float[] FlatternFloatArrayFromAccessor(this glTF self, int accessorIndex)
        {
            var vertexAccessor = self.accessors[accessorIndex];

            if (vertexAccessor.count <= 0)
            {
                return new float[] { }
            }
            ;

            var bufferCount = vertexAccessor.count * vertexAccessor.TypeCount;

            float[] result = null;
            if (vertexAccessor.bufferView != -1)
            {
                var attrib = new float[vertexAccessor.count * vertexAccessor.TypeCount];

                var view    = self.bufferViews[vertexAccessor.bufferView];
                var segment = self.buffers[view.buffer].GetBytes();
                var bytes   = new ArraySegment <Byte>(segment.Array, segment.Offset + view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * view.byteStride);
                bytes.MarshalCopyTo(attrib);
                result = attrib;
            }
            else
            {
                result = new float[bufferCount];
            }

            var sparse = vertexAccessor.sparse;

            if (sparse != null && sparse.count > 0)
            {
                // override sparse values
                var indices = self._GetIndices(self.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType);
                var values  = self.GetAttrib <float>(sparse.count * vertexAccessor.TypeCount, sparse.values.byteOffset, self.bufferViews[sparse.values.bufferView]);

                var it = indices.GetEnumerator();
                for (int i = 0; i < sparse.count; ++i)
                {
                    it.MoveNext();
                    result[it.Current] = values[i];
                }
            }
            return(result);
        }
Exemple #3
0
        public static int[] GetIndices(this glTF self, int accessorIndex)
        {
            int count;
            var result  = self._GetIndices(self.accessors[accessorIndex], out count);
            var indices = new int[count];

            // flip triangles
            var it = result.GetEnumerator();

            {
                for (int i = 0; i < count; i += 3)
                {
                    it.MoveNext(); indices[i + 2] = it.Current;
                    it.MoveNext(); indices[i + 1] = it.Current;
                    it.MoveNext(); indices[i]     = it.Current;
                }
            }

            return(indices);
        }