private static void DoTask(Task task)
        {
            if (TaskMap.Count == 0)
            {
                return;
            }

            if (TheExportMod == ExportMod.AskEverytime && !BrowseExportPath())
            {
                return;
            }

            RefreshMergeSetting();

            string failMessage  = "[Voxel] Failed to create model for {0} model{1}.";
            int    successedNum = 0;
            int    failedNum    = 0;
            int    taskCount    = TaskMap.Count;
            var    resultList   = new List <Core_Voxel.Result>();

            Util.ProgressBar("Creating", "Starting task...", 0f);
            Util.StartWatch();
            ForAllSelection((pathData) => {
                try {
                    string fileName = Util.GetNameWithoutExtension(pathData.Path);

                    Util.ProgressBar("Creating", string.Format("[{1}/{2}] Creating {0}", fileName, successedNum + failedNum + 1, taskCount), (float)(successedNum + failedNum + 1) / (taskCount + 1));

                    VoxelData voxelData = null;
                    switch (task)
                    {
                    case Task.Prefab:
                    case Task.Lod:
                    case Task.Obj:
                        // Model
                        voxelData = VoxelFile.GetVoxelData(Util.FileToByte(pathData.Path), pathData.Extension == ".vox");
                        if (pathData.Extension == ".vox" || pathData.Extension == ".qb")
                        {
                            var result = Core_Voxel.CreateLodModel(
                                voxelData,
                                ModelScale,
                                task == Task.Lod ? LodNum : 1,
                                LightMapSupportMode,
                                ModelPivot
                                );
                            if (TheExportMod == ExportMod.OriginalPath)
                            {
                                result.ExportRoot    = Util.GetParentPath(pathData.Path);
                                result.ExportSubRoot = "";
                            }
                            else
                            {
                                result.ExportRoot    = ExportPath;
                                result.ExportSubRoot = pathData.Root;
                            }
                            result.FileName   = fileName;
                            result.Extension  = task == Task.Obj ? ".obj" : ".prefab";
                            result.IsRigged   = false;
                            result.WithAvatar = false;
                            resultList.Add(result);
                        }
                        break;

                    case Task.ToJson:
                        if (pathData.Extension == ".vox" || pathData.Extension == ".qb")
                        {
                            // Voxel To Json
                            voxelData   = VoxelFile.GetVoxelData(Util.FileToByte(pathData.Path), pathData.Extension == ".vox");
                            var json    = Core_Voxel.VoxelToJson(voxelData);
                            string path = TheExportMod == ExportMod.OriginalPath ?
                                          Util.ChangeExtension(pathData.Path, ".json") :
                                          Util.CombinePaths(ExportPath, pathData.Root, fileName + ".json");
                            Util.CreateFolder(Util.GetParentPath(path));
                            Util.Write(json, path);
                        }
                        break;

                    case Task.ToVox:
                    case Task.ToQb:
                        // Json To Voxel
                        string aimEx = task == Task.ToVox ? ".vox" : ".qb";
                        if (pathData.Extension == ".json")
                        {
                            voxelData = Core_Voxel.JsonToVoxel(Util.Read(pathData.Path));
                        }
                        else if (pathData.Extension == ".vox" || pathData.Extension == ".qb")
                        {
                            if (aimEx != pathData.Extension)
                            {
                                voxelData = VoxelFile.GetVoxelData(Util.FileToByte(pathData.Path), pathData.Extension == ".vox");
                            }
                        }
                        if (voxelData)
                        {
                            string aimPath = TheExportMod == ExportMod.OriginalPath ?
                                             Util.ChangeExtension(pathData.Path, aimEx) :
                                             Util.CombinePaths(ExportPath, pathData.Root, fileName + aimEx);
                            Util.ByteToFile(
                                VoxelFile.GetVoxelByte(voxelData, task == Task.ToVox),
                                aimPath
                                );
                        }
                        break;
                    }
                    successedNum++;
                } catch (System.Exception ex) {
                    failMessage += "\n" + ex.Message;
                    failedNum++;
                }
            });

            // File
            try {
                Core_File.CreateFileForResult(resultList, TheShader, ModelScale, ModelPivot);

                double taskTime = Util.StopWatchAndGetTime();

                // Log Messages
                if (successedNum > 0)
                {
                    string msg = string.Format("[Voxel] {0} model{1} created in {2}sec.", successedNum, (successedNum > 1 ? "s" : ""), taskTime.ToString("0.00"));
                    if (LogMessage)
                    {
                        Debug.Log(msg);
                    }
                    if (ShowDialog)
                    {
                        Util.Dialog("Success", msg, "OK");
                    }
                }
                if (failedNum > 0)
                {
                    string msg = string.Format(failMessage, failedNum.ToString(), (failedNum > 1 ? "s" : ""));
                    if (LogMessage)
                    {
                        Debug.LogWarning(msg);
                    }
                    if (ShowDialog)
                    {
                        Util.Dialog("Warning", msg, "OK");
                    }
                }
            } catch (System.Exception ex) {
                Debug.LogError(ex.Message);
            }

            Util.ClearProgressBar();
        }
