Пример #1
0
        private static byte[] GetQbFromVoxelData(VoxelData data)
        {
            List <byte> bytes = new List <byte>();

            bytes.AddRange(System.BitConverter.GetBytes((uint)257));
            bytes.AddRange(System.BitConverter.GetBytes((uint)0));       // Always RGBA
            bytes.AddRange(System.BitConverter.GetBytes((uint)1));       // Always Right Handed
            bytes.AddRange(System.BitConverter.GetBytes(0));             // Always Not Compressed
            bytes.AddRange(System.BitConverter.GetBytes((uint)1));
            bytes.AddRange(System.BitConverter.GetBytes((uint)data.Voxels.Count));

            for (int index = 0; index < data.Voxels.Count; index++)
            {
                var voxels = data.Voxels[index];

                // Get Size
                int sizeX = voxels.GetLength(0);
                int sizeY = voxels.GetLength(1);
                int sizeZ = voxels.GetLength(2);

                // Get Position
                Vector3 size = data.GetModelSize(index);
                Vector3 pos, rot, scl;
                data.GetModelTransform(index, out pos, out rot, out scl);
                int posX = (int)pos.x - (int)size.x / 2;
                int posY = (int)pos.y - (int)size.y / 2;
                int posZ = (int)pos.z - (int)size.z / 2;

                // name
                bytes.Add(0);

                // size
                bytes.AddRange(System.BitConverter.GetBytes(sizeX));
                bytes.AddRange(System.BitConverter.GetBytes(sizeY));
                bytes.AddRange(System.BitConverter.GetBytes(sizeZ));

                // pos
                bytes.AddRange(System.BitConverter.GetBytes(posX));
                bytes.AddRange(System.BitConverter.GetBytes(posY));
                bytes.AddRange(System.BitConverter.GetBytes(posZ));

                // voxels
                for (int z = 0; z < sizeZ; z++)
                {
                    for (int y = 0; y < sizeY; y++)
                    {
                        for (int x = 0; x < sizeX; x++)
                        {
                            int v = voxels[x, y, z];
                            bytes.AddRange(System.BitConverter.GetBytes(
                                               v == 0 ? 0 : ColorToInt(data.GetColorFromPalette(v))
                                               ));
                        }
                    }
                }
            }


            return(bytes.ToArray());
        }
Пример #2
0
        // LGC
        private static Voxel[,,] GetVoxels(VoxelData voxelData, int modelIndex)
        {
            var size  = voxelData.GetModelSize(modelIndex);
            int sizeX = (int)size.x;
            int sizeY = (int)size.z;
            int sizeZ = (int)size.y;

            Voxel[,,] voxels = new Voxel[sizeX, sizeY, sizeZ];
            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j < sizeY; j++)
                {
                    for (int k = 0; k < sizeZ; k++)
                    {
                        voxels[i, j, k].Init();
                        voxels[i, j, k].ColorIndex = voxelData.Voxels[modelIndex][i, k, j];
                    }
                }
            }
            for (int i = 0; i < sizeX; i++)
            {
                for (int j = 0; j < sizeY; j++)
                {
                    for (int k = 0; k < sizeZ; k++)
                    {
                        if (voxels[i, j, k].IsEmpty)
                        {
                            voxels[i, j, k].IsVisible = true;
                            continue;
                        }
                        voxels[i, j, k].VisibleLeft  = i > 0 ? voxels[i - 1, j, k].IsEmpty : true;
                        voxels[i, j, k].VisibleRight = i < sizeX - 1 ? voxels[i + 1, j, k].IsEmpty : true;
                        voxels[i, j, k].VisibleFront = j > 0 ? voxels[i, j - 1, k].IsEmpty : true;
                        voxels[i, j, k].VisibleBack  = j < sizeY - 1 ? voxels[i, j + 1, k].IsEmpty : true;
                        voxels[i, j, k].VisibleDown  = k > 0 ? voxels[i, j, k - 1].IsEmpty : true;
                        voxels[i, j, k].VisibleUp    = k < sizeZ - 1 ? voxels[i, j, k + 1].IsEmpty : true;
                    }
                }
            }
            return(voxels);
        }
