Exemple #1
0
        public static CgfChunkHeader Read(BinaryReader br)
        {
            var header = new CgfChunkHeader();

            header.chunkType    = (CgfChunkType)br.ReadInt32();
            header.chunkVersion = br.ReadInt32();
            header.chunkOffset  = br.ReadInt32();
            header.chunkId      = br.ReadInt32();
            return(header);
        }
Exemple #2
0
        private void Load(byte[] rawBytes)
        {
            m_originalFileBytes = rawBytes;
            m_chunkHeaders      = new List <CgfChunkHeader>();
            using (var meshInputStream = new BinaryReader(new MemoryStream(m_originalFileBytes)))
            {
                byte[] signature = meshInputStream.ReadBytes(8);
                if (signature[0] != 0x4E || signature[1] != 0x43 || signature[2] != 0x41 || signature[3] != 0x69 ||
                    signature[4] != 0x6F || signature[5] != 0x6E || signature[6] != 0x00 || signature[7] != 0x00)        // NCAion
                {
                    throw new IOException("Wrong signature");
                }

                int fileType = meshInputStream.ReadInt32();
                if ((uint)fileType == 0xFFFF0001)                // animation data
                {
                    throw new InvalidOperationException("todo"); // System.out.println("Animation data");
                }
                if ((uint)fileType != 0xFFFF0000)                // static model
                {
                    throw new IOException("Wrong filetype");
                }

                meshInputStream.ReadInt32(); // unknown data

                int tableOffset = meshInputStream.ReadInt32();

                // Move to the chunks table
                meshInputStream.BaseStream.Seek(tableOffset, SeekOrigin.Begin);

                int chunksCount = meshInputStream.ReadInt32();
                for (int i = 0; i < chunksCount; i++)
                {
                    var chunkHeader = CgfChunkHeader.Read(meshInputStream);
                    m_chunkHeaders.Add(chunkHeader);
                }
            }

            for (int i = 0; i < m_chunkHeaders.Count; i++)
            {
                if (m_chunkHeaders[i].chunkType == CgfChunkType.Material)
                {
                    m_materials.Add(i, LoadMaterialData(i));
                }
            }

            // Create NodeData items
            var flatNodes = new List <NodeData>();

            for (int i = 0; i < m_chunkHeaders.Count; i++)
            {
                if (m_chunkHeaders[i].chunkType == CgfChunkType.Node)
                {
                    flatNodes.Add(GetNodeData(i));
                }
            }

            // TODO - add check to ensure all nodes are accounted for

            // Build node tree. For each node, find the parent (or root) and add it to its parent's children.
            foreach (var node in flatNodes)
            {
                if (node.parentId != -1)
                {
                    foreach (var p in flatNodes)
                    {
                        if (p.chunkId == node.parentId)
                        {
                            if (p.Children == null)
                            {
                                p.Children = new List <NodeData>();
                            }
                            p.Children.Add(node);
                            break;
                        }
                    }
                }
                else
                {
                    Nodes.Add(node); // node is top level
                }
            }
        }