public VoxelData GetVoxelData() { var data = new VoxelData() { Version = Version, Voxels = new List <int[, , ]>(), Palette = new List <Color>(Palette), Materials = new List <VoxelData.MaterialData>(Materials), Transforms = new Dictionary <int, VoxelData.TransformData>(), Groups = new Dictionary <int, VoxelData.GroupData>(), Shapes = new Dictionary <int, VoxelData.ShapeData>(), Rigs = new Dictionary <int, VoxelData.RigData>(), }; // Voxels for (int i = 0; i < Voxels.Length; i++) { var sourceV = Voxels[i]; int[,,] vs = new int[sourceV.SizeX, sourceV.SizeY, sourceV.SizeZ]; for (int x = 0; x < sourceV.SizeX; x++) { for (int y = 0; y < sourceV.SizeY; y++) { for (int z = 0; z < sourceV.SizeZ; z++) { vs[x, y, z] = sourceV.Get(x, y, z); } } } data.Voxels.Add(vs); } // Transforms for (int i = 0; i < Transforms.Length; i++) { data.Transforms.Add(TransformIndexs[i], Transforms[i]); } // Groups for (int i = 0; i < Groups.Length; i++) { var groupData = new VoxelData.GroupData() { ChildNodeId = Groups[i].ChildNodeId, Attributes = new Dictionary <string, string>(), }; for (int j = 0; j < Groups[i].AttKeys.Length; j++) { groupData.Attributes.Add(Groups[i].AttKeys[j], Groups[i].Attributes[j]); } data.Groups.Add(Groups[i].Index, groupData); } // Shapes for (int i = 0; i < Shapes.Length; i++) { var sourceShape = Shapes[i]; var shapeData = new VoxelData.ShapeData() { Attributes = new Dictionary <string, string>(), ModelData = new KeyValuePair <int, Dictionary <string, string> > [sourceShape.ModelDatas.Length], }; for (int j = 0; j < sourceShape.Attributes.Length; j++) { shapeData.Attributes.Add(sourceShape.AttKeys[j], sourceShape.Attributes[j]); } for (int j = 0; j < sourceShape.ModelDatas.Length; j++) { shapeData.ModelData[j] = new KeyValuePair <int, Dictionary <string, string> >( sourceShape.ModelDataIndexs[j], new Dictionary <string, string>() ); var sourceData = sourceShape.ModelDatas[j]; for (int k = 0; k < sourceData.Value.Length; k++) { shapeData.ModelData[j].Value.Add(sourceData.Key[k], sourceData.Value[k]); } } data.Shapes.Add(sourceShape.Index, shapeData); } // Rig for (int i = 0; i < Rigs.Length; i++) { var bones = Rigs[i].Bones; for (int j = 0; j < bones.Count; j++) { bones[j].Parent = null; int pIndex = bones[j].ParentIndex; if (pIndex >= 0 && pIndex < bones.Count) { bones[j].Parent = bones[pIndex]; } } data.Rigs.Add(RigIndexs[i], Rigs[i]); } return(data); }
public VoxelData GetVoxelData() { var data = new VoxelData { Version = 150, Palette = new List <Color>(), Transforms = new Dictionary <int, VoxelData.TransformData>() { { 0, new VoxelData.TransformData() { ChildID = 1, Hidden = false, LayerID = -1, Name = "", Reserved = -1, Frames = new VoxelData.TransformData.FrameData[1] { new VoxelData.TransformData.FrameData() { Position = Vector3.zero, Rotation = Vector3.zero, Scale = Vector3.one, } }, } }, }, Shapes = new Dictionary <int, VoxelData.ShapeData>(), Groups = new Dictionary <int, VoxelData.GroupData>() { { 1, new VoxelData.GroupData() { Attributes = new Dictionary <string, string>(), ChildNodeId = new int[MatrixList.Count], } } }, Materials = new List <VoxelData.MaterialData>(), Voxels = new List <int[, , ]>(), }; bool leftHanded = ZAxisOrientation == 0; var palette = new Dictionary <Color, int>(); for (int index = 0; index < MatrixList.Count; index++) { var m = MatrixList[index]; int[,,] voxels = new int[m.SizeX, m.SizeY, m.SizeZ]; for (int x = 0; x < m.SizeX; x++) { for (int y = 0; y < m.SizeY; y++) { for (int z = 0; z < m.SizeZ; z++) { int colorInt = m.Voxels[x, y, z]; if (colorInt == 0) { if (leftHanded) { voxels[x, y, m.SizeZ - z - 1] = 0; } else { voxels[x, y, z] = 0; } } else { var color = GetColor(colorInt); int cIndex; if (palette.ContainsKey(color)) { cIndex = palette[color]; } else { cIndex = palette.Count; palette.Add(color, cIndex); data.Palette.Add(color); } if (leftHanded) { voxels[x, y, m.SizeZ - z - 1] = cIndex + 1; } else { voxels[x, y, z] = cIndex + 1; } } } } } data.Voxels.Add(voxels); data.Groups[1].ChildNodeId[index] = index * 2 + 2; data.Transforms.Add(index * 2 + 2, new VoxelData.TransformData() { ChildID = index * 2 + 3, Hidden = false, LayerID = 0, Reserved = 0, Name = "", Frames = new VoxelData.TransformData.FrameData[1] { new VoxelData.TransformData.FrameData() { Position = leftHanded ? new Vector3(m.PosX + m.SizeX / 2, m.PosY + m.SizeY / 2, -(m.PosZ + Mathf.CeilToInt(m.SizeZ / 2f))) : new Vector3(m.PosX + m.SizeX / 2, m.PosY + m.SizeY / 2, m.PosZ + m.SizeZ / 2), Rotation = Vector3.zero, Scale = Vector3.one, } }, }); data.Shapes.Add(index * 2 + 3, new VoxelData.ShapeData() { Attributes = new Dictionary <string, string>(), ModelData = new KeyValuePair <int, Dictionary <string, string> >[1] { new KeyValuePair <int, Dictionary <string, string> >(index, new Dictionary <string, string>()) }, }); } return(data); }
public VoxelJsonData(VoxelData source) { // Version Version = source.Version; // Voxels Voxels = new IntArray3[source.Voxels.Count]; for (int i = 0; i < Voxels.Length; i++) { int sizeX = source.Voxels[i].GetLength(0); int sizeY = source.Voxels[i].GetLength(1); int sizeZ = source.Voxels[i].GetLength(2); var voxels = new IntArray3(sizeX, sizeY, sizeZ); var sourceVoxels = source.Voxels[i]; for (int x = 0; x < sizeX; x++) { for (int y = 0; y < sizeY; y++) { for (int z = 0; z < sizeZ; z++) { voxels.Set(x, y, z, sourceVoxels[x, y, z]); } } } Voxels[i] = voxels; } // Palette Palette = source.Palette.ToArray(); // Materials Materials = source.Materials.ToArray(); // Transforms var tfMap = source.Transforms; TransformIndexs = new int[tfMap.Count]; Transforms = new VoxelData.TransformData[tfMap.Count]; int index = 0; foreach (var tf in tfMap) { TransformIndexs[index] = tf.Key; Transforms[index] = tf.Value; index++; } // Groups var gpMap = source.Groups; Groups = new SerGroupData[gpMap.Count]; index = 0; foreach (var gp in gpMap) { Groups[index] = new SerGroupData() { Index = gp.Key, ChildNodeId = gp.Value.ChildNodeId, AttKeys = new string[gp.Value.Attributes.Count], Attributes = new string[gp.Value.Attributes.Count], }; var attMap = gp.Value.Attributes; int i = 0; foreach (var att in attMap) { Groups[index].AttKeys[i] = att.Key; Groups[index].Attributes[i] = att.Value; i++; } index++; } // Shapes var shMap = source.Shapes; Shapes = new SerShapeData[shMap.Count]; index = 0; foreach (var sh in shMap) { Shapes[index] = new SerShapeData { Index = sh.Key, ModelDataIndexs = new int[sh.Value.ModelData.Length], ModelDatas = new SerShapeData.ModelMap[sh.Value.ModelData.Length], AttKeys = new string[sh.Value.Attributes.Count], Attributes = new string[sh.Value.Attributes.Count], }; var attMap = sh.Value.Attributes; int i = 0; foreach (var att in attMap) { Shapes[index].AttKeys[i] = att.Key; Shapes[index].Attributes[i] = att.Value; i++; } for (i = 0; i < sh.Value.ModelData.Length; i++) { Shapes[index].ModelDataIndexs[i] = sh.Value.ModelData[i].Key; Shapes[index].ModelDatas[i] = new SerShapeData.ModelMap() { Key = new string[sh.Value.ModelData[i].Value.Count], Value = new string[sh.Value.ModelData[i].Value.Count], }; int j = 0; var map = sh.Value.ModelData[i].Value; foreach (var aj in map) { Shapes[index].ModelDatas[i].Key[j] = aj.Key; Shapes[index].ModelDatas[i].Value[j] = aj.Value; j++; } } index++; } // Rigs var rigMap = source.Rigs; RigIndexs = new int[rigMap.Count]; Rigs = new VoxelData.RigData[rigMap.Count]; index = 0; foreach (var rig in rigMap) { RigIndexs[index] = rig.Key; Rigs[index] = rig.Value; var bones = Rigs[index].Bones; for (int i = 0; i < bones.Count; i++) { bones[i].ParentIndex = bones.IndexOf(bones[i].Parent); } index++; } }
private static VoxelData GetVoxelDataFromVox(byte[] voxBytes, OnProgressHandler onProgress) { VoxelData data = new VoxelData(); if (!CheckID(voxBytes, "VOX ")) { Debug.LogError("Error with Magic Number. The file is not a vox file."); return(null); } using (MemoryStream ms = new MemoryStream(voxBytes)) { using (BinaryReader br = new BinaryReader(ms)) { // VOX_ br.ReadInt32(); // VERSION data.Version = System.BitConverter.ToInt32(br.ReadBytes(4), 0); // MAIN byte[] chunkId = br.ReadBytes(4); int mainChunkSize = br.ReadInt32(); int mainChildrenSize = br.ReadInt32(); br.ReadBytes(mainChunkSize); if (!CheckID(chunkId, "MAIN")) { Debug.LogError("Error with Main Chunk ID"); return(null); } // Containt int readSize = 0; Vector3 tempSize = new Vector3(); while (readSize < mainChildrenSize) { string id = GetID(br.ReadBytes(4)); readSize += 4; switch (id) { case "PACK": readSize += ReadPackChunk(br); break; case "SIZE": readSize += ReadSizeChunk(br, out tempSize); break; case "XYZI": int[,,] tempVoxels = new int[(int)tempSize.x, (int)tempSize.y, (int)tempSize.z]; readSize += ReadVoxelChunk(br, ref tempVoxels); data.Voxels.Add(tempVoxels); break; case "RGBA": readSize += ReadPalattee(br, ref data.Palette); break; case "nTRN": readSize += ReadTransform(br, ref data); break; case "nGRP": readSize += ReadGroup(br, ref data); break; case "nSHP": readSize += ReadShape(br, ref data); break; case "MATL": readSize += ReadMaterial(br, ref data); break; case "RIGG": readSize += ReadRig(br, ref data, 0); break; case "_RIG": readSize += ReadRig(br, ref data, VoxelData.RigData.CURRENT_VERSION); break; default: int chunkSize = br.ReadInt32(); int childrenSize = br.ReadInt32(); br.ReadBytes(chunkSize + childrenSize); readSize += chunkSize + childrenSize + 4 + 4; break; } } if (onProgress != null) { onProgress.Invoke((float)readSize / mainChildrenSize); } // Add Default Node if No Node if (data.Transforms.Count == 0) { data.ResetToDefaultNode(); } } } return(data); }
private static byte[] WriteMain(VoxelData data, OnProgressHandler onProgress) { const float STEP_COUNT = 5f; List <byte> bytes = new List <byte>(); if (data.Voxels.Count > 1) { // PACK //bytes.AddRange(WritePack(data)); } for (int i = 0; i < data.Voxels.Count; i++) { if (onProgress != null) { onProgress.Invoke((i + 1) / STEP_COUNT / data.Voxels.Count); } // SIZE bytes.AddRange(WriteSize(data.Voxels[i])); // XYZI bytes.AddRange(WriteVoxels(data.Voxels[i])); } // RGBA if (onProgress != null) { onProgress.Invoke((2 / STEP_COUNT)); } bytes.AddRange(WritePalette(data.Palette)); // TGS var tgsList = new SortedList <int, byte[]>(); if (onProgress != null) { onProgress.Invoke(3 / STEP_COUNT); } // nTRN foreach (var t in data.Transforms) { tgsList.Add(t.Key, WriteTransform(t.Key, t.Value)); } // nGRP foreach (var g in data.Groups) { tgsList.Add(g.Key, WriteGroup(g.Key, g.Value)); } // nSHP foreach (var s in data.Shapes) { tgsList.Add(s.Key, WriteShape(s.Key, s.Value)); } for (int i = 0; i < tgsList.Keys.Count; i++) { bytes.AddRange(tgsList[tgsList.Keys[i]]); } if (onProgress != null) { onProgress.Invoke(4 / STEP_COUNT); } // MATL for (int i = 0; i < data.Materials.Count; i++) { bytes.AddRange(WriteMaterial(data.Materials[i])); } if (onProgress != null) { onProgress.Invoke(5 / STEP_COUNT); } // RIGG foreach (var r in data.Rigs) { bytes.AddRange(WriteRig(r.Key, r.Value)); } return(bytes.ToArray()); }
private static int ReadRig(BinaryReader _br, ref VoxelData data, int version) { int chunkSize = _br.ReadInt32(); int childrenSize = _br.ReadInt32(); int id = _br.ReadInt32(); var rigData = new VoxelData.RigData() { Bones = new List <VoxelData.RigData.Bone>(), Weights = new List <VoxelData.RigData.Weight>(), Version = version, }; // Bone int boneCount = _br.ReadInt32(); for (int i = 0; i < boneCount; i++) { var bone = new VoxelData.RigData.Bone { Name = ReadString(_br), ParentIndex = _br.ReadInt32(), PositionX = _br.ReadInt32(), PositionY = _br.ReadInt32(), PositionZ = _br.ReadInt32(), }; rigData.Bones.Add(bone); } for (int i = 0; i < rigData.Bones.Count; i++) { int pIndex = rigData.Bones[i].ParentIndex; if (pIndex >= 0 && pIndex < rigData.Bones.Count) { rigData.Bones[i].Parent = rigData.Bones[pIndex]; } } // Weight int WeightCount = _br.ReadInt32(); for (int i = 0; i < WeightCount; i++) { var weight = new VoxelData.RigData.Weight { X = _br.ReadInt32(), Y = _br.ReadInt32(), Z = _br.ReadInt32(), BoneIndexA = _br.ReadInt32(), BoneIndexB = _br.ReadInt32(), }; rigData.Weights.Add(weight); } // Version rigData.FixVersion(); // End if (!data.Rigs.ContainsKey(id)) { data.Rigs.Add(id, rigData); } if (childrenSize > 0) { _br.ReadBytes(childrenSize); } return(chunkSize + childrenSize + 4 + 4); }
public static byte[] GetVoxelByte(VoxelData data, bool isVox, OnProgressHandler onProgress = null) { return(isVox ? GetVoxFromVoxelData(data, onProgress) : GetQbFromVoxelData(data)); }
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(); }
// Json public static string VoxelToJson(VoxelData data) { return(JsonUtility.ToJson(new VoxelJsonData(data), false)); }
public static Result CreateModel(VoxelData data, float scale, LightMapSupportType supportLightMap, bool supportRig) { if (data == null) { return(null); } Data = data; ModelScale = scale; _SupportLightMap = supportLightMap; SupportRig = supportRig; var result = new Result { VoxelModels = new Result.VoxelModel[1] { new Result.VoxelModel() { Meshs = new UnlimitiedMesh[data.Voxels.Count], Textures = new Texture2D[data.Voxels.Count], ModelSize = new Vector3[data.Voxels.Count], FootPoints = new Vector3[data.Voxels.Count], MaxModelBounds = new Int3(data.GetBounds()).Max, } } }; for (int index = 0; index < data.Voxels.Count; index++) { UnlimitiedMesh mesh; Texture2D texture; PointWeights = null; RootBones = null; Voxels = data.Voxels[index]; SizeX = Voxels.GetLength(0); SizeY = Voxels.GetLength(1); SizeZ = Voxels.GetLength(2); ModelIndex = index; GetFaces(); GetColors(); if (SupportRig) { var rootBoneList = GetChildBones(null); if (rootBoneList != null) { RootBones = rootBoneList.ToArray(); } GetWeights(); } GetAreas(); GetResultFromArea(out mesh, out texture); result.VoxelModels[0].Meshs[index] = mesh; result.VoxelModels[0].Textures[index] = texture; result.VoxelModels[0].ModelSize[index] = data.GetModelSize(index); result.VoxelModels[0].FootPoints[index] = data.GetFootPoint(index); result.VoxelModels[0].RootBones = RootBones; } result.VoxelModels[0].RootNode = GetTransformData(result.VoxelModels[0].Meshs, result.VoxelModels[0].Textures, 0); Data = null; Voxels = null; Faces = null; //Weights = null; PointWeights = null; RootBones = null; Colors = null; AreaList.Clear(); PackingList.Clear(); AreaPackingMap.Clear(); return(result); }