Пример #3
0
        public static VoxelData GetSplitedData(VoxelData source)
        {
            const int SPLIT_SIZE = 126;

            if (source.Voxels.Count == 0)
            {
                return(source);
            }

            var size  = source.GetModelSize(0);
            int sizeX = Mathf.RoundToInt(size.x);
            int sizeY = Mathf.RoundToInt(size.y);
            int sizeZ = Mathf.RoundToInt(size.z);

            if (sizeX <= SPLIT_SIZE && sizeY <= SPLIT_SIZE && sizeZ <= SPLIT_SIZE)
            {
                return(source);
            }

            int splitCountX = (sizeX / SPLIT_SIZE) + 1;
            int splitCountY = (sizeY / SPLIT_SIZE) + 1;
            int splitCountZ = (sizeZ / SPLIT_SIZE) + 1;

            var data = new VoxelData();

            // Nodes
            var childNodeId = new int[splitCountX * splitCountY * splitCountZ];

            for (int i = 0; i < childNodeId.Length; i++)
            {
                childNodeId[i] = i * 2 + 2;
            }

            data.Voxels     = new List <int[, , ]>();
            data.Transforms = new Dictionary <int, TransformData>()
            {
                { 0, new TransformData()
                  {
                      Name     = "",
                      ChildID  = 1,
                      Hidden   = false,
                      LayerID  = 0,
                      Reserved = 0,
                      Frames   = new TransformData.FrameData[1] {
                          new TransformData.FrameData()
                          {
                              Position = Vector3.zero,
                              Rotation = Vector3.zero,
                              Scale    = Vector3.one,
                          },
                      },
                  } },
            };
            data.Groups = new Dictionary <int, GroupData>()
            {
                { 1, new GroupData()
                  {
                      Attributes  = new Dictionary <string, string>(),
                      ChildNodeId = childNodeId,
                  } },
            };
            data.Shapes  = new Dictionary <int, ShapeData>();
            data.Palette = new List <Color>(source.Palette);
            data.Version = source.Version;

            int _i = 0;

            for (int x = 0; x < splitCountX; x++)
            {
                for (int y = 0; y < splitCountY; y++)
                {
                    for (int z = 0; z < splitCountZ; z++)
                    {
                        int splitSizeX = x < splitCountX - 1 ? SPLIT_SIZE : (sizeX - x * SPLIT_SIZE) % SPLIT_SIZE;
                        int splitSizeY = y < splitCountY - 1 ? SPLIT_SIZE : (sizeY - y * SPLIT_SIZE) % SPLIT_SIZE;
                        int splitSizeZ = z < splitCountZ - 1 ? SPLIT_SIZE : (sizeZ - z * SPLIT_SIZE) % SPLIT_SIZE;
                        int childID    = childNodeId[_i];
                        data.Transforms.Add(childID, new TransformData()
                        {
                            Name     = "Splited_Model_" + _i,
                            Reserved = 0,
                            LayerID  = 0,
                            Hidden   = false,
                            ChildID  = childID + 1,
                            Frames   = new TransformData.FrameData[1] {
                                new TransformData.FrameData()
                                {
                                    Position = new Vector3(
                                        x * SPLIT_SIZE + splitSizeX / 2,
                                        y * SPLIT_SIZE + splitSizeY / 2,
                                        z * SPLIT_SIZE + splitSizeZ / 2
                                        ),
                                    Rotation = Vector3.zero,
                                    Scale    = Vector3.one,
                                },
                            },
                        });
                        data.Shapes.Add(childID + 1, new ShapeData()
                        {
                            Attributes = new Dictionary <string, string>(),
                            ModelData  = new KeyValuePair <int, Dictionary <string, string> >[] {
                                new KeyValuePair <int, Dictionary <string, string> >(_i, new Dictionary <string, string>()),
                            },
                        });
                        _i++;
                    }
                }
            }


            // Split
            var sourceVoxels = source.Voxels[0];

            for (int x = 0; x < splitCountX; x++)
            {
                for (int y = 0; y < splitCountY; y++)
                {
                    for (int z = 0; z < splitCountZ; z++)
                    {
                        int splitSizeX = x < splitCountX - 1 ? SPLIT_SIZE : (sizeX - x * SPLIT_SIZE) % SPLIT_SIZE;
                        int splitSizeY = y < splitCountY - 1 ? SPLIT_SIZE : (sizeY - y * SPLIT_SIZE) % SPLIT_SIZE;
                        int splitSizeZ = z < splitCountZ - 1 ? SPLIT_SIZE : (sizeZ - z * SPLIT_SIZE) % SPLIT_SIZE;
                        var voxels     = new int[splitSizeX, splitSizeY, splitSizeZ];
                        for (int i = 0; i < splitSizeX; i++)
                        {
                            for (int j = 0; j < splitSizeY; j++)
                            {
                                for (int k = 0; k < splitSizeZ; k++)
                                {
                                    voxels[i, j, k] = sourceVoxels[
                                        x * SPLIT_SIZE + i,
                                        y *SPLIT_SIZE + j,
                                        z *SPLIT_SIZE + k
                                                      ];
                                }
                            }
                        }
                        data.Voxels.Add(voxels);
                    }
                }
            }

            return(data);
        }