示例#2
0
        private bool SpawnVoxelFaces()
        {
            // Faces
            if (!Data)
            {
                return(false);
            }

            int[,,] voxels = Data.Voxels[CurrentModelIndex];
            bool  tooLargeAsked = false;
            int   sizeX         = voxels.GetLength(0);
            int   sizeY         = voxels.GetLength(1);
            int   sizeZ         = voxels.GetLength(2);
            float maxSize       = Mathf.Max(sizeX, sizeY, sizeZ, 12);

            CameraSizeMin = 5f;
            CameraSizeMax = maxSize * 2f;
            SetCameraSize((CameraSizeMin + CameraSizeMax) * 0.5f);
            SetCameraFarClip(maxSize * 4f);
            SetCameraPosition(new Vector3(0f, 0f, -maxSize * 2f));
            ContainerTF.localPosition = -new Vector3(sizeX / 2f, sizeY / 2f, sizeZ / 2f);

            switch (CurrentEditorMode)
            {
            case EditorMode.Rigging:
                // Quads
                for (int x = 0; x < sizeX; x++)
                {
                    Util.ProgressBar("", "Importing...", ((float)x / (sizeX - 1)) * 0.5f + 0.5f);
                    for (int y = 0; y < sizeY; y++)
                    {
                        for (int z = 0; z < sizeZ; z++)
                        {
                            if (voxels[x, y, z] == 0)
                            {
                                continue;
                            }
                            AddQuad(x, y, z, voxels[x, y, z], sizeX, sizeY, sizeZ);

                            if (!tooLargeAsked && ContainerTF.childCount > MAX_QUAD_COUNT)
                            {
                                Util.ClearProgressBar();
                                bool go = Util.Dialog("", "This model is too large. Still want to edit it ?", "Yes", "Cancel");
                                tooLargeAsked = true;
                                if (!go)
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
                break;

            case EditorMode.Sprite:
            case EditorMode.MapGenerator:
            case EditorMode.CharacterGenerator:
                bool allInOne = CurrentEditorMode == EditorMode.CharacterGenerator;
                // Mesh
                var result = Core_Voxel.CreateModel(Data, 1f, false, Core_Voxel.LightMapSupportType.SmallTextureButNoLightmap, false, Vector3.one * 0.5f);
                var vModel = result.VoxelModels != null && result.VoxelModels.Length > 0 ? result.VoxelModels[0] : null;
                for (int modelIndex = allInOne ? 0 : CurrentModelIndex; modelIndex < (allInOne ? vModel.Meshs.Length : CurrentModelIndex + 1); modelIndex++)
                {
                    var vMesh = vModel != null && vModel.Meshs != null && vModel.Meshs.Length > modelIndex ? vModel.Meshs[modelIndex] : null;
                    if (vMesh == null)
                    {
                        break;
                    }
                    for (int i = 0; i < vMesh.Count; i++)
                    {
                        var       mesh = vMesh.GetMeshAt(i);
                        Texture2D texture;
                        if (IsSpriting)
                        {
                            texture = vModel != null && vModel.Textures != null && vModel.Textures.Length > CurrentModelIndex ? vModel.Textures[CurrentModelIndex] : null;
                        }
                        else
                        {
                            texture = CurrentModelIndex == 0 && vModel != null && vModel.Textures != null && vModel.Textures.Length > 0 ? vModel.Textures[0] : null;
                        }
                        if (!mesh)
                        {
                            break;
                        }

                        FixMeshColorByNormal(mesh);

                        var meshTF = new GameObject("_mesh", typeof(MeshRenderer), typeof(MeshFilter)).transform;
                        meshTF.SetParent(ContainerTF);
                        meshTF.localPosition    = new Vector3(sizeX / 2f, sizeY / 2f, sizeZ / 2f);
                        meshTF.localScale       = Vector3.one;
                        meshTF.localRotation    = Quaternion.identity;
                        meshTF.gameObject.layer = LAYER_ID;

                        var mf = meshTF.GetComponent <MeshFilter>();
                        mf.mesh = mesh;

                        var mr  = meshTF.GetComponent <MeshRenderer>();
                        var mat = new Material(MESH_SHADER);
                        mat.SetColor(COLOR_SHADER_ID, Color.white);
                        mat.mainTexture      = texture ? texture : Texture2D.blackTexture;
                        mr.material          = mat;
                        mr.receiveShadows    = false;
                        mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
                    }
                }
                break;
            }
            return(true);
        }
 private static void RefreshMergeSetting()
 {
     Core_Voxel.SetMergeInDirection(OptimizeFront, OptimizeBack, OptimizeUp, OptimizeDown, OptimizeLeft, OptimizeRight);
 }