Beispiel #1
0
        // Spawn Roots
        private bool SpawnRoot()
        {
            if (!Data)
            {
                return(false);
            }

            try {
                // Root
                Root = new GameObject(ROOT_NAME).transform;
                Root.gameObject.hideFlags = ShowRoot ? HideFlags.DontSave : HideFlags.HideAndDontSave;
                Root.position             = Random.insideUnitSphere * Random.Range(10240f, 20480f);
                Root.localScale           = Vector3.one;

                // Camera
                SpawnCamera();

                // Environment
                SpawnContainer();
                SpawnBox();
                SpawnDirectionSign();
                SpawnLight();

                // UI
                SpawnHightlight();
                SpawnCube();

                // Edit Mode
                SpawnRigRoot();
                SpawnSpriteRoot();
                SpawnGeneratorRoot();
                SpawnCombinerRoot();
                SpawnSkeletalRoot();
                SpawnMoveBoneAsixRoot();

                // Faces (Must be Last)
                bool success = SpawnVoxelFaces();
                Util.ClearProgressBar();
                return(success);
            } catch (System.Exception ex) {
                Util.ClearProgressBar();
                Debug.LogError(ex.Message);
                return(false);
            }
        }
 private void ProgressStep(float progress01, int step, GeneratorMode mod)
 {
     string[] labels = mod == GeneratorMode.Map ? MAP_STEP_LABELS : CHARACTER_STEP_LABELS;
     if (step >= labels.Length)
     {
         Util.ClearProgressBar();
         return;
     }
     step = Mathf.Clamp(step, 0, labels.Length - 1);
     Util.ProgressBar(
         "Working...",
         string.Format(
             labels[step],
             (step + 1).ToString(),
             labels.Length.ToString(),
             (progress01 * 100).ToString("00")
             ),
         progress01 / labels.Length + step / (labels.Length - 1f)
         );
 }
        private void ProgressStep(float progress01, float step01, GeneratorMode mod)
        {
            if (step01 > 1.001f)
            {
                Util.ClearProgressBar();
                return;
            }
            string[] labels = mod == GeneratorMode.Map ? MAP_STEP_LABELS : CHARACTER_STEP_LABELS;
            int      step   = Mathf.Clamp((int)(step01 * (labels.Length + 1)), 0, labels.Length - 1);

            Util.ProgressBar(
                "Generating...",
                string.Format(
                    labels[step],
                    (step + 1).ToString(),
                    labels.Length.ToString(),
                    (progress01 * 100).ToString("00")
                    ),
                progress01 / labels.Length + step01
                );
        }
        private void CombinePrefab(bool combineMesh)
        {
            if (!CombinerEditingRoot || CombinerEditingRoot.childCount == 0)
            {
                return;
            }
            var path = Util.FixedRelativePath(EditorUtility.SaveFilePanel("Save Combined Prefab", "Assets", "Combined Prefab", "prefab"));

            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            try {
                var result = Core_Combine.Combine(
                    CombinerEditingRoot,
                    combineMesh,
                    (msg, progress) => {
                    Util.ProgressBar("Combining", msg, progress);
                }
                    );
                if (result != null)
                {
                    // Empty Prefab
                    if (Util.FileExists(path))
                    {
                        Object[] things = AssetDatabase.LoadAllAssetRepresentationsAtPath(path);
                        foreach (Object o in things)
                        {
                            DestroyImmediate(o, true);
                        }
                    }
                    else
                    {
                        var tempObject = new GameObject();
                        SavePrefab(tempObject, path, true);
                        DestroyImmediate(tempObject, false);
                    }

                    // Add Sub Objects In
                    AssetDatabase.AddObjectToAsset(result.Texture, path);
                    for (int i = 0; i < result.Meshs.Count; i++)
                    {
                        AssetDatabase.AddObjectToAsset(result.Meshs[i], path);
                    }
                    for (int i = 0; i < result.Materials.Count; i++)
                    {
                        AssetDatabase.AddObjectToAsset(result.Materials[i], path);
                    }

                    // Override Prefab
                    SavePrefab(result.Root.gameObject, path);
                    DestroyImmediate(result.Root.gameObject, false);

                    // Done
                    AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                    AssetDatabase.SaveAssets();
                    Resources.UnloadUnusedAssets();
                }
            } catch (System.Exception ex) { Debug.LogError(ex.Message); }

            Util.ClearProgressBar();
        }
Beispiel #5
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 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();
        }