Esempio n. 1
0
        private static uint DetermineNoOfComponents(Accessor.TypeEnum type)
        {
            switch (type)
            {
            case Accessor.TypeEnum.SCALAR:
                return(1U);

            case Accessor.TypeEnum.VEC2:
                return(2U);

            case Accessor.TypeEnum.VEC3:
                return(3U);

            case Accessor.TypeEnum.VEC4:
                return(4U);

            case Accessor.TypeEnum.MAT2:
                return(4U);

            case Accessor.TypeEnum.MAT3:
                return(9U);

            case Accessor.TypeEnum.MAT4:
                return(16U);

            default:
                throw new NotSupportedException();
            }
        }
Esempio n. 2
0
        public static int GetTypeMultiplier(Accessor.TypeEnum accessorType)
        {
            var mult = 0;

            switch (accessorType)
            {
            case glTFLoader.Schema.Accessor.TypeEnum.SCALAR:
                mult = 1;
                break;

            case glTFLoader.Schema.Accessor.TypeEnum.VEC2:
                mult = 2;
                break;

            case glTFLoader.Schema.Accessor.TypeEnum.VEC3:
                mult = 3;
                break;

            case glTFLoader.Schema.Accessor.TypeEnum.VEC4:
            case glTFLoader.Schema.Accessor.TypeEnum.MAT2:
                mult = 4;
                break;

            case glTFLoader.Schema.Accessor.TypeEnum.MAT3:
                mult = 9;
                break;

            case glTFLoader.Schema.Accessor.TypeEnum.MAT4:
                mult = 16;
                break;
            }

            return(mult);
        }
Esempio n. 3
0
        private int GetTypeSize(Accessor.TypeEnum type)
        {
            switch (type)
            {
            case Accessor.TypeEnum.SCALAR:
                return(1);

            case Accessor.TypeEnum.VEC2:
                return(2);

            case Accessor.TypeEnum.VEC3:
                return(3);

            case Accessor.TypeEnum.VEC4:
                return(4);

            case Accessor.TypeEnum.MAT2:
                return(4);

            case Accessor.TypeEnum.MAT3:
                return(9);

            case Accessor.TypeEnum.MAT4:
                return(16);
            }

            throw new InvalidEnumArgumentException("Unknown type!");
        }
Esempio n. 4
0
        public static int NumOfComponents(this Accessor.TypeEnum t)
        {
            switch (t)
            {
            case Accessor.TypeEnum.Scalar:
                return(1);

            case Accessor.TypeEnum.Vec2:
                return(2);

            case Accessor.TypeEnum.Vec3:
                return(3);

            case Accessor.TypeEnum.Vec4:
                return(4);

            case Accessor.TypeEnum.Mat2:
                return(4);

            case Accessor.TypeEnum.Mat3:
                return(9);

            case Accessor.TypeEnum.Mat4:
                return(16);

            default:
                throw new NotImplementedException();
            }
        }
Esempio n. 5
0
        public static int GetComponentDimension(this Accessor.TypeEnum type)
        {
            switch (type)
            {
            case Accessor.TypeEnum.SCALAR:
                return(1);

            case Accessor.TypeEnum.VEC2:
                return(2);

            case Accessor.TypeEnum.VEC3:
                return(3);

            case Accessor.TypeEnum.VEC4:
            case Accessor.TypeEnum.MAT2:
                return(4);

            case Accessor.TypeEnum.MAT3:
                return(9);

            case Accessor.TypeEnum.MAT4:
                return(16);

            default:
                return(0);
            }
        }
Esempio n. 6
0
 private T[] ReadTypeArray <T>(System.Func <T> read, Accessor.TypeEnum expectedType)
 {
     if (m_accessor.Type != expectedType)
     {
         throw new System.TypeLoadException("Wrong expected type: " + expectedType + " got " + m_accessor.Type);
     }
     T[] res = new T[m_accessor.Count];
     for (int i = 0; i < m_accessor.Count; ++i)
     {
         res[i]    = read();
         m_offset += m_skip;
     }
     return(res);
 }
Esempio n. 7
0
        private static int AddAccessor(this Gltf gltf, int bufferView, int byteOffset, Accessor.ComponentTypeEnum componentType, int count, float[] min, float[] max, Accessor.TypeEnum accessorType)
        {
            var a = new Accessor();

            a.BufferView    = bufferView;
            a.ByteOffset    = byteOffset;
            a.ComponentType = componentType;
            a.Min           = min;
            a.Max           = max;
            a.Type          = accessorType;
            a.Count         = count;

            if (gltf.Accessors != null)
            {
                var accessors = gltf.Accessors.ToList();
                accessors.Add(a);
                gltf.Accessors = accessors.ToArray();
            }
            else
            {
                gltf.Accessors = new [] { a };
            }

            return(gltf.Accessors.Length - 1);
        }
Esempio n. 8
0
        private static int AddAccessor(List <Accessor> accessors, int bufferView, int byteOffset, Accessor.ComponentTypeEnum componentType, int count, float[] min, float[] max, Accessor.TypeEnum accessorType)
        {
            var a = new Accessor();

            a.BufferView    = bufferView;
            a.ByteOffset    = byteOffset;
            a.ComponentType = componentType;
            a.Min           = min;
            a.Max           = max;
            a.Type          = accessorType;
            a.Count         = count;

            accessors.Add(a);

            return(accessors.Count - 1);
        }
