private void DrawChunkRectangle(BlockTerrain blockTerrain) { var pos = blockTerrain.gameObject.transform.position; for (int j = 0; j < blockTerrain.depth; j++) { for (int i = 0; i < blockTerrain.width; i++) { Vector3[] verts = new Vector3[] { new Vector3(pos.x + i * 16, pos.y, pos.z + j * 16), new Vector3(pos.x + (i + 1) * 16, pos.y, pos.z + j * 16), new Vector3(pos.x + (i + 1) * 16, pos.y, pos.z + (j + 1) * 16), new Vector3(pos.x + i * 16, pos.y, pos.z + (j + 1) * 16), }; if ((i % 2 == 0 && j % 2 != 0) || (i % 2 != 0 && j % 2 == 0)) { Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.1f), new Color(1f, 1f, 1f, 0.2f)); } else { Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.01f), new Color(1f, 1f, 1f, 0.2f)); } // Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.1f), new Color(0f, 1f, 0f, 1f)); } } }
private void OnEnable() { _terrain = (BlockTerrain)target; if (_terrain != null) { _terrainData = _terrain.data; } }
private void DrawMapCube(BlockTerrain blockTerrain) { var pos = blockTerrain.gameObject.transform.position; var center = new Vector3(); center.x = pos.x + blockTerrain.width / 2 * 16; center.y = pos.y; center.z = pos.z + blockTerrain.depth / 2 * 16; Handles.DrawWireCube(center, new Vector3(blockTerrain.width * 16, 1, blockTerrain.depth * 16)); }
private Bounds GetMapBounds(BlockTerrain blockTerrain) { var pos = blockTerrain.gameObject.transform.position; var center = new Vector3(pos.x + blockTerrain.width / 2 * 16, 0.5f, pos.z + blockTerrain.depth / 2 * 16); var size = new Vector3(blockTerrain.width * 16, 1, blockTerrain.depth * 16); Bounds b = new Bounds(center, size); return(b); }
private void DrawMapRectangle(BlockTerrain blockTerrain) { var pos = blockTerrain.gameObject.transform.position; Vector3[] verts = new Vector3[] { new Vector3(pos.x, pos.y, pos.z), new Vector3(pos.x + blockTerrain.width * 16, pos.y, pos.z), new Vector3(pos.x + blockTerrain.width * 16, pos.y, pos.z + blockTerrain.depth * 16), new Vector3(pos.x, pos.y, pos.z + blockTerrain.depth * 16), }; Handles.DrawSolidRectangleWithOutline(verts, new Color(1f, 1f, 1f, 0.1f), new Color(0f, 1f, 0f, 1f)); }
/// <summary> /// 异步创建chunk的网格 /// </summary> /// <returns></returns> public IEnumerator CreateChunkMeshAsyn() { while (isWorking) { yield return(null); } isWorking = true; //获取图块地形组件 if (terrain == null) { this.terrain = GetComponentInParent <BlockTerrain>(); } //初始化截面数组 sections = new Section[sectionCount]; //初始化网格 mesh = new Mesh(); //设置索引格式,支持32位三角形索引 mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; //设置网格名字 mesh.name = "chunk"; for (int i = 0; i < sections.Length; i++) { sections[i] = new Section(i, terrain); sections[i].SetBlocks(this.data.sectionData[i]); yield return(StartCoroutine(sections[i].CreateSectionMeshAsyn())); } CombineInstance[] instances = new CombineInstance[sections.Length]; for (int i = 0; i < sections.Length; i++) { instances[i] = new CombineInstance(); instances[i].mesh = sections[i].mesh; Vector3 pos = Vector3.zero; pos.y += i * 16 + 8; Quaternion rot = Quaternion.identity; Vector3 scale = new Vector3(1, 1, 1); instances[i].transform = Matrix4x4.TRS(pos, rot, scale); } //组合mesh mesh.CombineMeshes(instances); mesh.RecalculateNormals(); mesh.bounds = new Bounds(Vector3.zero, new Vector3(16, 256, 16)); this.GetComponent <MeshFilter>().mesh = mesh; this.GetComponent <MeshCollider>().sharedMesh = mesh; isWorking = false; }
public override void OnInspectorGUI() { // base.OnInspectorGUI(); BlockTerrain blockTerrain = (BlockTerrain)target; var data = blockTerrain.data; data = EditorGUILayout.ObjectField("Data", data, data.GetType()) as BlockTerrainData; data.name = EditorGUILayout.TextField("Name", data.name); EditorGUILayout.TextField("Version", data.version); data.width = EditorGUILayout.IntField("Wdith(X)", data.width); data.height = EditorGUILayout.IntField("Height(Y)", data.height); data.depth = EditorGUILayout.IntField("Depth(Z)", data.depth); GUILayout.BeginHorizontal(); if (GUILayout.Button("创建地形", (GUIStyle)"ButtonLeft")) { //创建map的chunks MyTools.DeleteAllChildren(blockTerrain.transform); InitTerrainData(); EditorCoroutineUtility.StartCoroutine(blockTerrain.LoadTerrainAsyn(), this); } if (GUILayout.Button("清空Mesh缓存", (GUIStyle)"ButtonMid")) { //创建map的chunks MyTools.DeleteAllChildren(blockTerrain.transform); } if (GUILayout.Button("更新Mesh", (GUIStyle)"ButtonRight")) { //blockTerrain.CreateRandomMap(); } GUILayout.EndHorizontal(); selectPanelIndex = GUILayout.Toolbar(selectPanelIndex, panelNames); if (panelNames[selectPanelIndex] == "笔刷") { this.DrawBrushesPanel(data); } else if (panelNames[selectPanelIndex] == "图块") { this.DrawBlocksPanel(data); } else if (panelNames[selectPanelIndex] == "图层") { this.DrawLayersPanel(data); } else if (panelNames[selectPanelIndex] == "设置") { } }
/// <summary> /// 为每个chunk绘制一个白色的线框 /// </summary> /// <param name="blockTerrain"></param> private void DrawChunkCube(BlockTerrain blockTerrain) { var pos = blockTerrain.gameObject.transform.position; for (int i = 0; i < blockTerrain.width; i++) { for (int j = 0; j < blockTerrain.depth; j++) { var center = new Vector3(); center.x = pos.x + i * 16 + 8; center.y = pos.y; center.z = pos.z + j * 16 + 8; Handles.DrawWireCube(center, new Vector3(16, 16, 16)); } } }
private List <ChunkBounds> CreateChunkBounds(BlockTerrain blockTerrain) { var chunksBounds = new List <ChunkBounds>(); var pos = blockTerrain.gameObject.transform.position; for (int j = 0; j < blockTerrain.depth; j++) { for (int i = 0; i < blockTerrain.width; i++) { Bounds b = new Bounds(); b.center = new Vector3(pos.x + i * 16 + 8, pos.y + 0.5f, pos.z + j * 16 + 8); //一层的高度,一个chunk的宽度和深度 b.size = new Vector3(16, 1, 16); chunksBounds.Add(new ChunkBounds(i, j, b)); } } return(chunksBounds); }
/// <summary> /// 构造函数 /// </summary> /// <param name="setId">标识</param> /// <param name="setTerrain">地形</param> public Section(int setId, BlockTerrain setTerrain) { this.id = setId; this.terrain = setTerrain; }