Exemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        //读入文件数据
        Stream sw = new MemoryStream(_file.bytes);

        System.IO.BinaryReader br = new System.IO.BinaryReader(sw);

        //从文件中解析MagicaVoxel文件,并转化成通用格式
        MagicaVoxel magica = MagicaVoxelFormater.ReadFromBinary(br);
        VoxelStruct vs     = magica.vs;


        //拿出体素数据
        VoxelData[] datas = vs.datas.ToArray();


        //创建体素“产品”
        VoxelProduct product = new VoxelProduct();

        //将数据写入产品
        (new VoxelData2Point(datas)).build(product);
        //切成8x8x8立方体,为了等下减面的时间可控,如果不切割的话,对于大模型减面时间太长
        (new VoxelSplitSmall(new VectorInt3(8, 8, 8))).build(product);
        //构造模型
        (new VoxelMeshBuild()).build(product);
        //减去重复的顶点
        (new VoxelRemoveSameVertices()).build(product);

        //减去重复的面
        (new VoxelRemoveFace()).build(product);
        //减去重复的顶点(在减面过程中出现的重复顶点)
        (new VoxelRemoveSameVertices()).build(product);

        //得到模型数据
        VoxelMeshData data = product.getMeshData();

        //以上是通过体素生成模型数据的代码,如果有别的算法生成,可以直接走下面代码

        //通过模型数据生成Mesh 和 MeshFilter。
        Mesh       mesh   = VoxelBuilder.Data2Mesh(data);
        MeshFilter filter = VoxelBuilder.Mesh2Filter(mesh);

        //别忘了加上材质。
        VoxelBuilder.FilterAddRenderer(filter, _material);


        //设置模型的坐标和名称等。
        filter.transform.SetParent(this.transform);
        filter.transform.localEulerAngles = Vector3.zero;
        filter.transform.localPosition    = data.offset;
        filter.name = "Voxel";
    }
Exemplo n.º 2
0
    public void VoxelFormaterTest()
    {
        //Arrange
        // var gameObject = new GameObject();

        //Act
        //Try to rename the GameObject
        // var newGameObjectName = "My game object";
        //gameObject.name = newGameObjectName;

        //VoxelWriter writer = gameObject.GetComponent<VoxelWriter> ();
        //VoxelFormater formater = gameObject.GetComponent<VoxelFormater> ();

        FileStream sr = new FileStream(".//Assets//Voxel//chr_cop2.bytes", FileMode.OpenOrCreate, FileAccess.Read);


        System.IO.BinaryReader br = new System.IO.BinaryReader(sr);


        MagicaVoxel magic = MagicaVoxelFormater.ReadFromBinary(br);


        FileStream sw = new FileStream("fly2.vox", FileMode.Create, FileAccess.Write);

        System.IO.BinaryWriter bw = new System.IO.BinaryWriter(sw);
        MagicaVoxelFormater.WriteToBinary(magic.structure, bw);


        sw.Close();
        sr.Close();

        FileStream sr2 = new FileStream("fly2.vox", FileMode.OpenOrCreate, FileAccess.Read);


        System.IO.BinaryReader br2 = new System.IO.BinaryReader(sr2);

        MagicaVoxel magic2 = MagicaVoxelFormater.ReadFromBinary(br2);

        Assert.AreEqual(magic.main.name, magic2.main.name);
        Assert.AreEqual(magic.main.size, magic2.main.size);
        Assert.AreEqual(magic.main.chunks, magic2.main.chunks);


        Assert.AreEqual(magic.size.box, magic2.size.box);
        Assert.AreEqual(magic.size.name, magic2.size.name);
        Assert.AreEqual(magic.size.size, magic2.size.size);
        Assert.AreEqual(magic.size.chunks, magic2.size.chunks);


        Assert.AreEqual(magic.rgba.palette.Length, magic2.rgba.palette.Length);
        for (int i = 0; i < magic.rgba.palette.Length; ++i)
        {
            Assert.AreEqual(magic.rgba.palette[i], magic2.rgba.palette[i]);
        }

//		Debug.Log (vs2.rgba.palette.Length);
        Assert.AreEqual(magic.rgba.name, magic2.rgba.name);
        Assert.AreEqual(magic.rgba.size, magic2.rgba.size);
        Assert.AreEqual(magic.rgba.chunks, magic2.rgba.chunks);

        sr2.Close();

        for (int i = 0; i < MagicaVoxelFormater.palette_.Length; ++i)
        {
            ushort s  = MagicaVoxelFormater.palette_ [i];
            Color  c  = MagicaVoxelFormater.Short2Color(s);
            ushort s2 = MagicaVoxelFormater.Color2Short(c);
            Color  c2 = MagicaVoxelFormater.Short2Color(s2);
            Assert.AreEqual(s, s2);
            Assert.AreEqual(c, c2);
        }


        //Debug.Log ();
        Assert.AreEqual(magic.structure.count, magic2.structure.count);

//		Debug.Log (vs2.datas.Length);
        for (int i = 0; i < magic.structure.count; ++i)
        {
            Assert.AreEqual(magic.structure.getData(i).color, magic2.structure.getData(i).color);
            Assert.AreEqual(magic.structure.getData(i).position.x, magic2.structure.getData(i).position.x);
            Assert.AreEqual(magic.structure.getData(i).position.y, magic2.structure.getData(i).position.y);
            Assert.AreEqual(magic.structure.getData(i).position.z, magic2.structure.getData(i).position.z);
        }


        Assert.AreEqual(magic.structure.count, magic.structure.count);
    }