Пример #4
0
        public static Result CreateModel(VoxelData data, float scale, LightMapSupportType supportLightMap, bool supportRig, Vector3 pivot)
        {
            if (data == null)
            {
                return(null);
            }

            Data             = data;
            ModelScale       = scale;
            _SupportLightMap = supportLightMap;
            SupportRig       = supportRig;
            Pivot            = pivot;


            var result = new Result {
                VoxelModels = new Result.VoxelModel[1] {
                    new Result.VoxelModel()
                    {
                        Meshs          = new UnlimitiedMesh[data.Voxels.Count],
                        Textures       = new Texture2D[data.Voxels.Count],
                        ModelSize      = new Vector3[data.Voxels.Count],
                        FootPoints     = new Vector3[data.Voxels.Count],
                        MaxModelBounds = new Int3(data.GetBounds()).Max,
                    }
                }
            };

            for (int index = 0; index < data.Voxels.Count; index++)
            {
                UnlimitiedMesh mesh;
                Texture2D      texture;
                PointWeights = null;
                RootBones    = null;
                Voxels       = data.Voxels[index];
                SizeX        = Voxels.GetLength(0);
                SizeY        = Voxels.GetLength(1);
                SizeZ        = Voxels.GetLength(2);
                ModelIndex   = index;

                GetFaces();
                GetColors();
                if (SupportRig)
                {
                    var rootBoneList = GetChildBones(null);
                    if (rootBoneList != null)
                    {
                        RootBones = rootBoneList.ToArray();
                    }
                    GetWeights();
                }
                GetAreas();
                GetResultFromArea(out mesh, out texture);

                result.VoxelModels[0].Meshs[index]      = mesh;
                result.VoxelModels[0].Textures[index]   = texture;
                result.VoxelModels[0].ModelSize[index]  = data.GetModelSize(index);
                result.VoxelModels[0].FootPoints[index] = data.GetFootPoint(index);
                result.VoxelModels[0].RootBones         = RootBones;
            }

            result.VoxelModels[0].RootNode = GetTransformData(result.VoxelModels[0], result.VoxelModels[0].Textures, 0);

            Data         = null;
            Voxels       = null;
            Faces        = null;
            PointWeights = null;
            RootBones    = null;
            Colors       = null;
            AreaList.Clear();
            PackingList.Clear();
            AreaPackingMap.Clear();

            return(result);
        }