Esempio n. 1
0
        private static byte[] WriteShape(int id, VoxelData.ShapeData shape)
        {
            List <byte> bytes = new List <byte>();

            bytes.AddRange(GetBytes(id));
            bytes.AddRange(GetBytes(shape.Attributes.Count));
            foreach (var att in shape.Attributes)
            {
                AddVoxStringBytes(ref bytes, att.Key);
                AddVoxStringBytes(ref bytes, att.Value);
            }
            bytes.AddRange(GetBytes(shape.ModelData.Length));
            for (int i = 0; i < shape.ModelData.Length; i++)
            {
                var pair = shape.ModelData[i];
                bytes.AddRange(GetBytes(pair.Key));
                bytes.AddRange(GetBytes(pair.Value.Count));
                foreach (var att in pair.Value)
                {
                    AddVoxStringBytes(ref bytes, att.Key);
                    AddVoxStringBytes(ref bytes, att.Value);
                }
            }
            return(ToChrunkByte(bytes, "nSHP"));
        }
Esempio n. 2
0
        public VoxelData GetVoxelData()
        {
            var data = new VoxelData()
            {
                Version    = Version,
                Voxels     = new List <int[, , ]>(),
                Palette    = new List <Color>(Palette),
                Materials  = new List <VoxelData.MaterialData>(Materials),
                Transforms = new Dictionary <int, VoxelData.TransformData>(),
                Groups     = new Dictionary <int, VoxelData.GroupData>(),
                Shapes     = new Dictionary <int, VoxelData.ShapeData>(),
                Rigs       = new Dictionary <int, VoxelData.RigData>(),
            };

            // Voxels
            for (int i = 0; i < Voxels.Length; i++)
            {
                var sourceV = Voxels[i];
                int[,,] vs = new int[sourceV.SizeX, sourceV.SizeY, sourceV.SizeZ];
                for (int x = 0; x < sourceV.SizeX; x++)
                {
                    for (int y = 0; y < sourceV.SizeY; y++)
                    {
                        for (int z = 0; z < sourceV.SizeZ; z++)
                        {
                            vs[x, y, z] = sourceV.Get(x, y, z);
                        }
                    }
                }
                data.Voxels.Add(vs);
            }

            // Transforms
            for (int i = 0; i < Transforms.Length; i++)
            {
                data.Transforms.Add(TransformIndexs[i], Transforms[i]);
            }

            // Groups
            for (int i = 0; i < Groups.Length; i++)
            {
                var groupData = new VoxelData.GroupData()
                {
                    ChildNodeId = Groups[i].ChildNodeId,
                    Attributes  = new Dictionary <string, string>(),
                };
                for (int j = 0; j < Groups[i].AttKeys.Length; j++)
                {
                    groupData.Attributes.Add(Groups[i].AttKeys[j], Groups[i].Attributes[j]);
                }
                data.Groups.Add(Groups[i].Index, groupData);
            }

            // Shapes
            for (int i = 0; i < Shapes.Length; i++)
            {
                var sourceShape = Shapes[i];
                var shapeData   = new VoxelData.ShapeData()
                {
                    Attributes = new Dictionary <string, string>(),
                    ModelData  = new KeyValuePair <int, Dictionary <string, string> > [sourceShape.ModelDatas.Length],
                };
                for (int j = 0; j < sourceShape.Attributes.Length; j++)
                {
                    shapeData.Attributes.Add(sourceShape.AttKeys[j], sourceShape.Attributes[j]);
                }
                for (int j = 0; j < sourceShape.ModelDatas.Length; j++)
                {
                    shapeData.ModelData[j] = new KeyValuePair <int, Dictionary <string, string> >(
                        sourceShape.ModelDataIndexs[j],
                        new Dictionary <string, string>()
                        );
                    var sourceData = sourceShape.ModelDatas[j];
                    for (int k = 0; k < sourceData.Value.Length; k++)
                    {
                        shapeData.ModelData[j].Value.Add(sourceData.Key[k], sourceData.Value[k]);
                    }
                }
                data.Shapes.Add(sourceShape.Index, shapeData);
            }

            // Rig
            for (int i = 0; i < Rigs.Length; i++)
            {
                var bones = Rigs[i].Bones;
                for (int j = 0; j < bones.Count; j++)
                {
                    bones[j].Parent = null;
                    int pIndex = bones[j].ParentIndex;
                    if (pIndex >= 0 && pIndex < bones.Count)
                    {
                        bones[j].Parent = bones[pIndex];
                    }
                }
                data.Rigs.Add(RigIndexs[i], Rigs[i]);
            }

            return(data);
        }