Exemplo n.º 3
0
    public void SplitVoxelTest()
    {
        Vector3Int a = new Vector3Int(3, 4, 5);
        Vector3Int b = new Vector3Int(1, 2, 3);

        Assert.AreEqual(a - b, new Vector3Int(2, 2, 2));

        FileStream sr2 = new FileStream(".//Assets//Voxel//grass.bytes", FileMode.OpenOrCreate, FileAccess.Read);

        System.IO.BinaryReader br2 = new System.IO.BinaryReader(sr2);
        VoxelStruct            vs  = MagicaVoxelFormater.ReadFromBinary(br2).structure;


        SplitVoxel split = new SplitVoxel(vs);

        split.addBox(new Vector3Int(0, 0, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(0, 17, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(0, 34, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(0, 54, 0), new Vector3Int(16, 16, 3));


        split.addBox(new Vector3Int(20, 0, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(20, 17, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(20, 34, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(20, 54, 0), new Vector3Int(16, 16, 3));


        split.addBox(new Vector3Int(37, 0, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(37, 17, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(37, 34, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(37, 54, 0), new Vector3Int(16, 16, 3));


        split.addBox(new Vector3Int(54, 0, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(54, 17, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(54, 34, 0), new Vector3Int(16, 16, 3));
        split.addBox(new Vector3Int(54, 54, 0), new Vector3Int(16, 16, 3));
        /**/

        VoxelStruct[] voxels = split.doIt();
        for (int i = 0; i < voxels.Length; ++i)
        {
            FileStream sw = new FileStream("cool" + i + ".vox", FileMode.Create, FileAccess.Write);

            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(sw);
            //voxels [i].normal ;
            MagicaVoxelFormater.WriteToBinary(voxels[i], bw);
            sw.Close();
        }
        //VoxelStruct vs2 = splice.spliceAll ();
    }

    [Test]
    public void JoinVoxelTest()
    {
        FileStream sr2 = new FileStream(".//Assets//Voxel//temp.vox", FileMode.OpenOrCreate, FileAccess.Read);

        System.IO.BinaryReader br2 = new System.IO.BinaryReader(sr2);
        VoxelStruct            vs  = MagicaVoxelFormater.ReadFromBinary(br2).structure;

        Debug.Log(MagicaVoxelFormater.GetMd5(vs));
    }

    [Test]
    public void VoxelMapMakerTest()     /*
                                         * VoxelMapMaker vmm = Component.FindObjectOfType<VoxelMapMaker> ();
                                         * VoxelStruct map = vmm.building ();
                                         * FileStream sw = new FileStream ("map.vox", FileMode.Create, FileAccess.Write);
                                         * System.IO.BinaryWriter bw = new System.IO.BinaryWriter (sw);
                                         * VoxelFormater.WriteToMagicaVoxel (map, bw);
                                         * sw.Close ();
                                         *
                                         * Debug.Log (vmm);*/
    {
    }
Exemplo n.º 4
0
    public void SplitVoxelTest()
    {
        VectorInt3 a = new VectorInt3(3, 4, 5);
        VectorInt3 b = new VectorInt3(1, 2, 3);

        Assert.AreEqual(a - b, new VectorInt3(2, 2, 2));

        FileStream sr2 = new FileStream(".//Assets//Voxel//grass.bytes", FileMode.OpenOrCreate, FileAccess.Read);

        System.IO.BinaryReader br2 = new System.IO.BinaryReader(sr2);
        VoxelStruct            vs  = MagicaVoxelFormater.ReadFromBinary(br2).vs;


        SplitVoxel split = new SplitVoxel(vs);

        split.addBox(new VectorInt3(0, 0, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(0, 17, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(0, 34, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(0, 54, 0), new VectorInt3(16, 16, 3));


        split.addBox(new VectorInt3(20, 0, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(20, 17, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(20, 34, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(20, 54, 0), new VectorInt3(16, 16, 3));


        split.addBox(new VectorInt3(37, 0, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(37, 17, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(37, 34, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(37, 54, 0), new VectorInt3(16, 16, 3));


        split.addBox(new VectorInt3(54, 0, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(54, 17, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(54, 34, 0), new VectorInt3(16, 16, 3));
        split.addBox(new VectorInt3(54, 54, 0), new VectorInt3(16, 16, 3));
        /**/

        VoxelStruct[] voxels = split.doIt();
        for (int i = 0; i < voxels.Length; ++i)
        {
            FileStream sw = new FileStream("cool" + i + ".vox", FileMode.Create, FileAccess.Write);

            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(sw);
            //voxels [i].normal ;
            MagicaVoxelFormater.WriteToBinary(voxels[i], bw);
            sw.Close();
        }
        //VoxelStruct vs2 = splice.spliceAll ();
    }

    [Test]
    public void JoinVoxelTest()
    {
        FileStream sr2 = new FileStream(".//Assets//Voxel//temp.vox", FileMode.OpenOrCreate, FileAccess.Read);


        System.IO.BinaryReader br2 = new System.IO.BinaryReader(sr2);

        VoxelStruct vs = MagicaVoxelFormater.ReadFromBinary(br2).vs;

        //vs.arrange ();
        Debug.Log(MagicaVoxelFormater.GetMd5(vs));

        return;

        /*
         *      FileStream sr2 = new FileStream ("fly2.vox", FileMode.OpenOrCreate, FileAccess.Read);
         *
         *
         *      System.IO.BinaryReader br2 = new System.IO.BinaryReader (sr2);
         *
         *      VoxelStruct vs = VoxelFormater.ReadFromMagicaVoxel (br2);
         *
         *      sr2.Close ();
         *      JoinVoxel join = new JoinVoxel ();
         *      join.addVoxel(vs, new VectorInt3(0, 0, 0));
         *      join.addVoxel(vs, new VectorInt3(10, 10, 10));
         *      VoxelStruct vs2 = join.doIt ();
         *
         *      FileStream sw = new FileStream ("fly3.vox", FileMode.Create, FileAccess.Write);
         *
         *      System.IO.BinaryWriter bw = new System.IO.BinaryWriter (sw);
         *      VoxelFormater.WriteToMagicaVoxel (vs2, bw);
         *      sw.Close ();
         *      /*
         *
         *      Assert.AreEqual(vs.main.name, vs2.main.name);
         *      Assert.AreEqual(vs.main.size, vs2.main.size);
         *      Assert.AreEqual(vs.main.chunks, vs2.main.chunks);
         *
         *
         *      Assert.AreEqual(vs.size.box, vs2.size.box);
         *      Assert.AreEqual(vs.size.name, vs2.size.name);
         *      Assert.AreEqual(vs.size.size, vs2.size.size);
         *      Assert.AreEqual(vs.size.chunks, vs2.size.chunks);
         */

        //Assert.AreEqual(vs.rgba.palette.Length, vs2.rgba.palette.Length);

        /*for (int i = 0; i < vs.rgba.palette.Length; ++i) {
         *      Assert.AreEqual(vs.rgba.palette[i], vs2.rgba.palette[i]);
         * }*/
        /*//		Debug.Log (vs2.rgba.palette.Length);
         * Assert.AreEqual(vs.rgba.name, vs2.rgba.name);
         * Assert.AreEqual(vs.rgba.size, vs2.rgba.size);
         * Assert.AreEqual(vs.rgba.chunks, vs2.rgba.chunks);
         */
    }