Exemplo n.º 1
0
    public string[] GetObjectNames(string path)
    {
        Chunk mainChunk = MagicaVoxelReader.ReadMagicaChunks(path);

        HashSet <string> names = new HashSet <string>();

        for (int i = 0; i < mainChunk.children.Count; i++)
        {
            Chunk chunk = mainChunk.children[i];
            if (chunk is TransformNodeChunk)
            {
                TransformNodeChunk transformNodeChunk = (TransformNodeChunk)chunk;
                if (transformNodeChunk.attributes.Keys.Contains("_name"))
                {
                    names.Add(transformNodeChunk.attributes["_name"]);
                }
            }
        }

        return(names.ToArray <string>());
    }
Exemplo n.º 2
0
    public GameObject ImportMagicaVoxelFileAssets(string path)
    {
        GameObject levelGo = new GameObject(Path.GetFileName(path));

        string fileName = Path.GetFileName(path);

        List <GameObject> gameObjects = new List <GameObject>();
        Dictionary <string, GameObject> namedGameObjects = new Dictionary <string, GameObject>();

        Chunk           mainChunk      = MagicaVoxelReader.ReadMagicaChunks(path);
        List <Material> colorMaterials = ImportColors(mainChunk);
        List <string>   names          = new List <string>();
        List <string>   doubleNames    = new List <string>();

        string localPath      = AssetDatabase.GenerateUniqueAssetPath(Path.Combine("Assets", "ImportedPrefabs", fileName));
        string materialFolder = Path.Combine("Assets", "ImportedPrefabs", fileName, "Materials");
        string meshFolder     = Path.Combine("Assets", "ImportedPrefabs", fileName, "Meshes");

        if (!AssetDatabase.IsValidFolder(localPath))
        {
            AssetDatabase.CreateFolder(Path.Combine("Assets", "ImportedPrefabs"), fileName);
        }
        if (!AssetDatabase.IsValidFolder(materialFolder))
        {
            AssetDatabase.CreateFolder(localPath, "Materials");
        }
        if (!AssetDatabase.IsValidFolder(meshFolder))
        {
            AssetDatabase.CreateFolder(localPath, "Meshes");
        }

        for (int m = 0; m < colorMaterials.Count; m++)
        {
            AssetDatabase.CreateAsset(colorMaterials[m], Path.Combine(materialFolder, "Color_" + m + ".mat"));
        }

        for (int i = 0; i < mainChunk.children.Count; i++)
        {
            Chunk chunk = mainChunk.children[i];
            if (chunk is VoxelModelChunk)
            {
                VoxelModelChunk voxelChunk = (VoxelModelChunk)chunk;

                foreach (ShapeNodeChunk shape in voxelChunk.shapes)
                {
                    TransformNodeChunk transformNodeChunk = shape.transform;

                    bool isPotentialAsset = false;
                    while (transformNodeChunk != null)
                    {
                        if (transformNodeChunk.attributes.Count > 0 && transformNodeChunk.attributes.ContainsKey("_name"))
                        {
                            isPotentialAsset = true;
                            break;
                        }
                    }
                    if (!isPotentialAsset)
                    {
                        continue;
                    }

                    MeshAndColors   meshAndColors = createMesh(voxelChunk, shape);
                    List <Material> materials     = new List <Material>();
                    foreach (int color in meshAndColors.colors)
                    {
                        materials.Add(colorMaterials[color - 1]);
                    }

                    GameObject go = new GameObject();
                    gameObjects.Add(go);
                    ObjectAttributes script = go.AddComponent <ObjectAttributes>();

                    MeshRenderer renderer = go.AddComponent <MeshRenderer>();
                    renderer.materials = materials.ToArray();
                    MeshFilter filter = go.AddComponent <MeshFilter>();
                    filter.mesh = meshAndColors.mesh;

                    Vector3 shift = Vector3.zero; //new Vector3(script.singleCenter.x, 0, script.singleCenter.y);
                    while (transformNodeChunk != null)
                    {
                        if (transformNodeChunk.attributes.Count > 0 && transformNodeChunk.attributes.ContainsKey("_name"))
                        {
                            string name = transformNodeChunk.attributes["_name"];
                            script.names.Add(name);

                            if (names.Contains(name))
                            {
                                if (!doubleNames.Contains(name))
                                {
                                    doubleNames.Add(name);
                                }
                            }
                            else
                            {
                                names.Add(name);
                                namedGameObjects.Add(name, go);
                            }
                        }

                        if (transformNodeChunk.frameAttributes[0].ContainsKey("_r"))
                        {
                            script.rotations.Add(transformNodeChunk.frameAttributes[0]["_r"]);
                        }

                        Vector3[] rotationMatrix = getRotationMatrix(transformNodeChunk);
                        script.rotationMatrices.Add(rotationMatrix);

                        if (transformNodeChunk.frameAttributes[0].ContainsKey("_t"))
                        {
                            string[] coords       = transformNodeChunk.frameAttributes[0]["_t"].Split(' ');
                            Vector3  currentShift = new Vector3(float.Parse(coords[0]) / 10f, float.Parse(coords[2]) / 10f, float.Parse(coords[1]) / 10f);;
                            script.magicaTransitions.Add(currentShift);
                            shift += currentShift;
                        }
                        if (transformNodeChunk.group != null && transformNodeChunk.group.transform != null)
                        {
                            transformNodeChunk = transformNodeChunk.group.transform;
                        }
                        else
                        {
                            transformNodeChunk = null;
                        }
                    }

                    Vector3 ve = rotateVector(new Vector3(voxelChunk.sizeChunk.sizeX, voxelChunk.sizeChunk.sizeY, voxelChunk.sizeChunk.sizeZ), shape.transform);

                    script.magicaTotalSize = new Vector3(Math.Abs(ve.x), Math.Abs(ve.z), Math.Abs(ve.y));

                    script.bottomCenterOfVoxelMass = shape.singleCenter / 10f;

                    script.centerOfMagicaMass = new Vector3((float)Math.Floor((double)(Math.Abs(ve.x) / 2f)) / 10f, (float)Math.Floor((double)(Math.Abs(ve.z) / 2f)) / 10f, (float)Math.Floor((double)(Math.Abs(ve.y) / 2f)) / 10f);
                    script.parentVoxFile      = Path.GetFileName(path);
                    //shift -= script.trans;
                    shift += new Vector3(script.bottomCenterOfVoxelMass.x - script.centerOfMagicaMass.x, script.bottomCenterOfVoxelMass.y - script.centerOfMagicaMass.y, script.bottomCenterOfVoxelMass.z - script.centerOfMagicaMass.z);
                    //shift += (script.bottomCenterOfVoxelMass - script.centerOfMagicaMass);

                    go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    go.transform.position   = shift;
                    go.name             = script.names[0];
                    go.transform.parent = levelGo.transform;
                }
            }
        }

        // remove double named objects since we cannot insert them properly
        foreach (string name in doubleNames)
        {
            namedGameObjects.Remove(name);
        }

        foreach (GameObject g in namedGameObjects.Values)
        {
            TeardownProperties teardownProperties = g.AddComponent <TeardownProperties>();

            // Create the new Prefab.
            AssetDatabase.CreateAsset(g.GetComponent <MeshFilter>().sharedMesh, Path.Combine(meshFolder, g.name.Replace(" ", "_") + ".mesh"));
            AssetDatabase.SaveAssets();
            PrefabUtility.SaveAsPrefabAssetAndConnect(g, Path.Combine("Assets", "ImportedPrefabs", fileName, g.name.Replace(" ", "_") + ".prefab"), InteractionMode.UserAction);
        }

        PrefabUtility.SaveAsPrefabAssetAndConnect(levelGo, Path.Combine("Assets", "ImportedPrefabs", fileName, fileName + ".prefab"), InteractionMode.UserAction);

        return(levelGo);
    }
