public static void CreateVoxels(MVVoxel[] voxels, MVVoxModel voxModel)
    {
        MVVoxelChunk chunk = voxModel.vox.voxelChunk;

        float sizePerVox = voxModel.sizePerVox;

        float cx = sizePerVox * chunk.sizeX / 2;
        float cy = sizePerVox * chunk.sizeY / 2;
        float cz = sizePerVox * chunk.sizeZ / 2;

        Material mat = (voxModel.voxMaterial != null) ? voxModel.voxMaterial : MVImporter.DefaultMaterial;

        List <GameObject> objects = new List <GameObject> ();

        foreach (MVVoxel voxel in voxels)
        {
            float px = voxel.x * sizePerVox - cx, py = voxel.y * sizePerVox - cy, pz = voxel.z * sizePerVox - cz;

            GameObject go = MVImporter.CreateGameObject(voxModel.gameObject.gameObject.transform,
                                                        new Vector3(px, py, pz),
                                                        string.Format("Voxel ({0}, {1}, {2})", voxel.x, voxel.y, voxel.z),
                                                        MVImporter.CubeMeshWithColor(sizePerVox, voxModel.vox.palatte [chunk.voxels [voxel.x, voxel.y, voxel.z] - 1]),
                                                        mat);

            MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> ();
            v.voxel = new MVVoxel()
            {
                x = voxel.x, y = voxel.y, z = voxel.z, colorIndex = chunk.voxels [voxel.x, voxel.y, voxel.z]
            };

            objects.Add(go);
        }

        Selection.objects = objects.ToArray();
    }
示例#2
0
    public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, MVVoxelChunk alphaMask, Vector3 origin)
    {
        List <GameObject> result = new List <GameObject> ();

        if (alphaMask != null && (alphaMask.sizeX != chunk.sizeX || alphaMask.sizeY != chunk.sizeY || alphaMask.sizeZ != chunk.sizeZ))
        {
            Debug.LogErrorFormat("Unable to create meshes from chunk : Chunk's size ({0},{1},{2}) differs from alphaMask chunk's size ({3},{4},{5})", chunk.sizeX, chunk.sizeY, chunk.sizeZ, alphaMask.sizeX, alphaMask.sizeY, alphaMask.sizeZ);
            return(result.ToArray());
        }

        for (int x = 0; x < chunk.sizeX; ++x)
        {
            for (int y = 0; y < chunk.sizeY; ++y)
            {
                for (int z = 0; z < chunk.sizeZ; ++z)
                {
                    if (chunk.voxels [x, y, z] != 0)
                    {
                        float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox;
                        Color c = palatte [chunk.voxels [x, y, z] - 1];

                        if (alphaMask != null && alphaMask.voxels[x, y, z] != 0)
                        {
                            c.a = (float)(alphaMask.voxels [x, y, z] - 1) / 255;
                        }

                        GameObject go = CreateGameObject(
                            parent,
                            new Vector3(px, py, pz),
                            string.Format("Voxel ({0}, {1}, {2})", x, y, z),
                            MVImporter.CubeMeshWithColor(sizePerVox, c),
                            mat);

                        MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> ();
                        v.voxel = new MVVoxel()
                        {
                            x = (byte)x, y = (byte)y, z = (byte)z, colorIndex = chunk.voxels [x, y, z]
                        };

                        result.Add(go);
                    }
                }
            }
        }

        return(result.ToArray());
    }
示例#3
0
    public static GameObject[] CreateIndividualVoxelGameObjectsForChunk(MVVoxelChunk chunk, Color[] palatte, Transform parent, Material mat, float sizePerVox, Vector3 origin)
    {
        List <GameObject> result = new List <GameObject> ();

        for (int x = 0; x < chunk.sizeX; ++x)
        {
            for (int y = 0; y < chunk.sizeY; ++y)
            {
                for (int z = 0; z < chunk.sizeZ; ++z)
                {
                    if (chunk.voxels [x, y, z] != 0)
                    {
                        float px = (x - origin.x + 0.5f) * sizePerVox, py = (y - origin.y + 0.5f) * sizePerVox, pz = (z - origin.z + 0.5f) * sizePerVox;
                        int   cidx = chunk.voxels[x, y, z];

                        GameObject go = CreateGameObject(
                            parent,
                            new Vector3(px, py, pz),
                            string.Format("Voxel ({0}, {1}, {2})", x, y, z),
                            MVImporter.CubeMeshWithColor(sizePerVox, palatte[cidx - 1], cidx),
                            mat);

#if UNITY_EDITOR
                        MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> ();
                        v.voxel = new MVVoxel()
                        {
                            x = (byte)x, y = (byte)y, z = (byte)z, colorIndex = chunk.voxels [x, y, z]
                        };
#endif

                        result.Add(go);
                    }
                }
            }
        }

        return(result.ToArray());
    }