public static void CreateVoxels(MVVoxel[] voxels, MVVoxModel voxModel) { MVVoxelChunk chunk = voxModel.vox.voxelChunk; float sizePerVox = voxModel.sizePerVox; float cx = sizePerVox * chunk.sizeX / 2; float cy = sizePerVox * chunk.sizeY / 2; float cz = sizePerVox * chunk.sizeZ / 2; Material mat = (voxModel.voxMaterial != null) ? voxModel.voxMaterial : MVImporter.DefaultMaterial; List <GameObject> objects = new List <GameObject> (); foreach (MVVoxel voxel in voxels) { float px = voxel.x * sizePerVox - cx, py = voxel.y * sizePerVox - cy, pz = voxel.z * sizePerVox - cz; GameObject go = MVImporter.CreateGameObject(voxModel.gameObject.gameObject.transform, new Vector3(px, py, pz), string.Format("Voxel ({0}, {1}, {2})", voxel.x, voxel.y, voxel.z), MVImporter.CubeMeshWithColor(sizePerVox, voxModel.vox.palatte [chunk.voxels [voxel.x, voxel.y, voxel.z] - 1]), mat); MVVoxModelVoxel v = go.AddComponent <MVVoxModelVoxel> (); v.voxel = new MVVoxel() { x = voxel.x, y = voxel.y, z = voxel.z, colorIndex = chunk.voxels [voxel.x, voxel.y, voxel.z] }; objects.Add(go); } Selection.objects = objects.ToArray(); }
static void Load() { string path = EditorUtility.OpenFilePanel( "Open VOX model", "Assets/MagicaVoxel/Vox", "vox" ); if (path != null && path.Length > 0) { string alphaMaskPath = string.Empty; if (EditorUtility.DisplayDialog("Question", "Do you want to load an alpha mask model?", "yes", "no")) { alphaMaskPath = EditorUtility.OpenFilePanel( "Open VOX model for alpha mask", "Assets/MagicaVoxel/Vox", "vox" ); } GameObject go = new GameObject(); go.name = System.IO.Path.GetFileNameWithoutExtension(path); MVVoxModel voxModel = go.AddComponent <MVVoxModel> (); voxModel.ed_filePath = path; voxModel.ed_alphaMaskFilePath = alphaMaskPath; voxModel.LoadVOXFile(path, alphaMaskPath, voxModel.ed_importAsIndividualVoxels); } }
public static void CreateVoxels(MVVoxel[] voxels, MVVoxModel voxModel) { MVVoxelChunk chunk = voxModel.vox.voxelChunk; float sizePerVox = voxModel.sizePerVox; float cx = sizePerVox * chunk.sizeX / 2; float cy = sizePerVox * chunk.sizeY / 2; float cz = sizePerVox * chunk.sizeZ / 2; Material mat = (voxModel.voxMaterial != null) ? voxModel.voxMaterial : MVImporter.DefaultMaterial; List<GameObject> objects = new List<GameObject> (); foreach (MVVoxel voxel in voxels) { float px = voxel.x * sizePerVox - cx, py = voxel.y * sizePerVox - cy, pz = voxel.z * sizePerVox - cz; GameObject go = MVImporter.CreateGameObject (voxModel.gameObject.gameObject.transform, new Vector3 (px, py, pz), string.Format ("Voxel ({0}, {1}, {2})", voxel.x, voxel.y, voxel.z), MVImporter.CubeMeshWithColor (sizePerVox, voxModel.vox.palatte [chunk.voxels [voxel.x, voxel.y, voxel.z] - 1]), mat); MVVoxModelVoxel v = go.AddComponent<MVVoxModelVoxel> (); v.voxel = new MVVoxel () { x = voxel.x, y = voxel.y, z = voxel.z, colorIndex = chunk.voxels [voxel.x, voxel.y, voxel.z] }; objects.Add (go); } Selection.objects = objects.ToArray (); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); MVVoxModel voxModel = this.target as MVVoxModel; if (voxModel.vox != null) { AU.AUEditorUtility.ColoredLabel(string.Format("Controls ({3}: {0}x{1}x{2})", voxModel.vox.sizeX, voxModel.vox.sizeY, voxModel.vox.sizeZ, System.IO.Path.GetFileNameWithoutExtension(voxModel.ed_filePath)), Color.green); } else { AU.AUEditorUtility.ColoredLabel("Controls", Color.green); } AU.AUEditorUtility.ColoredHelpBox(Color.yellow, "Enabling this may create lots of GameObjects, careful when the vox model is big"); voxModel.ed_importAsIndividualVoxels = EditorGUILayout.Toggle("Import as Individual Voxels", voxModel.ed_importAsIndividualVoxels); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Load")) { string path = EditorUtility.OpenFilePanel( "Open VOX model", "Assets/MagicaVoxel/Vox", "vox" ); if (!string.IsNullOrEmpty(path)) { string alphaMaskPath = string.Empty; if (EditorUtility.DisplayDialog("Question", "Do you want to load an alpha mask model file?", "yes", "no")) { alphaMaskPath = EditorUtility.OpenFilePanel( "Open VOX model for alpha mask", "Assets/MagicaVoxel/Vox", "vox" ); } voxModel.ed_filePath = path; voxModel.ed_alphaMaskFilePath = alphaMaskPath; voxModel.LoadVOXFile(path, alphaMaskPath, voxModel.ed_importAsIndividualVoxels); } } if (GUILayout.Button("Reimport")) { voxModel.LoadVOXFile(voxModel.ed_filePath, voxModel.ed_alphaMaskFilePath, voxModel.ed_importAsIndividualVoxels); } if (GUILayout.Button("Clear")) { voxModel.ClearVoxMeshes(); } EditorGUILayout.EndHorizontal(); }
public static void CombineVoxels(MVVoxModelVoxel[] voxels) { if (voxels != null && voxels.Length > 0) { MVVoxelChunk chunk = new MVVoxelChunk(); MVVoxModel model = voxels [0].parentVoxModel; MVVoxelChunk origChunk = model.vox.voxelChunk; chunk.voxels = new byte[origChunk.sizeX, origChunk.sizeY, origChunk.sizeZ]; foreach (MVVoxModelVoxel v in voxels) { chunk.voxels [v.voxel.x, v.voxel.y, v.voxel.z] = v.voxel.colorIndex; } MVImporter.GenerateFaces(chunk); Mesh[] meshes = MVImporter.CreateMeshesFromChunk(chunk, model.vox.palatte, model.sizePerVox); if (meshes.Length > 1) { Debug.LogError("[MVCombine] Currently does not support combining voxels into multiple meshes, please reduce the number of voxels you are trying to combine"); return; } Material mat = (model.voxMaterial != null) ? model.voxMaterial : MVImporter.DefaultMaterial; int index = 0; foreach (Mesh mesh in meshes) { GameObject go = MVImporter.CreateGameObject(model.gameObject.transform, Vector3.zero, string.Format("VoxMesh ({0})", index), mesh, mat); MVVoxModelMesh voxMesh = go.AddComponent <MVVoxModelMesh> (); voxMesh.voxels = voxels.Select(o => o.voxel).ToArray(); Selection.activeGameObject = go; index++; } foreach (MVVoxModelVoxel v in voxels) { GameObject.DestroyImmediate(v.gameObject); } } else { Debug.LogError("[MVCombine] Invalid voxels"); } }
static void Load() { string path = EditorUtility.OpenFilePanel( "Open VOX model", "Assets/MagicaVoxel/Vox", "vox" ); if (path != null && path.Length > 0) { GameObject go = new GameObject(); go.name = System.IO.Path.GetFileNameWithoutExtension(path); MVVoxModel voxModel = go.AddComponent <MVVoxModel> (); voxModel.ed_filePath = path; voxModel.LoadVOXFile(path, voxModel.ed_importAsIndividualVoxels); } }
public override void OnInspectorGUI() { AU.AUEditorUtility.ColoredHelpBox(Color.yellow, "Combining multiple voxels into one"); GUILayout.BeginHorizontal(); if (GUILayout.Button("Combine Selected")) { if (this.targets != null && this.targets.Length > 1) { CombineVoxels(System.Array.ConvertAll(this.targets, item => (MVVoxModelVoxel)item)); } } if (GUILayout.Button("Combine All")) { MVVoxModel voxModel = (this.target as MVVoxModelVoxel).parentVoxModel; MVVoxModelVoxel[] allVoxels = voxModel.GetComponentsInChildren <MVVoxModelVoxel> (); CombineVoxels(allVoxels); } GUILayout.EndHorizontal(); }