Пример #1
0
        public override Mesh decode(MeshInfo minfo, CtmInputStream input)
        {
            int vc = minfo.getVertexCount();

            AttributeData[] tex = new AttributeData[minfo.getUvMapCount()];
            AttributeData[] att = new AttributeData[minfo.getAttrCount()];

            checkTag(input.readLittleInt(), INDX);
            int[] indices = readIntArray(input, minfo.getTriangleCount(), 3, false);

            checkTag(input.readLittleInt(), VERT);
            float[] vertices = readFloatArray(input, vc * Mesh.CTM_POSITION_ELEMENT_COUNT, 1);

            float[] normals = null;
            if (minfo.hasNormals())
            {
                checkTag(input.readLittleInt(), NORM);
                normals = readFloatArray(input, vc, Mesh.CTM_NORMAL_ELEMENT_COUNT);
            }

            for (int i = 0; i < tex.Length; ++i)
            {
                checkTag(input.readLittleInt(), TEXC);
                tex[i] = readUVData(vc, input);
            }

            for (int i = 0; i < att.Length; ++i)
            {
                checkTag(input.readLittleInt(), ATTR);
                att[i] = readAttrData(vc, input);
            }

            return(new Mesh(vertices, normals, indices, tex, att));
        }
Пример #2
0
        public override Mesh decode(MeshInfo minfo, CtmInputStream input)
        {
            int vc = minfo.getVertexCount();

            AttributeData[] tex = new AttributeData[minfo.getUvMapCount()];
            AttributeData[] att = new AttributeData[minfo.getAttrCount()];

            checkTag(input.readLittleInt(), INDX);
            int[] indices = readIntArray(input, minfo.getTriangleCount(), 3, false);

            checkTag(input.readLittleInt(), VERT);
            float[] vertices = readFloatArray(input, vc * Mesh.CTM_POSITION_ELEMENT_COUNT, 1);

            float[] normals = null;
            if (minfo.hasNormals()) {
                checkTag(input.readLittleInt(), NORM);
                normals = readFloatArray(input, vc, Mesh.CTM_NORMAL_ELEMENT_COUNT);
            }

            for (int i = 0; i < tex.Length; ++i) {
                checkTag(input.readLittleInt(), TEXC);
                tex[i] = readUVData(vc, input);
            }

            for (int i = 0; i < att.Length; ++i) {
                checkTag(input.readLittleInt(), ATTR);
                att[i] = readAttrData(vc, input);
            }

            return new Mesh(vertices, normals, indices, tex, att);
        }
Пример #3
0
        private float[] readVertices(CtmInputStream input, Grid grid, int vcount, float precision)
        {
            checkTag(input.readLittleInt(), VERT);
            int[] intVertices = input.readPackedInts(vcount, Mesh.CTM_POSITION_ELEMENT_COUNT, false);

            checkTag(input.readLittleInt(), GIDX);
            int[] gridIndices = input.readPackedInts(vcount, 1, false);
            for (int i = 1; i < vcount; i++)
            {
                gridIndices[i] += gridIndices[i - 1];
            }

            return(CommonAlgorithm.restoreVertices(intVertices, gridIndices, grid, precision));
        }
Пример #4
0
        public void  decode(ref List <Mesh> meshList)
        {
            if (decoded)
            {
                throw new Exception("Ctm File got already decoded");
            }
            decoded = true;

            if (input.readLittleInt() != OCTM)
            {
                throw new BadFormatException("The CTM file doesn't start with the OCTM tag!");
            }


            int formatVersion = input.readLittleInt();
            int methodTag     = input.readLittleInt();

            MeshInfo mi = new MeshInfo(input.readLittleInt(),  //vertex count
                                       input.readLittleInt(),  //triangle count
                                       input.readLittleInt(),  //uvmap count
                                       input.readLittleInt(),  //attribute count
                                       input.readLittleInt()); //flags

            comment = input.readString();


            // Uncompress from stream
            Mesh ctmMesh = null;

            foreach (MeshDecoder md in DECODER)
            {
                if (md.isFormatSupported(methodTag, formatVersion))
                {
                    ctmMesh = md.decode(mi, input);
                    break;
                }
            }

            if (ctmMesh == null)
            {
                throw new IOException("No sutible decoder found for Mesh of compression type: " + unpack(methodTag) + ", version " + formatVersion);
            }

            // Check mesh integrity
            ctmMesh.checkIntegrity();

            // Unity only support maximum 65534 vertices per mesh. So large meshes need to be splitted.
            if (ctmMesh.vertices.Length > maxNumVerticesPerMesh * 3)
            {
                SplitMesh(ctmMesh, ref meshList);
            }
            else
            {
                meshList.Add(ctmMesh);
            }

            //return m;
        }
