private void CreateScreenShot() { string path = Util.FixPath(EditorUtility.SaveFilePanel("Select Export Path", "Assets", Util.GetNameWithoutExtension(VoxelFilePath) + "_screenShot", "png")); if (!string.IsNullOrEmpty(path)) { path = EditorUtil.RelativePath(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 { EditorUtil.Dialog("Warning", "Export path must in Assets folder.", "OK"); } } }
private bool SpawnVoxelFaces() { // Faces if (!Data) { return(false); } Vector3 pos, rot, scl; Data.GetModelTransform(CurrentModelIndex, out pos, out rot, out scl); 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); 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); for (int x = 0; x < sizeX; x++) { EditorUtil.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) { EditorUtil.ClearProgressBar(); bool go = EditorUtil.Dialog("", "This model is too large. Still want to edit it ?", "Yes", "Cancel"); tooLargeAsked = true; if (!go) { return(false); } } } } } return(true); }
private static bool BrowseExportPath() { string newPath = Util.FixPath(EditorUtility.OpenFolderPanel("Select Export Path", ExportPath, "")); if (!string.IsNullOrEmpty(newPath)) { newPath = EditorUtil.RelativePath(newPath); if (!string.IsNullOrEmpty(newPath)) { ExportPath.Value = newPath; return(true); } else { EditorUtil.Dialog("Warning", "Export path must in Assets folder.", "OK"); } } return(false); }
// 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 = EditorUtil.RelativePath(path); if (!string.IsNullOrEmpty(path)) { var result = _8bit ? Core_Sprite.Create8bitSprite(Data, CurrentModelIndex) : Core_Sprite.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 { EditorUtil.Dialog("Warning", "Export path must in Assets folder.", "OK"); } } }
private static Color[][] GetColorss( Voxel[,,] voxels, Color[] palette, Vector3 mainPivot, int spriteNum, float lightIntensity, out int[] widths, out int[] heights, out Vector2[] pivots ) { int voxelSizeX = voxels.GetLength(0); int voxelSizeY = voxels.GetLength(1); int voxelSizeZ = voxels.GetLength(2); widths = new int[spriteNum]; heights = new int[spriteNum]; pivots = new Vector2[spriteNum]; if (voxelSizeX * voxelSizeY * voxelSizeZ > MAX_ROOM_NUM) { if (!EditorUtil.Dialog( "Warning", "Model Is Too Large !\nIt may takes very long time to create this sprite.\nAre you sure to do that?", "Just Go ahead!", "Cancel" )) { return(null); } } Color[][] colorss = new Color[spriteNum][]; Vector3 pivotOffset = new Vector3( voxelSizeX * mainPivot.x, voxelSizeZ * mainPivot.y, voxelSizeY * mainPivot.z ); for (int index = 0; index < spriteNum; index++) { float angleY = SPRITE_ANGLE_SIMPLE[index]; Quaternion cameraRot = Quaternion.Inverse(Quaternion.Euler(CAMERA_ANGLE, angleY, 0f)); Vector3 minPos; Vector3 maxPos; // Get faces List <VoxelFace> faces = GetFaces( voxels, palette, new Vector3(voxelSizeX, voxelSizeY, voxelSizeZ), mainPivot, cameraRot, pivotOffset, index, lightIntensity, out minPos, out maxPos ); // Get Pivot01 Vector3 pivot = new Vector3( Mathf.LerpUnclamped(0f, voxelSizeX, mainPivot.x), Mathf.LerpUnclamped(0f, voxelSizeZ, mainPivot.y), Mathf.LerpUnclamped(0f, voxelSizeY, mainPivot.z) ); pivot = cameraRot * new Vector3( pivot.x - voxelSizeX * mainPivot.x, pivot.y - voxelSizeZ * mainPivot.y, pivot.z - voxelSizeY * mainPivot.z ) + pivotOffset; pivot = new Vector3( (pivot.x - minPos.x) / (maxPos.x - minPos.x), (pivot.y - minPos.y) / (maxPos.y - minPos.y), (pivot.z - minPos.z) / (maxPos.z - minPos.z) ); // Get Pixels int minPixelX; int minPixelY; int maxPixelX; int maxPixelY; // Get Pixels List <Pixel> pixels = GetPixels( faces, out minPixelX, out minPixelY, out maxPixelX, out maxPixelY ); // W and H int width = maxPixelX - minPixelX + 1 + 2; int height = maxPixelY - minPixelY + 1 + 2; // Get Colorss colorss[index] = new Color[width * height]; int len = pixels.Count; for (int i = 0; i < len; i++) { int id = (pixels[i].Y - minPixelY + 1) * width + (pixels[i].X - minPixelX + 1); colorss[index][id] = pixels[i].Color; } // Cheat { List <int> cheatPixels = new List <int>(); List <Color> cheatColors = new List <Color>(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color c = CheckPixelsAround_Cheat(colorss[index], x, y, width, height); if (c != Color.clear) { cheatPixels.Add(y * width + x); cheatColors.Add(c); } } } int cheatCount = cheatPixels.Count; for (int i = 0; i < cheatCount; i++) { colorss[index][cheatPixels[i]] = cheatColors[i]; } } // Final widths[index] = width; heights[index] = height; pivots[index] = pivot; } return(colorss); }
private static void DoTask(Task task) { if (TaskMap.Count == 0) { return; } if (PathExportMod == 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 <VoxelCore.Result>(); EditorUtil.ProgressBar("Creating", "Starting task...", 0f); EditorUtil.StartWatch(); ForAllSelection((pathData) => { try { string fileName = Util.GetNameWithoutExtension(pathData.Path); EditorUtil.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 = VoxelCore.CreateLodModel(voxelData, ModelScale, task == Task.Lod ? LodNum : 1, LightMapSupportMode); if (PathExportMod == 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 = VoxelCore.VoxelToJson(voxelData); string path = PathExportMod == ExportMod.OriginalPath ? Util.ChangeExtension(pathData.Path, ".json") : Util.CombinePaths(ExportPath, pathData.Root, fileName + ".json"); Util.CreateParent(path); Util.Save(json, path); } break; case Task.ToVox: case Task.ToQb: // Json To Voxel string aimEx = task == Task.ToVox ? ".vox" : ".qb"; if (pathData.Extension == ".json") { voxelData = VoxelCore.JsonToVoxel(Util.Load(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 = PathExportMod == 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 { EditorVoxelCore.CreateFileForResult(resultList, TheShader, ModelScale); double taskTime = EditorUtil.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) { EditorUtil.Dialog("Success", msg, "OK"); } } if (failedNum > 0) { string msg = string.Format(failMessage, failedNum.ToString(), (failedNum > 1 ? "s" : "")); if (LogMessage) { Debug.LogWarning(msg); } if (ShowDialog) { EditorUtil.Dialog("Warning", msg, "OK"); } } } catch (System.Exception ex) { Debug.LogError(ex.Message); } EditorUtil.ClearProgressBar(); }