Esempio n. 9
0
        private static MgFormat DetermineFormat(Accessor.TypeEnum type, Accessor.ComponentTypeEnum componentType)
        {
            if (type == Accessor.TypeEnum.SCALAR)
            {
                switch (componentType)
                {
                case Accessor.ComponentTypeEnum.BYTE:
                    return(MgFormat.R8_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                    return(MgFormat.R8_UINT);

                case Accessor.ComponentTypeEnum.FLOAT:
                    return(MgFormat.R32_SFLOAT);

                case Accessor.ComponentTypeEnum.SHORT:
                    return(MgFormat.R16_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                    return(MgFormat.R16_UINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_INT:
                    return(MgFormat.R32_UINT);

                default:
                    throw new NotSupportedException();
                }
            }
            else if (type == Accessor.TypeEnum.VEC2)
            {
                switch (componentType)
                {
                case Accessor.ComponentTypeEnum.BYTE:
                    return(MgFormat.R8G8_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                    return(MgFormat.R8G8_UINT);

                case Accessor.ComponentTypeEnum.FLOAT:
                    return(MgFormat.R32G32_SFLOAT);

                case Accessor.ComponentTypeEnum.SHORT:
                    return(MgFormat.R16G16_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                    return(MgFormat.R16G16_UINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_INT:
                    return(MgFormat.R32G32_UINT);

                default:
                    throw new NotSupportedException();
                }
            }
            else if (type == Accessor.TypeEnum.VEC3)
            {
                switch (componentType)
                {
                case Accessor.ComponentTypeEnum.BYTE:
                    return(MgFormat.R8G8B8_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                    return(MgFormat.R8G8B8_UINT);

                case Accessor.ComponentTypeEnum.FLOAT:
                    return(MgFormat.R32G32B32_SFLOAT);

                case Accessor.ComponentTypeEnum.SHORT:
                    return(MgFormat.R16G16B16_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                    return(MgFormat.R16G16B16_UINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_INT:
                    return(MgFormat.R32G32B32_UINT);

                default:
                    throw new NotSupportedException();
                }
            }
            else if (type == Accessor.TypeEnum.VEC4)
            {
                switch (componentType)
                {
                case Accessor.ComponentTypeEnum.BYTE:
                    return(MgFormat.R8G8B8A8_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                    return(MgFormat.R8G8B8A8_UINT);

                case Accessor.ComponentTypeEnum.FLOAT:
                    return(MgFormat.R32G32B32A32_SFLOAT);

                case Accessor.ComponentTypeEnum.SHORT:
                    return(MgFormat.R16G16B16A16_SINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                    return(MgFormat.R16G16B16A16_UINT);

                case Accessor.ComponentTypeEnum.UNSIGNED_INT:
                    return(MgFormat.R32G32B32A32_UINT);

                default:
                    throw new NotSupportedException();
                }
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Esempio n. 10
0
 private static int GetCountOfAccessorType(Accessor.TypeEnum type) => type switch
 {
Esempio n. 11
0
        public static dynamic AccessBuffer(Accessor.TypeEnum accessorType, int count, Accessor.ComponentTypeEnum componentType, int stride, byte[] arr)
        {
            dynamic result = null;

            var elementCount   = count;                                     //how many times do we need to do this?
            var componentCount = GetTypeMultiplier(accessorType);;          //each time we do this, how many times do I need to read the buffer?
            var byteCount      = GetComponentTypeMultiplier(componentType); //how many bytes is each component from ComponentTypeEnum
            var elementBytes   = componentCount * byteCount;

            var strideDiff = stride > 0 ? stride - elementBytes : 0;

            using (var memoryStream = new MemoryStream(arr))
                using (var reader = new BinaryReader(memoryStream))
                {
                    // TODO: clean this up
                    switch (componentType)
                    {
                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.BYTE:

                        var listSByte = new List <sbyte>();

                        //loop through element count

                        //loop through component count
                        //if stride, position the reader appropriately
                        // element bytes should be byteCount * componentCount
                        // stride diff should be stride - elementbytes

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listSByte.Add(reader.ReadSByte());
                            }
                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listSByte;

                        break;

                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT:

                        var listSingle = new List <Single>();

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listSingle.Add(reader.ReadSingle());
                            }
                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listSingle;

                        break;

                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.SHORT:

                        var listShort = new List <Int16>();

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listShort.Add(reader.ReadInt16());
                            }
                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listShort;

                        break;

                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE:

                        var listByte = new List <byte>();

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listByte.Add(reader.ReadByte());
                            }
                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listByte;

                        break;

                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_INT:

                        var listUInt = new List <uint>();

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listUInt.Add(reader.ReadUInt32());
                            }
                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listUInt;

                        break;

                    case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT:

                        var listUInt16 = new List <UInt16>();

                        for (int i = 0; i < elementCount; i++)
                        {
                            for (int j = 0; j < componentCount; j++)
                            {
                                listUInt16.Add(reader.ReadUInt16());
                            }

                            reader.BaseStream.Position += strideDiff;
                        }

                        result = listUInt16;

                        break;
                    }
                }

            return(result);
        }