/// <summary> /// Saves the part at index (x,y,z). /// This function is placed in editor script because it calls AssetDatabase functions which are in UnityEditor namespace. /// </summary> /// <param name="x">The x index.</param> /// <param name="y">The y index.</param> /// <param name="z">The z index.</param> /// <param name="path">Path where the mesh files will be saved (starting from Assets/...).</param> public void SavePart(int x, int y, int z, string path) { VTPart vp = v.getPart(x, y, z); foreach (VTChunk c in vp._chunks) { if (c == null) { continue; } string p = path + "/" + c.gameObject.name + ".asset"; string sysPath = p.Substring("Assets".Length); // Strip "Assets" from the path if (System.IO.File.Exists(Application.dataPath + sysPath)) { System.IO.File.Delete(Application.dataPath + sysPath); } if (AssetDatabase.LoadMainAssetAtPath(p) != null) { AssetDatabase.DeleteAsset(p); } AssetDatabase.CreateAsset(c.GetComponent <MeshFilter>().sharedMesh, p); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); // Load back saved mesh. // Just a precautionary measure to ensure the mesh has been saved properly. string pp = p.Substring("Assets/Resources/".Length); pp = pp.Substring(0, pp.IndexOf(".")); Object[] o = Resources.LoadAll(pp); c.GetComponent <MeshFilter>().sharedMesh = o[0] as Mesh; } }
void MakePart(int x, int y, int z) { VTPart p = v.getPart(x, y, z); string makeTitle = "Making " + v.partName(x, y, z); string saveTitle = "Saving " + v.partName(x, y, z); string f1 = "/" + v.partName(x, y, z); try { EditorUtility.DisplayProgressBar(makeTitle, "", 0); for (int i = 0; i < v.lod.Length; i++) { p.Make(v.lod[i].gridSize); EditorUtility.DisplayProgressBar(makeTitle, "", ((float)i + 0.5f) / (float)v.lod.Length); string f0 = "/Resources/VT/LOD" + i; if (!System.IO.Directory.Exists(Application.dataPath + f0 + f1)) { System.IO.Directory.CreateDirectory(Application.dataPath + f0 + f1); } SavePart(x, y, z, "Assets" + f0 + f1); EditorUtility.DisplayProgressBar(saveTitle, "", (float)(i + 1) / (float)v.lod.Length); } } catch { EditorUtility.ClearProgressBar(); throw; } EditorUtility.ClearProgressBar(); }
void OnSceneGUI() { v = target as VoxelTerrain; if (!v.initialized) { return; } VTPart part = v.getPart(v.edx, v.edy, v.edz); Event e = Event.current; if (e.type == EventType.keyDown) { if (e.control) { if (e.keyCode == KeyCode.RightArrow) { v.edx++; } if (e.keyCode == KeyCode.LeftArrow) { v.edx--; } if (e.keyCode == KeyCode.UpArrow) { v.edz++; } if (e.keyCode == KeyCode.DownArrow) { v.edz--; } if (e.keyCode == KeyCode.Keypad8) { v.edy++; } if (e.keyCode == KeyCode.Keypad2) { v.edy--; } v.edx = Mathf.Clamp(v.edx, 0, v.npart - 1); v.edy = Mathf.Clamp(v.edy, 0, v.npart - 1); v.edz = Mathf.Clamp(v.edz, 0, v.npart - 1); Vector3 cp = Camera.current.transform.position; v.LoadMeshes(cp); } } Vector3 p = new Vector3(v.edx + 0.5f, v.edy + 0.5f, v.edz + 0.5f) * v.partSize; partCube.transform.position = p; partCube.transform.localScale = v.partSize * Vector3.one; /* * int l=v.getPart (v.edx,v.edy,v.edz).getLOD (Camera.current.transform.position); * Handles.BeginGUI (); * GUI.Label(new Rect(10,10,100,100),"LOD: "+l); * Handles.EndGUI (); */ }
/// <summary> /// Initialize the VoxelTerrain and its parts (npart^3 in number). /// This is called from the editor script /// Can Initialize multiple times - each initialize will reset the terrain. /// </summary> public void Init() { // Set to true to signal editor script initialized = true; // Initialize part array _part = new VTPart[npart * npart * npart]; for (int i = 0; i < transform.childCount;) { // Delete all parts if (transform.GetChild(i).name.StartsWith("Part")) { DestroyImmediate(transform.GetChild(i).gameObject); } else { i++; } } // Delete previous mesh gameobject if (GameObject.Find("VTMesh") != null) { DestroyImmediate(GameObject.Find("VTMesh")); } // LOD list contains LOD arranged in increasing order of distance // Sort LOD list according to distance System.Array.Sort(lod, (LOD l0, LOD l1) => { if (l0.maxDis < l1.maxDis) { return(-1); } if (l0.maxDis > l1.maxDis) { return(1); } return(0); }); for (int i = 0; i < npart; i++) { for (int j = 0; j < npart; j++) { for (int k = 0; k < npart; k++) { GameObject g = new GameObject(partName(i, j, k)); VTPart p = g.AddComponent <VTPart>(); p.Init(this, i, j, k); setPart(i, j, k, p); g.transform.parent = transform; } } } }
public void setPart(int i, int j, int k, VTPart p) { _part[i * npart * npart + j * npart + k] = p; }