Exemple #1
0
        int ComponentSize(glTFLoader.Schema.Accessor.ComponentTypeEnum componentType)
        {
            switch (componentType)
            {
            case glTFLoader.Schema.Accessor.ComponentTypeEnum.BYTE:
                return(sizeof(sbyte));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.SHORT:
                return(sizeof(short));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                return(sizeof(byte));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                return(sizeof(ushort));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_INT:
                return(sizeof(uint));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT:
                return(sizeof(float));

            default:
                return(sizeof(byte));
            }
        }
Exemple #2
0
        private int GetTypeSize(glTFLoader.Schema.Accessor.ComponentTypeEnum componentType)
        {
            switch (componentType)
            {
            case glTFLoader.Schema.Accessor.ComponentTypeEnum.BYTE:
                return(sizeof(sbyte));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                return(sizeof(byte));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.SHORT:
                return(sizeof(short));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                return(sizeof(ushort));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_INT:
                return(sizeof(uint));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT:
                return(sizeof(float));

            default:
                throw new InvalidOperationException($"Invalid component type ({componentType})");
            }
        }
Exemple #3
0
        private object GetElement(glTFLoader.Schema.Accessor.ComponentTypeEnum componentType, byte[] buffer, int index)
        {
            switch (componentType)
            {
            case glTFLoader.Schema.Accessor.ComponentTypeEnum.BYTE:
                return((sbyte)buffer[index]);

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE:
                return(buffer[index]);

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.SHORT:
                return(BitConverter.ToInt16(buffer, index));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT:
                return(BitConverter.ToUInt16(buffer, index));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_INT:
                return(BitConverter.ToUInt32(buffer, index));

            case glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT:
                return(BitConverter.ToSingle(buffer, index));

            default:
                throw new InvalidOperationException($"Invalid component type ({componentType})");
            }
        }
Exemple #4
0
        private int AddIndices(List <int> indices)
        {
            // evaulate min max
            //
            var MinMaxV = indices.Aggregate(new
            {
                MinV = int.MaxValue,
                MaxV = int.MinValue
            },
                                            (accumulator, o) => new
            {
                MinV = Math.Min(o, accumulator.MinV),
                MaxV = Math.Max(o, accumulator.MaxV)
            });


            gltf.Accessor.ComponentTypeEnum ct = gltf.Accessor.ComponentTypeEnum.BYTE;
            // depending on the count of positions and normals, we determine the index type
            //
            Func <int, byte[]> ToBits;
            var size = 0;

            if (!Prevent8bitIndices && MinMaxV.MaxV <= Math.Pow(2, 8))
            {
                ct     = gltf.Accessor.ComponentTypeEnum.UNSIGNED_BYTE;
                size   = sizeof(byte);
                ToBits = x => new byte[] { (byte)x };
            }
            else if (MinMaxV.MaxV <= Math.Pow(2, 16))
            {
                ct     = gltf.Accessor.ComponentTypeEnum.UNSIGNED_SHORT;
                size   = sizeof(short);
                ToBits = x => BitConverter.GetBytes((short)x);
            }
            else
            {
                ct     = gltf.Accessor.ComponentTypeEnum.UNSIGNED_INT;
                size   = sizeof(int);
                ToBits = x => BitConverter.GetBytes((int)x);
            }

            // the offset position needs to be a multiple of the size
            // (this is from a warning we received in beta testing)
            // so we inject some padding when needed
            //
            var padding = _indicesBuffer.Count % size;

            for (int i = 0; i < padding; i++)
            {
                _indicesBuffer.Add(0);
            }

            var indAccessor = new gltf.Accessor
            {
                BufferView    = 0,
                ComponentType = ct,
                ByteOffset    = _indicesBuffer.Count,
                Normalized    = false,
                Type          = gltf.Accessor.TypeEnum.SCALAR,
                Count         = indices.Count,
                Min           = new float[] { MinMaxV.MinV },
                Max           = new float[] { MinMaxV.MaxV }
            };

            var         IndexSize         = indices.Count * size;
            List <byte> indicesBufferData = new List <byte>(IndexSize);

            foreach (var index in indices)
            {
                var lst = ToBits(index);
                indicesBufferData.AddRange(lst);
            }
            _indicesBuffer.AddRange(indicesBufferData);
            var thisIndex = _accessors.Count;

            _accessors.Add(indAccessor);
            return(thisIndex);
        }
Exemple #5
0
 int TotalStride(glTFLoader.Schema.Accessor.ComponentTypeEnum componentType, glTFLoader.Schema.Accessor.TypeEnum type)
 {
     return(ComponentSize(componentType) * ComponentsCount(type));
 }