Ejemplo n.º 1
0
        private void OnPreprocessTexture()
        {
            string          fullPath = Util.GetFullPath(assetImporter.assetPath);
            TextureImporter ti       = assetImporter as TextureImporter;

            if (TextureList.Contains(fullPath))
            {
                ti.alphaIsTransparency = Screenshot;
                ti.isReadable          = true;
                ti.mipmapEnabled       = false;
                ti.npotScale           = TextureImporterNPOTScale.None;
                ti.filterMode          = Screenshot ? FilterMode.Bilinear : FilterMode.Point;
                ti.textureCompression  = TextureImporterCompression.Uncompressed;
                ti.textureShape        = TextureImporterShape.Texture2D;
                ti.textureType         = TextureImporterType.Default;
                ti.wrapMode            = TextureWrapMode.Clamp;
                ti.maxTextureSize      = 8192;
            }
            else if (SpriteMap.ContainsKey(fullPath))
            {
                var config = SpriteMap[fullPath];

                // Impoert
                ti.isReadable          = true;
                ti.alphaIsTransparency = true;
                ti.filterMode          = FilterMode.Point;
                ti.mipmapEnabled       = false;
                ti.textureType         = TextureImporterType.Sprite;
                ti.spriteImportMode    = SpriteImportMode.Multiple;
                ti.maxTextureSize      = 8192;
                ti.textureCompression  = TextureImporterCompression.Uncompressed;


                // Sprites
                Rect[] rects = config.spriteRects;
                List <SpriteMetaData> newData = new List <SpriteMetaData>();
                for (int i = 0; i < rects.Length; i++)
                {
                    SpriteMetaData smd = new SpriteMetaData()
                    {
                        pivot     = config.Pivots[i],
                        alignment = 9,
                        name      = Util.GetNameWithoutExtension(fullPath) + "_" + config.Names[i],
                        rect      = rects[i]
                    };
                    newData.Add(smd);
                }
                ti.spritesheet = newData.ToArray();
            }
        }
        private void CreateScreenShot()
        {
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + "_screenShot", "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    bool oldShow = ShowBackgroundBox;
                    CubeTF.gameObject.SetActive(false);
                    SetBoxBackgroundActive(false);
                    var texture = Util.RenderTextureToTexture2D(Camera);
                    if (texture)
                    {
                        texture = Util.TrimTexture(texture, 0.01f, 12);
                        Util.ByteToFile(texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddScreenshot(path);
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh();
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                    CubeTF.gameObject.SetActive(true);
                    SetBoxBackgroundActive(oldShow);
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
        // Data



        #endregion



        #region --- TSK ---



        private void CreateSprite(bool _8bit)
        {
            if (!Data)
            {
                return;
            }
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + (_8bit ? "_8bit" : "_2D"), "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    var result = _8bit ?
                                 EditorSpriteCore.Create8bitSprite(Data, CurrentModelIndex) :
                                 EditorSpriteCore.Create2DSprite(Data, CurrentModelIndex);
                    if (result.Texture)
                    {
                        Util.ByteToFile(result.Texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddSprite(path, new VoxelPostprocessor.SpriteConfig()
                        {
                            width       = result.Width,
                            height      = result.Height,
                            Pivots      = result.Pivots,
                            spriteRects = result.Rects,
                            Names       = result.NameFixes,
                        });
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
Ejemplo n.º 4
0
        private void CreateSprite(Core_Sprite.SpriteType type)
        {
            if (!Data)
            {
                return;
            }
            string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + type.ToString(), "png"));

            if (!string.IsNullOrEmpty(path))
            {
                path = Util.FixedRelativePath(path);
                if (!string.IsNullOrEmpty(path))
                {
                    bool oldShow = ShowBackgroundBox;
                    CubeTF.gameObject.SetActive(false);
                    SetBoxBackgroundActive(false);

                    var result = Core_Sprite.CreateSprite(
                        Data,
                        CurrentModelIndex,
                        type,
                        GetSpriteNum(type),
                        GetSpriteLight(type),
                        GetSpritePivot(type),
                        Camera,
                        Sprite25DCameraScale
                        );

                    CubeTF.gameObject.SetActive(true);
                    SetBoxBackgroundActive(oldShow);

                    if (result.Texture)
                    {
                        Util.ByteToFile(result.Texture.EncodeToPNG(), path);
                        VoxelPostprocessor.AddSprite(path, new VoxelPostprocessor.SpriteConfig()
                        {
                            width       = result.Width,
                            height      = result.Height,
                            Pivots      = result.Pivots,
                            spriteRects = result.Rects,
                            Names       = result.NameFixes,
                        });
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                        EditorApplication.delayCall += VoxelPostprocessor.ClearAsset;
                    }
                }
                else
                {
                    Util.Dialog("Warning", "Export path must in Assets folder.", "OK");
                }
            }
        }
Ejemplo n.º 5
0
        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();
        }