Пример #5
0
 private float[] readNormals(CtmInputStream input, float[] vertices, int[] indices,
                             float normalPrecision, int vcount)
 {
     checkTag(input.readLittleInt(), NORM);
     int[] intNormals = input.readPackedInts(vcount, Mesh.CTM_NORMAL_ELEMENT_COUNT, false);
     return(restoreNormals(intNormals, vertices, indices, normalPrecision));
 }
Пример #6
0
        public override Mesh decode(MeshInfo minfo, CtmInputStream input)
        {
            int vc = minfo.getVertexCount();

            checkTag(input.readLittleInt(), MG2_HEADER_TAG);
            float vertexPrecision = input.readLittleFloat();
            float normalPrecision = input.readLittleFloat();

            Grid grid = Grid.fromStream(input);
            if(!grid.checkIntegrity()) {
                throw new InvalidDataException("The vertex size grid is corrupt!");
            }

            float[] vertices = readVertices(input, grid, vc, vertexPrecision);

            int[] indices = readIndices(input, minfo.getTriangleCount(), vc);

            float[] normals = null;
            if (minfo.hasNormals()) {
                normals = readNormals(input, vertices, indices, normalPrecision, vc);
            }

            AttributeData[] uvData = new AttributeData[minfo.getUvMapCount()];
            for (int i = 0; i < uvData.Length; i++) {
                uvData[i] = readUvData(input, vc);
            }

            AttributeData[] attributs = new AttributeData[minfo.getAttrCount()];
            for (int i = 0; i < attributs.Length; i++) {
                attributs[i] = readAttribute(input, vc);
            }

            return new Mesh(vertices, normals, indices, uvData, attributs);
        }
Пример #7
0
 protected virtual int[] readIntArray(CtmInputStream input, int count, int size, bool signed)
 {
     int[] array = new int[count * size];
     for (int i = 0; i < array.Length; i++) {
         array[i] = input.readLittleInt();
     }
     return array;
 }
Пример #8
0
 protected virtual int[] readIntArray(CtmInputStream input, int count, int size, bool signed)
 {
     int[] array = new int[count * size];
     for (int i = 0; i < array.Length; i++)
     {
         array[i] = input.readLittleInt();
     }
     return(array);
 }
Пример #9
0
 private int[] readIndices(CtmInputStream input, int triCount, int vcount)
 {
     checkTag(input.readLittleInt(), INDX);
     int[] indices = input.readPackedInts(triCount, 3, false);
     restoreIndices(triCount, indices);
     foreach(int i in indices) {
         if (i > vcount) {
             throw new InvalidDataException("One element of the indice array "
                                            + "points to a none existing vertex(id: " + i + ")");
         }
     }
     return indices;
 }
Пример #10
0
        private AttributeData readAttribute(CtmInputStream input, int vc)
        {
            checkTag(input.readLittleInt(), ATTR);

            String name = input.readString();
            float precision = input.readLittleFloat();
            if (precision <= 0f) {
                throw new InvalidDataException("An attribute precision value <= 0.0 was read");
            }

            int[] intData = input.readPackedInts(vc, Mesh.CTM_ATTR_ELEMENT_COUNT, true);
            float[] data = restoreAttribs(precision, intData);

            return new AttributeData(name, null, precision, data);
        }
Пример #11
0
        public Mesh decode()
        {
            if (decoded)
            {
                throw new Exception("Ctm File got already decoded");
            }
            decoded = true;

            if (input.readLittleInt() != OCTM)
            {
                throw new BadFormatException("The CTM file doesn't start with the OCTM tag!");
            }

            int formatVersion = input.readLittleInt();
            int methodTag     = input.readLittleInt();

            MeshInfo mi = new MeshInfo(input.readLittleInt(),  //vertex count
                                       input.readLittleInt(),  //triangle count
                                       input.readLittleInt(),  //uvmap count
                                       input.readLittleInt(),  //attribute count
                                       input.readLittleInt()); //flags

            comment = input.readString();

            // Uncompress from stream
            Mesh m = null;

            foreach (MeshDecoder md in DECODER)
            {
                if (md.isFormatSupported(methodTag, formatVersion))
                {
                    m = md.decode(mi, input);
                    break;
                }
            }

            if (m == null)
            {
                throw new IOException("No sutible decoder found for Mesh of compression type: " + unpack(methodTag) + ", version " + formatVersion);
            }

            // Check mesh integrity
            m.checkIntegrity();

            return(m);
        }
Пример #12
0
 private int[] readIndices(CtmInputStream input, int triCount, int vcount)
 {
     checkTag(input.readLittleInt(), INDX);
     int[] indices = input.readPackedInts(triCount, 3, false);
     restoreIndices(triCount, indices);
     foreach (int i in indices)
     {
         if (i > vcount)
         {
             throw new InvalidDataException("One element of the indice array "
                                            + "points to a none existing vertex(id: " + i + ")");
         }
     }
     return(indices);
 }
