private void PlacePieces() { PaletteItem[] itemArray; #if UNITY_EDITOR itemArray = Resources.LoadAll <PaletteItem> ((EditorApplication.isPlaying && VGlobal.GetSetting().FakeDeco) ? vd.ArtPack : PathCollect.pieces); #else itemArray = Resources.LoadAll <PaletteItem> (VGlobal.GetSetting().FakeDeco ? vd.ArtPack : PathCollect.pieces); #endif foreach (Chunk c in chunks.Values) { for (int b = 0; b < c.cData.blockAirs.Count; b++) { BlockAir ba = c.cData.blockAirs [b]; for (int i = 0; i < ba.pieceNames.Length; i++) { for (int k = 0; k < itemArray.Length; k++) { if (ba.pieceNames [i] == itemArray [k].name) { PlacePiece( new WorldPos( c.cData.ChunkPos.x + ba.BlockPos.x, c.cData.ChunkPos.y + ba.BlockPos.y, c.cData.ChunkPos.z + ba.BlockPos.z), new WorldPos(i % 3, 0, (int)(i / 3)), itemArray [k].gameObject.GetComponent <LevelPiece> ()); } } } } } }
public void ChangePointY(int _y) { VGlobal vg = VGlobal.GetSetting(); _y = Mathf.Clamp(_y, 0, chunkY * vg.chunkSize - 1); pointY = _y; YColor = new Color( (20 + (pointY % 10) * 20) / 255f, (200 - Mathf.Abs((pointY % 10) - 5) * 20) / 255f, (200 - (pointY % 10) * 20) / 255f, 0.4f ); if (bColl) { bColl.center = new Vector3( chunkX * vg.chunkSize * vg.hw - vg.hw, pointY * vg.h + vg.hh, chunkZ * vg.chunkSize * vg.hd - vg.hd ); } if (chunks != null && chunks.Count > 0) { UpdateChunks(); } }
void Start() { #if UNITY_EDITOR if (!UnityEditor.EditorApplication.isPlaying && VGlobal.GetSetting().saveBackup) { BroadcastMessage("SubscribeEvent", SendMessageOptions.RequireReceiver); UnityEditor.EditorApplication.CallbackFunction _event = UnityEditor.EditorApplication.playmodeStateChanged; string log = ""; for (int i = 0; i < _event.GetInvocationList().Length; i++) { log = log + i + "/" + _event.GetInvocationList().Length + ": " + _event.GetInvocationList() [i].Method.ToString() + "\n"; } Debug.LogWarning(log); } #endif if (VGlobal.GetSetting().FakeDeco) { if (!deco) { deco = new GameObject("Decoration"); deco.transform.parent = transform; deco.transform.localPosition = Vector3.zero; deco.transform.localRotation = Quaternion.Euler(Vector3.zero); } CreateVoxels(); } }
void CreateVoxels() { for (int vi = 0; vi < dungeons.Count; vi++) { GameObject volume = new GameObject(); volume.name = "Volume" + dungeons [vi].position.ToString(); volume.transform.parent = deco.transform; volume.transform.localPosition = dungeons[vi].position; volume.transform.localRotation = dungeons[vi].rotation; for (int ci = 0; ci < dungeons [vi].volumeData.chunkDatas.Count; ci++) { ChunkData cData = dungeons [vi].volumeData.chunkDatas [ci]; GameObject chunk = Instantiate(Resources.Load(PathCollect.chunk) as GameObject, Vector3.zero, Quaternion.Euler(Vector3.zero)) as GameObject; chunk.name = "Chunk" + cData.ChunkPos.ToString(); chunk.transform.parent = volume.transform; VGlobal vg = VGlobal.GetSetting(); chunk.transform.localPosition = new Vector3(cData.ChunkPos.x * vg.w, cData.ChunkPos.y * vg.h, cData.ChunkPos.z * vg.d); chunk.transform.localRotation = Quaternion.Euler(Vector3.zero); Material vMat = Resources.Load(dungeons [vi].volumeData.vMaterial, typeof(Material)) as Material; if (vMat == null) { vMat = Resources.Load(PathCollect.pieces + "/Materials/Mat_Voxel", typeof(Material)) as Material; } chunk.GetComponent <Renderer> ().sharedMaterial = vMat; chunk.layer = LayerMask.NameToLayer("Floor"); Chunk c = chunk.GetComponent <Chunk> (); c.cData = cData; c.Init(); CreateMarkers(c, dungeons [vi].volumeData.ArtPack); } } }
private void PaintLayer(bool isErase) { RaycastHit gHit; Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); LayerMask _mask = 1 << LayerMask.NameToLayer("EditorLevel"); VGlobal vg = VGlobal.GetSetting(); bool isHit = Physics.Raycast(worldRay, out gHit, vg.editDis, _mask); WorldPos pos; if (isHit && gHit.collider.GetComponentInParent <Volume> () == volume) { gHit.point = gHit.point + new Vector3(0f, -vg.h, 0f); gHit.point = volume.transform.InverseTransformPoint(gHit.point); pos = EditTerrain.GetBlockPos(gHit, true); volume.SetBlock(pos.x, pos.y, pos.z, isErase ? null : new Block()); Chunk chunk = volume.GetChunk(pos.x, pos.y, pos.z); if (chunk) { if (!dirtyChunks.ContainsKey(pos)) { dirtyChunks.Add(pos, chunk); } chunk.UpdateMeshFilter(); SceneView.RepaintAll(); } } EditorUtility.SetDirty(volume.vd); }
void CreateRuler() { ruler = new GameObject("Ruler"); ruler.layer = LayerMask.NameToLayer("Editor"); ruler.tag = PathCollect.rularTag; ruler.transform.parent = transform; mColl = ruler.AddComponent <MeshCollider> (); MeshData meshData = new MeshData(); VGlobal vg = VGlobal.GetSetting(); float x = -vg.hw; float y = -vg.hh; float z = -vg.hd; float w = chunkX * vg.chunkSize * vg.w + x; float d = chunkZ * vg.chunkSize * vg.d + z; meshData.useRenderDataForCol = true; meshData.AddVertex(new Vector3(x, y, z)); meshData.AddVertex(new Vector3(x, y, d)); meshData.AddVertex(new Vector3(w, y, d)); meshData.AddVertex(new Vector3(w, y, z)); meshData.AddQuadTriangles(); mColl.sharedMesh = null; Mesh cmesh = new Mesh(); cmesh.vertices = meshData.colVertices.ToArray(); cmesh.triangles = meshData.colTriangles.ToArray(); cmesh.RecalculateNormals(); mColl.sharedMesh = cmesh; ruler.transform.localPosition = Vector3.zero; ruler.transform.localRotation = Quaternion.Euler(Vector3.zero); }
void OnDrawGizmos() { Matrix4x4 oldMatrix = Gizmos.matrix; VGlobal vg = VGlobal.GetSetting(); Gizmos.color = (chunks.Count == 0) ? Color.red : YColor; Gizmos.matrix = transform.localToWorldMatrix; if (!EditorApplication.isPlaying && mColl) { Gizmos.DrawWireCube( new Vector3( chunkX * vg.chunkSize * vg.hw - vg.hw, chunkY * vg.chunkSize * vg.hh - vg.hh, chunkZ * vg.chunkSize * vg.hd - vg.hd), new Vector3( chunkX * vg.chunkSize * vg.w, chunkY * vg.chunkSize * vg.h, chunkZ * vg.chunkSize * vg.d) ); } if (!EditorApplication.isPlaying) { DrawGizmoBoxCursor(); DrawGizmoLayer(); DrawBlockHold(); } Gizmos.matrix = oldMatrix; }
private void DrawLayerMarker() { RaycastHit hit; Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); LayerMask _mask = 1 << LayerMask.NameToLayer("EditorLevel"); VGlobal vg = VGlobal.GetSetting(); bool isHit = Physics.Raycast(worldRay, out hit, vg.editDis, _mask); if (isHit && hit.collider.GetComponentInParent <Volume> () == volume) { RaycastHit hitFix = hit; WorldPos pos = EditTerrain.GetBlockPos(hitFix, false); hitFix.point = new Vector3(pos.x * vg.w, pos.y * vg.h, pos.z * vg.d); Handles.RectangleCap(0, hitFix.point + new Vector3(0, vg.hh, 0), Quaternion.Euler(90, 0, 0), vg.hw); Handles.DrawLine(hit.point, hitFix.point); volume.useBox = true; BoxCursorUtils.UpdateBox(volume.box, hitFix.point, Vector3.zero); } else { volume.useBox = false; } SceneView.RepaintAll(); }
private void DrawMarker(bool isErase) { RaycastHit hit; Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); LayerMask _mask = 1 << LayerMask.NameToLayer("Editor"); VGlobal vg = VGlobal.GetSetting(); bool isHit = Physics.Raycast(worldRay, out hit, vg.editDis, _mask); if (isHit && !isErase && hit.collider.GetComponentInParent <Volume> () == volume) { RaycastHit hitFix = hit; WorldPos pos = EditTerrain.GetBlockPos(hitFix, isErase ? false : true); hitFix.point = new Vector3(pos.x * vg.w, pos.y * vg.h, pos.z * vg.d); Handles.DrawLine(hit.point, hit.point + hit.normal); if (hit.collider.gameObject.tag == PathCollect.rularTag) { hit.normal = Vector3.zero; } BoxCursorUtils.UpdateBox(volume.box, hitFix.point, hit.normal); SceneView.RepaintAll(); } else { volume.useBox = false; SceneView.RepaintAll(); } }
private void DrawModeGUI() { VGlobal vg = VGlobal.GetSetting(); List <EditMode> modes = EditorUtils.GetListFromEnum <EditMode> (); List <string> modeLabels = new List <string> (); foreach (EditMode mode in modes) { modeLabels.Add(mode.ToString()); } float ButtonW = 80; Handles.BeginGUI(); GUI.color = new Color(volume.YColor.r, volume.YColor.g, volume.YColor.b, 1.0f); GUILayout.BeginArea(new Rect(10f, 10f, modeLabels.Count * ButtonW, 50f), "", EditorStyles.textArea); //根據選項數量決定寬度 GUI.color = Color.white; selectedEditMode = (EditMode)GUILayout.Toolbar((int)currentEditMode, modeLabels.ToArray(), GUILayout.ExpandHeight(true)); GUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Editable Distance", GUILayout.Width(105)); EditorGUI.BeginChangeCheck(); vg.editDis = GUILayout.HorizontalSlider(vg.editDis, 99f, 999f); if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(vg); } EditorGUILayout.LabelField(((int)vg.editDis).ToString(), GUILayout.Width(25)); GUILayout.EndHorizontal(); GUILayout.EndArea(); DrawLayerModeGUI(); Handles.EndGUI(); }
void CreateBox() { if (!box) { VGlobal vg = VGlobal.GetSetting(); box = BoxCursorUtils.CreateBoxCursor(this.transform, new Vector3(vg.w, vg.h, vg.d)); } }
private void OnDisable() { if (!VGlobal.GetSetting().debugRuler) { volume.ActiveRuler(false); } UnsubscribeEvents(); }
private void DrawGridMarker() { if (_pieceSelected == null) { return; } bool isNotLayer = (currentEditMode != EditMode.ObjectLayer); RaycastHit hit; Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); LayerMask _mask = isNotLayer ? 1 << LayerMask.NameToLayer("Editor") : 1 << LayerMask.NameToLayer("EditorLevel"); VGlobal vg = VGlobal.GetSetting(); bool isHit = Physics.Raycast(worldRay, out hit, (int)vg.editDis, _mask); if (isHit && hit.collider.GetComponentInParent <Volume> () == volume) { if (hit.normal.y <= 0) { return; } RaycastHit hitFix = hit; WorldPos pos = EditTerrain.GetBlockPos(hitFix, isNotLayer); hitFix.point = new Vector3(pos.x * vg.w, pos.y * vg.h, pos.z * vg.d); WorldPos gPos = EditTerrain.GetGridPos(hit.point); gPos.y = isNotLayer ? 0 : (int)vg.h; float gx = pos.x * vg.w + gPos.x + ((pos.x < 0) ? 1 : -1); float gy = pos.y * vg.h + gPos.y - vg.hh; float gz = pos.z * vg.d + gPos.z + ((pos.z < 0) ? 1 : -1); LevelPiece.PivotType pivot = _pieceSelected.pivot; if (CheckPlaceable((int)gPos.x, (int)gPos.z, pivot)) { Handles.color = Color.red; Handles.RectangleCap(0, new Vector3(gx, gy, gz), Quaternion.Euler(90, 0, 0), 0.5f); Handles.color = Color.white; } Handles.color = Color.white; Handles.lighting = true; Handles.RectangleCap(0, hitFix.point - new Vector3(0, vg.hh - gPos.y, 0), Quaternion.Euler(90, 0, 0), vg.hw); Handles.DrawLine(hit.point, hitFix.point); volume.useBox = true; BoxCursorUtils.UpdateBox(volume.box, hitFix.point, Vector3.zero); SceneView.RepaintAll(); } else { volume.useBox = false; SceneView.RepaintAll(); } }
public static WorldPos GetBlockPos(Vector3 pos) { VGlobal vg = VGlobal.GetSetting(); WorldPos blockPos = new WorldPos( Mathf.RoundToInt(pos.x / vg.w), Mathf.RoundToInt(pos.y / vg.h), Mathf.RoundToInt(pos.z / vg.d) ); return(blockPos); }
public Chunk GetChunk(int x, int y, int z) { WorldPos pos = new WorldPos(); float multiple = VGlobal.GetSetting().chunkSize; pos.x = Mathf.FloorToInt(x / multiple) * (int)multiple; pos.y = Mathf.FloorToInt(y / multiple) * (int)multiple; pos.z = Mathf.FloorToInt(z / multiple) * (int)multiple; return(chunks.ContainsKey(pos) ? chunks [pos] : null); }
public static WorldPos GetGridPos(Vector3 pos) { VGlobal vg = VGlobal.GetSetting(); WorldPos gridPos = new WorldPos( Mathf.RoundToInt((int)(pos.x + vg.hw) % (int)vg.w), Mathf.RoundToInt((int)(pos.y + vg.hh) % (int)vg.h), Mathf.RoundToInt((int)(pos.z + vg.hd) % (int)vg.d) ); return(gridPos); }
void Awake() { Volume[] v = transform.GetComponentsInChildren <Volume> (false); if (VGlobal.GetSetting().FakeDeco) { for (int i = 0; i < v.Length; i++) { v [i].enabled = false; GameObject.Destroy(v [i].gameObject); } } }
public void ChangeCutY(int _y) { VGlobal vg = VGlobal.GetSetting(); _y = Mathf.Clamp(_y, 0, chunkY * vg.chunkSize - 1); cutY = _y; if (chunks != null && chunks.Count > 0) { // PlacePieces (); UpdateChunks(); } }
public static WorldPos GetBlockPos(Vector3 pos, Transform localRoot) { VGlobal vg = VGlobal.GetSetting(); pos = localRoot.InverseTransformPoint(pos); WorldPos blockPos = new WorldPos( Mathf.RoundToInt(pos.x / vg.w), Mathf.RoundToInt(pos.y / vg.h), Mathf.RoundToInt(pos.z / vg.d) ); return(blockPos); }
void CreateLevelRuler() { layerRuler = new GameObject("LevelRuler"); layerRuler.layer = LayerMask.NameToLayer("EditorLevel"); layerRuler.transform.parent = transform; layerRuler.transform.localPosition = Vector3.zero; layerRuler.transform.localRotation = Quaternion.Euler(Vector3.zero); bColl = layerRuler.AddComponent <BoxCollider> (); VGlobal vg = VGlobal.GetSetting(); bColl.size = new Vector3(chunkX * vg.chunkSize * vg.w, 0f, chunkZ * vg.chunkSize * vg.d); ChangePointY(pointY); }
protected virtual MeshData FaceDataUp(Chunk chunk, int x, int y, int z, MeshData meshData) { VGlobal vg = VGlobal.GetSetting(); meshData.AddVertex(new Vector3(x * vg.w - vg.hw, y * vg.h + vg.hh, z * vg.d + vg.hd)); meshData.AddVertex(new Vector3(x * vg.w + vg.hw, y * vg.h + vg.hh, z * vg.d + vg.hd)); meshData.AddVertex(new Vector3(x * vg.w + vg.hw, y * vg.h + vg.hh, z * vg.d - vg.hd)); meshData.AddVertex(new Vector3(x * vg.w - vg.hw, y * vg.h + vg.hh, z * vg.d - vg.hd)); meshData.AddQuadTriangles(); //Add the following line to every FaceData function with the direction of the face meshData.uv.AddRange(FaceUVs(Direction.up)); return(meshData); }
void DestoryChunks() { int chunkSize = VGlobal.GetSetting().chunkSize; for (int x = 0; x < chunkX; x++) { for (int y = 0; y < chunkY; y++) { for (int z = 0; z < chunkZ; z++) { DestroyChunk(x * chunkSize, y * chunkSize, z * chunkSize); } } } }
void Update() { VGlobal vg = VGlobal.GetSetting(); float x = transform.position.x - transform.position.x % vg.w; float y = transform.position.y - transform.position.y % vg.h; float z = transform.position.z - transform.position.z % vg.d; transform.position = new Vector3(x, y, z); #if UNITY_EDITOR if (VGlobal.GetSetting().saveBackup) { CompileSave(); } #endif }
public static Vector3 GetPieceOffset(int x, int z) { Vector3 offset = Vector3.zero; VGlobal vg = VGlobal.GetSetting(); float hw = vg.hw; float hh = vg.hh; float hd = vg.hd; if (x == 0 && z == 0) { return(new Vector3(-hw, -hh, -hd)); } if (x == 1 && z == 0) { return(new Vector3(0, -hh, -hd)); } if (x == 2 && z == 0) { return(new Vector3(hw, -hh, -hd)); } if (x == 0 && z == 1) { return(new Vector3(-hw, -hh, 0)); } if (x == 1 && z == 1) { return(new Vector3(0, -hh, 0)); } if (x == 2 && z == 1) { return(new Vector3(hw, -hh, 0)); } if (x == 0 && z == 2) { return(new Vector3(-hw, -hh, hd)); } if (x == 1 && z == 2) { return(new Vector3(0, -hh, hd)); } if (x == 2 && z == 2) { return(new Vector3(hw, -hh, hd)); } return(offset); }
void CreateChunks() { int chunksize = VGlobal.GetSetting().chunkSize; for (int x = 0; x < chunkX; x++) { for (int y = 0; y < chunkY; y++) { for (int z = 0; z < chunkZ; z++) { Chunk newChunk = CreateChunk(x * chunksize, y * chunksize, z * chunksize); newChunk.Init(); } } } }
void DrawBlockHold() { VGlobal vg = VGlobal.GetSetting(); foreach (Chunk chunk in chunks.Values) { for (int i = 0; i < chunk.cData.blockHolds.Count; i++) { WorldPos bh = chunk.cData.blockHolds [i].BlockPos; Vector3 localPos = new Vector3(bh.x * vg.w, bh.y * vg.h, bh.z * vg.d); Gizmos.color = new Color(255f / 255f, 244f / 255f, 228f / 255f, 0.15f); Gizmos.DrawCube(localPos, new Vector3(vg.w, vg.h, vg.d)); Gizmos.color = new Color(255f / 255f, 244f / 255f, 228f / 255f, 1.0f); Gizmos.DrawSphere(localPos, 0.1f); } } }
public void PlacePiece(WorldPos bPos, WorldPos gPos, LevelPiece _piece, Transform _parent) { Vector3 pos = Volume.GetPieceOffset(gPos.x, gPos.z); VGlobal vg = VGlobal.GetSetting(); GameObject pObj = null; float x = bPos.x * vg.w + pos.x; float y = bPos.y * vg.h + pos.y; float z = bPos.z * vg.d + pos.z; if (_piece != null) { pObj = GameObject.Instantiate(_piece.gameObject); pObj.transform.parent = _parent; pObj.transform.localPosition = new Vector3(x, y, z); pObj.transform.localRotation = Quaternion.Euler(0, Volume.GetPieceAngle(gPos.x, gPos.z), 0); } }
void CompileSave() { if (EditorApplication.isCompiling && !compileSave) { if (VGlobal.GetSetting().saveBackup) { SaveTempWorld(); } compileSave = true; } if (!EditorApplication.isCompiling && compileSave) { LoadTempWorld(); compileSave = false; } }
private void PaintPieces(bool isErase) { if (_pieceSelected == null) { return; } bool canPlace = false; RaycastHit gHit; Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); LayerMask _mask = (currentEditMode == EditMode.Object) ? 1 << LayerMask.NameToLayer("Editor") : 1 << LayerMask.NameToLayer("EditorLevel"); VGlobal vg = VGlobal.GetSetting(); bool isHit = Physics.Raycast(worldRay, out gHit, vg.editDis, _mask); if (isHit && gHit.collider.GetComponentInParent <Volume> () == volume) { if (gHit.normal.y <= 0) { return; } gHit.point = volume.transform.InverseTransformPoint(gHit.point); WorldPos bPos = EditTerrain.GetBlockPos(gHit, (currentEditMode == EditMode.Object)); WorldPos gPos = EditTerrain.GetGridPos(gHit.point); gPos.y = 0; int gx = gPos.x; int gz = gPos.z; if (CheckPlaceable(gx, gz, _pieceSelected.pivot)) { canPlace = true; } if (canPlace) { volume.PlacePiece(bPos, gPos, isErase ? null : _pieceSelected); Chunk chunk = volume.GetChunk(bPos.x, bPos.y, bPos.z); chunk.UpdateMeshFilter(); chunk.UpdateMeshCollider(); EditorUtility.SetDirty(volume.vd); SceneView.RepaintAll(); } } }
void DrawGizmoLayer() { VGlobal vg = VGlobal.GetSetting(); if (pointer) { for (int xi = 0; xi < chunkX * vg.chunkSize; xi++) { for (int zi = 0; zi < chunkZ * vg.chunkSize; zi++) { float cSize; cSize = (GetBlock(xi, pointY, zi) == null) ? 0.3f : 1.01f; Vector3 localPos = new Vector3(xi * vg.w, pointY * vg.h, zi * vg.d); Gizmos.DrawCube(localPos, new Vector3(vg.w * cSize, vg.h * cSize, vg.d * cSize)); } } } }