Exemplo n.º 3
0
    public GameObject ImportMagicaVoxelFile(string path)
    {
        Material vertexMaterial = Resources.Load("VertexShading", typeof(Material)) as Material;

        GameObject levelGo = new GameObject(Path.GetFileName(path));

        levelGo.AddComponent <TeardownProperties>();
        MagicaImportedFile magicaImportedFile = levelGo.AddComponent <MagicaImportedFile>();

        magicaImportedFile.voxFile = Path.GetFileName(path);

        List <GameObject> gameObjects = new List <GameObject>();
        Dictionary <string, GameObject> namedGameObjects = new Dictionary <string, GameObject>();

        Chunk           mainChunk      = MagicaVoxelReader.ReadMagicaChunks(path);
        List <Material> colorMaterials = ImportColors(mainChunk);
        List <string>   names          = new List <string>();
        List <string>   doubleNames    = new List <string>();

        for (int i = 0; i < mainChunk.children.Count; i++)
        {
            Chunk chunk = mainChunk.children[i];
            if (chunk is VoxelModelChunk)
            {
                VoxelModelChunk voxelChunk = (VoxelModelChunk)chunk;

                foreach (ShapeNodeChunk shape in voxelChunk.shapes)
                {
                    MeshAndColors meshAndColors = createMesh(voxelChunk, shape);
                    Color[]       colors        = new Color[meshAndColors.colors.Count];
                    for (int c = 0; c < meshAndColors.colors.Count; c++)
                    {
                        colors[c] = colorMaterials[meshAndColors.colors[c] - 1].color;
                    }
                    meshAndColors.mesh.colors = colors;

                    TransformNodeChunk transformNodeChunk = shape.transform;
                    GameObject         go = new GameObject();
                    gameObjects.Add(go);
                    ObjectAttributes script   = go.AddComponent <ObjectAttributes>();
                    MeshRenderer     renderer = go.AddComponent <MeshRenderer>();
                    renderer.material = vertexMaterial;
                    MeshFilter filter = go.AddComponent <MeshFilter>();
                    filter.mesh = meshAndColors.mesh;

                    Vector3 shift = Vector3.zero; //new Vector3(script.singleCenter.x, 0, script.singleCenter.y);
                    while (transformNodeChunk != null)
                    {
                        if (transformNodeChunk.attributes.Count > 0 && transformNodeChunk.attributes.ContainsKey("_name"))
                        {
                            string name = transformNodeChunk.attributes["_name"];
                            script.names.Add(name);

                            if (names.Contains(name))
                            {
                                if (!doubleNames.Contains(name))
                                {
                                    doubleNames.Add(name);
                                }
                            }
                            else
                            {
                                names.Add(name);
                                namedGameObjects.Add(name, go);
                            }
                        }

                        if (transformNodeChunk.frameAttributes[0].ContainsKey("_r"))
                        {
                            script.rotations.Add(transformNodeChunk.frameAttributes[0]["_r"]);
                        }

                        Vector3[] rotationMatrix = getRotationMatrix(transformNodeChunk);
                        script.rotationMatrices.Add(rotationMatrix);

                        if (transformNodeChunk.frameAttributes[0].ContainsKey("_t"))
                        {
                            string[] coords       = transformNodeChunk.frameAttributes[0]["_t"].Split(' ');
                            Vector3  currentShift = new Vector3(float.Parse(coords[0]) / 10f, float.Parse(coords[2]) / 10f, float.Parse(coords[1]) / 10f);;
                            script.magicaTransitions.Add(currentShift);
                            shift += currentShift;
                        }
                        if (transformNodeChunk.group != null && transformNodeChunk.group.transform != null)
                        {
                            transformNodeChunk = transformNodeChunk.group.transform;
                        }
                        else
                        {
                            transformNodeChunk = null;
                        }
                    }

                    Vector3 ve = rotateVector(new Vector3(voxelChunk.sizeChunk.sizeX, voxelChunk.sizeChunk.sizeY, voxelChunk.sizeChunk.sizeZ), shape.transform);

                    script.magicaTotalSize = new Vector3(Math.Abs(ve.x), Math.Abs(ve.z), Math.Abs(ve.y));

                    script.bottomCenterOfVoxelMass = shape.singleCenter / 10f;

                    script.centerOfMagicaMass = new Vector3((float)Math.Floor((double)(Math.Abs(ve.x) / 2f)) / 10f, (float)Math.Floor((double)(Math.Abs(ve.z) / 2f)) / 10f, (float)Math.Floor((double)(Math.Abs(ve.y) / 2f)) / 10f);
                    script.parentVoxFile      = Path.GetFileName(path);
                    //shift -= script.trans;
                    shift += new Vector3(script.bottomCenterOfVoxelMass.x - script.centerOfMagicaMass.x, script.bottomCenterOfVoxelMass.y - script.centerOfMagicaMass.y, script.bottomCenterOfVoxelMass.z - script.centerOfMagicaMass.z);
                    //shift += (script.bottomCenterOfVoxelMass - script.centerOfMagicaMass);

                    go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    go.transform.position   = shift;

                    go.transform.parent = levelGo.transform;

                    if (script.names.Count > 0)
                    {
                        go.name = script.names[0];
                    }
                }
            }
        }

        // remove double named objects since we cannot insert them properly
        foreach (string name in doubleNames)
        {
            namedGameObjects.Remove(name);
        }

        foreach (GameObject g in gameObjects)
        {
            if (namedGameObjects.Values.Contains(g))
            {
                TeardownProperties teardownProperties = g.AddComponent <TeardownProperties>();
            }
        }

        return(levelGo);
    }