Пример #13
0
        private AttributeData readAttribute(CtmInputStream input, int vc)
        {
            checkTag(input.readLittleInt(), ATTR);

            String name      = input.readString();
            float  precision = input.readLittleFloat();

            if (precision <= 0f)
            {
                throw new InvalidDataException("An attribute precision value <= 0.0 was read");
            }

            int[]   intData = input.readPackedInts(vc, Mesh.CTM_ATTR_ELEMENT_COUNT, true);
            float[] data    = restoreAttribs(precision, intData);

            return(new AttributeData(name, null, precision, data));
        }
Пример #14
0
        private AttributeData readUvData(CtmInputStream input, int vcount)
        {
            checkTag(input.readLittleInt(), TEXC);
            String name      = input.readString();
            String material  = input.readString();
            float  precision = input.readLittleFloat();

            if (precision <= 0f)
            {
                throw new InvalidDataException("A uv precision value <= 0.0 was read");
            }

            int[]   intCoords = input.readPackedInts(vcount, Mesh.CTM_UV_ELEMENT_COUNT, true);
            float[] data      = restoreUVCoords(precision, intCoords);

            return(new AttributeData(name, material, precision, data));
        }
        protected virtual int[] readIntArray(CtmInputStream input, int count, int size, bool signed)
        {
            //int[] array = new int[count * size];
            //for (int i = 0; i < array.Length; i++) {
            //    array[i] = input.readLittleInt();
            //}
            //return array;

            intArray.Clear();
            int length = count * size;

            for (int i = 0; i < length; i++)
            {
                intArray.Add(input.readLittleInt());
            }

            return(intArray.ToArray());
        }
Пример #16
0
        public override Mesh decode(MeshInfo minfo, CtmInputStream input)
        {
            int vc = minfo.getVertexCount();

            checkTag(input.readLittleInt(), MG2_HEADER_TAG);
            float vertexPrecision = input.readLittleFloat();
            float normalPrecision = input.readLittleFloat();

            Grid grid = Grid.fromStream(input);

            if (!grid.checkIntegrity())
            {
                throw new InvalidDataException("The vertex size grid is corrupt!");
            }

            float[] vertices = readVertices(input, grid, vc, vertexPrecision);

            int[] indices = readIndices(input, minfo.getTriangleCount(), vc);

            float[] normals = null;
            if (minfo.hasNormals())
            {
                normals = readNormals(input, vertices, indices, normalPrecision, vc);
            }

            AttributeData[] uvData = new AttributeData[minfo.getUvMapCount()];
            for (int i = 0; i < uvData.Length; i++)
            {
                uvData[i] = readUvData(input, vc);
            }

            AttributeData[] attributs = new AttributeData[minfo.getAttrCount()];
            for (int i = 0; i < attributs.Length; i++)
            {
                attributs[i] = readAttribute(input, vc);
            }

            return(new Mesh(vertices, normals, indices, uvData, attributs));
        }
Пример #17
0
        private AttributeData readUvData(CtmInputStream input, int vcount)
        {
            checkTag(input.readLittleInt(), TEXC);
            String name = input.readString();
            String material = input.readString();
            float precision = input.readLittleFloat();
            if (precision <= 0f) {
                throw new InvalidDataException("A uv precision value <= 0.0 was read");
            }

            int[] intCoords = input.readPackedInts(vcount, Mesh.CTM_UV_ELEMENT_COUNT, true);
            float[] data = restoreUVCoords(precision, intCoords);

            return new AttributeData(name, material, precision, data);
        }
Пример #18
0
        private float[] readNormals(CtmInputStream input, float[] vertices, int[] indices,
	                                float normalPrecision, int vcount)
        {
            checkTag(input.readLittleInt(), NORM);
            int[] intNormals = input.readPackedInts(vcount, Mesh.CTM_NORMAL_ELEMENT_COUNT, false);
            return restoreNormals(intNormals, vertices, indices, normalPrecision);
        }
Пример #19
0
        private float[] readVertices(CtmInputStream input, Grid grid, int vcount, float precision)
        {
            checkTag(input.readLittleInt(), VERT);
            int[] intVertices = input.readPackedInts(vcount, Mesh.CTM_POSITION_ELEMENT_COUNT, false);

            checkTag(input.readLittleInt(), GIDX);
            int[] gridIndices = input.readPackedInts(vcount, 1, false);
            for (int i = 1; i < vcount; i++) {
                gridIndices[i] += gridIndices[i - 1];
            }

            return CommonAlgorithm.restoreVertices(intVertices, gridIndices, grid, precision);
        }