Пример #1
0
        public List <int> GetIndices(int primitiveIndex)
        {
            List <int> indices = new List <int>();

            GLBPrimitive  primitive  = Primitives[primitiveIndex];
            GLBBufferView bufferView = primitive.Indices.BufferView;

            for (int bytePointer = 0; bytePointer < bufferView.ByteLength; bytePointer += sizeof(short))
            {
                int index = System.BitConverter.ToInt16(bufferView.Buffer, bufferView.ByteOffset + bytePointer);

                indices.Add(index);
            }

            if (indices.Count != primitive.Indices.Count)
            {
                throw new Exception("Indices count does not equal expected count");
            }

            return(indices);
        }
Пример #2
0
        public List <Vertex> GetVertices(int primitiveIndex)
        {
            List <Vertex> vertices = new List <Vertex>();

            GLBPrimitive  primitive  = Primitives[primitiveIndex];
            GLBBufferView bufferView = primitive.Attributes.Position.BufferView;

            for (int bytePointer = 0; bytePointer < bufferView.ByteLength; bytePointer += 3 * sizeof(float))
            {
                float x = System.BitConverter.ToSingle(bufferView.Buffer, bufferView.ByteOffset + bytePointer + sizeof(float) * 0);
                float y = System.BitConverter.ToSingle(bufferView.Buffer, bufferView.ByteOffset + bytePointer + sizeof(float) * 1);
                float z = System.BitConverter.ToSingle(bufferView.Buffer, bufferView.ByteOffset + bytePointer + sizeof(float) * 2);

                Vertex vertex = new Vertex(x, y, z);
                vertices.Add(vertex);
            }

            if (vertices.Count != primitive.Attributes.Position.Count)
            {
                throw new Exception("Vertices count does not equal expected count");
            }

            return(vertices);
        }
Пример #3
0
        /*
         * public byte[] GetBuffer(int bufferIndex)
         * {
         *  return FileChunks[bufferIndex + 1].Data;
         * }
         */

        public void ParseJSON(string JSONstring)
        {
            var json = JsonConvert.DeserializeObject <dynamic>(JSONstring);

            foreach (var bufferViewJSON in json.bufferViews)
            {
                GLBBufferView bufferView = new GLBBufferView();

                bufferView.Buffer     = FileChunks[(int)bufferViewJSON.buffer + 1].Data;
                bufferView.ByteOffset = bufferViewJSON.byteOffset;
                bufferView.ByteLength = bufferViewJSON.byteLength;
                bufferView.Target     = bufferViewJSON.target;

                BufferViews.Add(bufferView);
            }

            foreach (var accessorJSON in json.accessors)
            {
                GLBAccessor accessor = new GLBAccessor();

                accessor.BufferView    = BufferViews[(int)accessorJSON.bufferView];
                accessor.ComponentType = accessorJSON.componentType;
                accessor.Count         = accessorJSON.count;
                accessor.Type          = accessorJSON.type;

                Accessors.Add(accessor);
            }

            foreach (var imageJSON in json.images)
            {
                GLBImage image = new GLBImage();

                image.BufferView = BufferViews[(int)imageJSON.bufferView];
                image.MimeType   = imageJSON.mimeType;

                Images.Add(image);
            }

            foreach (var samplerJSON in json.samplers)
            {
                GLBSampler sampler = new GLBSampler();

                sampler.MinFilter = samplerJSON.minFilter;
                sampler.MagFilter = samplerJSON.magFilter;
                sampler.WrapS     = samplerJSON.wrapS;
                sampler.WrapT     = samplerJSON.wrapT;

                Samplers.Add(sampler);
            }

            foreach (var textureJSON in json.textures)
            {
                GLBTexture texture = new GLBTexture();

                texture.Source  = Images[(int)textureJSON.source];
                texture.Sampler = Samplers[(int)textureJSON.sampler];

                Textures.Add(texture);
            }

            foreach (var materialJSON in json.materials)
            {
                GLBMaterial material = new GLBMaterial();

                material.Name = materialJSON.name;

                Materials.Add(material);
            }

            foreach (var meshJSON in json.meshes)
            {
                GLBMesh mesh = new GLBMesh();

                mesh.Name = meshJSON.name;

                foreach (var primitiveJSON in meshJSON.primitives)
                {
                    GLBPrimitive primitive = new GLBPrimitive();

                    GLBAttributes attributes = new GLBAttributes();

                    attributes.Position = Accessors[(int)primitiveJSON.attributes.POSITION];
                    attributes.Normal   = Accessors[(int)primitiveJSON.attributes.NORMAL];

                    primitive.Attributes = attributes;
                    primitive.Indices    = Accessors[(int)primitiveJSON.indices];
                    primitive.Material   = Materials[(int)primitiveJSON.material];

                    mesh.Primitives.Add(primitive);
                }

                Meshes.Add(mesh);
            }
        }