Example #1
0
    public override Chunk parse(BinaryReader reader)
    {
        GroupNodeChunk chunk = new GroupNodeChunk();

        chunk.bytesInChunk    = reader.ReadInt32();
        chunk.bytesInChildren = reader.ReadInt32();
        chunk.nodeId          = reader.ReadInt32();
        chunk.attributes      = MagicaDictionary.readDictionary(reader);

        int numberOfChildNodes = reader.ReadInt32();

        for (int i = 0; i < numberOfChildNodes; i++)
        {
            chunk.childNodeIds.Add(reader.ReadInt32());
        }

        long currentPosition = reader.BaseStream.Position;

        while (reader.BaseStream.Position < currentPosition + chunk.bytesInChildren)
        {
            chunk.children.Add(Chunk.createChunk(reader));
        }

        return(chunk);
    }
Example #2
0
        private GroupNodeChunk ReadGroupNodeChunk(BinaryReader chunkReader)
        {
            var groupNodeChunk = new GroupNodeChunk
            {
                id         = chunkReader.ReadInt32(),
                attributes = ReadDICT(chunkReader),
                childIds   = ReadArray(chunkReader, r => r.ReadInt32())
            };

            return(groupNodeChunk);
        }
Example #3
0
        /// <summary>
        /// Main loop for write all chunks
        /// </summary>
        /// <param name="writer"></param>
        private int WriteChunks(BinaryWriter writer)
        {
            int byteWritten = 0;
            int RGBA        = WritePaletteChunk(writer);

            int MATL = 0;

            for (int i = 0; i < 256; i++)
            {
                if (_usedIndexColors.ContainsKey(i))
                {
                    KeyValuePair <int, int> modelIndex = _usedIndexColors[i];
                    if (_models[modelIndex.Key].materialChunks.Count > modelIndex.Value - 1)
                    {
                        MATL += WriteMaterialChunk(writer, _models[modelIndex.Key].materialChunks[modelIndex.Value - 1], i + 1);
                    }
                }
                else if (_models[0].materialChunks.Count > 0)
                {
                    MATL += WriteMaterialChunk(writer, _models[0].materialChunks[0], i + 1);
                }
            }

            int SIZE = 0;
            int XYZI = 0;

            Console.WriteLine("[LOG] Step [1/2]: Started to write SIZE and XYZI...");
            using (var progressbar = new ProgressBar())
            {
                int totalModels      = CountTotalModels();
                int indexProgression = 0;
                foreach (VoxModel model in _models)
                {
                    for (int j = 0; j < model.voxelFrames.Count; j++)
                    {
                        SIZE += WriteSizeChunk(writer, model.voxelFrames[j].GetVolumeSize());
                        XYZI += WriteXyziChunk(writer, model, j);
                        float progress = indexProgression / (float)totalModels;
                        progressbar.Report(progress);
                        indexProgression++;
                    }
                }
            }
            Console.WriteLine("[LOG] Done.");

            int nGRP = 0;
            int nTRN = 0;
            int nSHP = 0;

            int        mnTRN        = WriteMainTransformChunk(writer);
            int        indexChunk   = 2;
            List <int> mainGroupIds = new List <int>();

            mainGroupIds.Add(2);

            for (int i = 0; i < _models.Count - 1; i++)
            {
                int max         = _models[i].transformNodeChunks.Max(t => t.id);
                int transformId = max + (max % 2 == 0 ? 2 : 1) + mainGroupIds.Last();
                mainGroupIds.Add(transformId);
            }

            int mnGRP = WriteMainGroupChunk(writer, mainGroupIds);

            Console.WriteLine("[LOG] Step [2/2]: Started to write nTRN, nGRP and nSHP chunks...");
            using (var progressbar = new ProgressBar())
            {
                int indexProgression           = 0;
                int totalTransform             = CountTotalTransforms();
                Dictionary <int, int> modelIds = new Dictionary <int, int>();
                Dictionary <int, int> shapeIds = new Dictionary <int, int>();
                int indexModel = 0;
                mainGroupIds.Clear();
                mainGroupIds.Add(2);
                for (int i = 0; i < _models.Count; i++)
                {
                    for (int j = 0; j < _models[i].transformNodeChunks.Count; j++)
                    {
                        int childId = _models[i].transformNodeChunks[j].childId;

                        int transformIndexUnique = indexChunk++;

                        ShapeNodeChunk shapeNode = _models[i].shapeNodeChunks.FirstOrDefault(t => t.id == childId);
                        if (shapeNode != null)
                        {
                            int modelId          = shapeNode.models[0].modelId + i;
                            int modelIndexUnique = modelId + (i * 2000); //Hack ...

                            if (!modelIds.ContainsKey(modelIndexUnique))
                            {
                                modelIds.Add(modelIndexUnique, indexModel);
                                indexModel++;
                            }

                            int shapeIndexUnique = (shapeNode.id + ((i + 1) * 2000) + 2); //Hack
                            nTRN += WriteTransformChunk(writer, _models[i].transformNodeChunks[j], transformIndexUnique, shapeIds.ContainsKey(shapeIndexUnique) ? shapeIds[shapeIndexUnique] : indexChunk);

                            if (!shapeIds.ContainsKey(shapeIndexUnique))
                            {
                                shapeIds.Add(shapeIndexUnique, indexChunk);
                                nSHP += WriteShapeChunk(writer, indexChunk, modelIds[modelIndexUnique]);
                                indexChunk++;
                            }
                        }
                        else
                        {
                            GroupNodeChunk groupNode = _models[i].groupNodeChunks.FirstOrDefault(t => t.id == childId);

                            int groupUniqueIndex = indexChunk++;

                            List <int> childIds = groupNode.childIds.ToList();
                            for (int index = 0; index < childIds.Count; index++)
                            {
                                childIds[index] += mainGroupIds.Last();
                            }

                            nTRN += WriteTransformChunk(writer, _models[i].transformNodeChunks[j], transformIndexUnique, groupUniqueIndex);
                            nGRP += WriteGroupChunk(writer, groupUniqueIndex, childIds);
                        }

                        progressbar.Report(indexProgression / (float)totalTransform);
                        indexProgression++;
                    }

                    int max = _models[i].transformNodeChunks.Max(t => t.id);
                    mainGroupIds.Add(max + (max % 2 == 0 ? 2 : 1) + mainGroupIds.Last());
                }
            }

            Console.WriteLine("[LOG] Written RGBA: " + RGBA);
            Console.WriteLine("[LOG] Written MATL: " + MATL);
            Console.WriteLine("[LOG] Written SIZE: " + SIZE);
            Console.WriteLine("[LOG] Written XYZI: " + XYZI);
            Console.WriteLine("[LOG] Written nGRP: " + nGRP);
            Console.WriteLine("[LOG] Written nTRN: " + nTRN);
            Console.WriteLine("[LOG] Written nSHP: " + nSHP);
            Console.WriteLine("[LOG] Written mnTRN: " + mnTRN);
            Console.WriteLine("[LOG] Written mnGRP: " + mnGRP);

            byteWritten = RGBA + MATL + SIZE + XYZI + nGRP + nTRN + nSHP + mnTRN + mnGRP;
            return(byteWritten);
        }