private void RepaintMesh() { TacticsTerrainMesh terrain = (TacticsTerrainMesh)target; Vector2[] uvArray = new Vector2[terrain.GetComponent <MeshFilter>().sharedMesh.uv.Length]; foreach (Dictionary <Vector3, TerrainQuad> quadDictionary in quads.Values) { foreach (TerrainQuad quad in quadDictionary.Values) { quad.CopyUVs(uvArray); } } terrain.GetComponent <MeshFilter>().sharedMesh.uv = uvArray; uvs = new List <Vector2>(uvArray); }
private void AddEvent(MapEvent3D mapEvent) { TacticsTerrainMesh terrain = (TacticsTerrainMesh)target; Map map = terrain.GetComponent <Map>(); GameObjectUtility.SetParentAndAlign(mapEvent.gameObject, map.objectLayer.gameObject); Undo.RegisterCreatedObjectUndo(mapEvent, "Create " + mapEvent.name); mapEvent.SetLocation(new Vector2Int((int)primarySelection.pos.x, (int)primarySelection.pos.z)); Selection.activeObject = mapEvent.gameObject; }
private void Rebuild(bool regenMesh) { TacticsTerrainMesh terrain = (TacticsTerrainMesh)target; PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(terrain.gameObject); if (prefabStage != null) { EditorSceneManager.MarkSceneDirty(prefabStage.scene); } MeshFilter filter = terrain.GetComponent <MeshFilter>(); Mesh mesh = filter.sharedMesh; if (mesh == null) { mesh = new Mesh(); AssetDatabase.CreateAsset(mesh, "Assets/Resources/TacticsMaps/Meshes/" + terrain.gameObject.name + ".asset"); filter.sharedMesh = mesh; } quads = new Dictionary <Vector3, Dictionary <Vector3, TerrainQuad> >(); vertices = new List <Vector3>(); uvs = new List <Vector2>(); tris = new List <int>(); for (int z = 0; z < terrain.size.y; z += 1) { for (int x = 0; x < terrain.size.x; x += 1) { // top vertices float height = terrain.HeightAt(x, z); AddQuad(new Vector3(x, height, z), new Vector3(x + 1, height, z + 1), terrain.TileAt(x, z), new Vector3(x, height, z), new Vector3(0, 1, 0)); // side vertices foreach (OrthoDir dir in Enum.GetValues(typeof(OrthoDir))) { float currentHeight = terrain.HeightAt(x, z); float neighborHeight = terrain.HeightAt(x + dir.Px3DX(), z + dir.Px3DZ()); if (currentHeight > neighborHeight) { Vector2 off1 = Vector2.zero, off2 = Vector2.zero; switch (dir) { case OrthoDir.South: off1 = new Vector2(0, 0); off2 = new Vector2(1, 0); break; case OrthoDir.East: off1 = new Vector2(1, 1); off2 = new Vector2(1, 0); break; case OrthoDir.North: off1 = new Vector2(1, 1); off2 = new Vector2(0, 1); break; case OrthoDir.West: off1 = new Vector2(0, 0); off2 = new Vector2(0, 1); break; } for (float y = neighborHeight; y < currentHeight; y += 0.5f) { AddQuad(new Vector3(x + off1.x, y, z + off1.y), new Vector3(x + off2.x, y + 0.5f, z + off2.y), terrain.TileAt(x, z, y, dir), new Vector3(x, y + 0.5f, z), dir.Px3D()); } } } } } if (regenMesh) { selectedQuads.Clear(); mesh.Clear(); mesh.vertices = vertices.ToArray(); mesh.triangles = tris.ToArray(); mesh.uv = uvs.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); } }
public void ConfigureNewGrid(Vector2Int at, Vector2Int size, TacticsTerrainMesh terrain, Func <Vector2Int, bool> rangeRule, Func <Vector2Int, bool> selectRule) { transform.position = new Vector3(0.0f, 0.0f, 0.0f); LineOfSightEffect los = terrain.GetComponent <LineOfSightEffect>(); MeshFilter filter = this.mesh; if (filter.mesh != null) { Destroy(filter.mesh); } Mesh mesh = new Mesh(); filter.mesh = mesh; List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); List <int> tris = new List <int>(); for (int y = at.y; y < at.y + size.y; y += 1) { for (int x = at.x; x < at.x + size.x; x += 1) { if (terrain.HeightAt(x, y) == 0.0f || !los.CachedIsVisible(new Vector2Int(x, y))) { continue; } bool selectable = selectRule(new Vector2Int(x, y)); bool rangeable = rangeRule(new Vector2Int(x, y)); if (selectable || rangeable) { int vertIndex = vertices.Count; float height = terrain.HeightAt(x, y); for (int i = 0; i < 4; i += 1) { if (selectable) { uvs.Add(new Vector2( i % 2 == 0 ? .5f : 0, i < 2 ? .5f : 0)); } else if (rangeable) { uvs.Add(new Vector2( i % 2 == 0 ? 1 : .5f, i < 2 ? .5f : 0)); } vertices.Add(new Vector3( x + (i % 2 == 0 ? 1 : 0), height, y + (i < 2 ? 1 : 0))); } tris.Add(vertIndex); tris.Add(vertIndex + 2); tris.Add(vertIndex + 1); tris.Add(vertIndex + 1); tris.Add(vertIndex + 2); tris.Add(vertIndex + 3); } } } mesh.vertices = vertices.ToArray(); mesh.uv = uvs.ToArray(); mesh.triangles = tris.ToArray(); mesh.RecalculateNormals(); }