private void UpdateMesh() { OnlineMapsTileSetControl control = OnlineMapsTileSetControl.instance; if (tilesetCollider == null) { tilesetCollider = control.GetComponent <Collider>(); } Bounds bounds = tilesetCollider.bounds; // Clear overlay mesh overlayMesh.Clear(true); // Init verticles and normals float y = bounds.max.y + 0.5f; overlayMesh.vertices = new[] { new Vector3(bounds.min.x, y, bounds.min.z), new Vector3(bounds.min.x, y, bounds.max.z), new Vector3(bounds.max.x, y, bounds.max.z), new Vector3(bounds.max.x, y, bounds.min.z) }; overlayMesh.normals = new[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up }; // Init overlay UV OnlineMaps api = OnlineMaps.instance; Vector2 topLeftPosition = api.topLeftPosition; Vector2 bottomRightPosition = api.bottomRightPosition; Vector2 topLeftTile = OnlineMapsUtils.LatLongToTilef(topLeftPosition, api.zoom); Vector2 bottomRightTile = OnlineMapsUtils.LatLongToTilef(bottomRightPosition, api.zoom); int maxTileCount = 1 << api.zoom; float uvX1 = topLeftTile.x / maxTileCount; float uvX2 = bottomRightTile.x / maxTileCount; if (uvX1 > uvX2) { uvX2 += 1; } float uvY1 = 1 - topLeftTile.y / maxTileCount; float uvY2 = 1 - bottomRightTile.y / maxTileCount; overlayMesh.uv = new[] { new Vector2(uvX2, uvY1), new Vector2(uvX2, uvY2), new Vector2(uvX1, uvY2), new Vector2(uvX1, uvY1) }; // Init triangles overlayMesh.SetTriangles(new[] { 0, 1, 2, 0, 2, 3 }, 0); overlayMesh.RecalculateBounds(); overlayMesh.RecalculateNormals(); }
private void UpdateMesh() { OnlineMapsTileSetControl control = OnlineMapsTileSetControl.instance; if (tilesetCollider == null) { tilesetCollider = control.GetComponent <Collider>(); } Bounds bounds = tilesetCollider.bounds; // Clear overlay mesh overlayMesh.Clear(true); // Init verticles and normals float y = bounds.max.y + 0.5f; overlayMesh.vertices = new[] { new Vector3(bounds.min.x, y, bounds.min.z), new Vector3(bounds.min.x, y, bounds.max.z), new Vector3(bounds.max.x, y, bounds.max.z), new Vector3(bounds.max.x, y, bounds.min.z) }; overlayMesh.normals = new[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up }; // Init overlay UV OnlineMaps map = OnlineMaps.instance; double tlx, tly, brx, bry; map.GetTileCorners(out tlx, out tly, out brx, out bry); int maxTileCount = 1 << map.zoom; float uvX1 = (float)(tlx / maxTileCount); float uvX2 = (float)(brx / maxTileCount); if (uvX1 > uvX2) { uvX2 += 1; } float uvY1 = (float)(1 - tly / maxTileCount); float uvY2 = (float)(1 - bry / maxTileCount); overlayMesh.uv = new[] { new Vector2(uvX2, uvY1), new Vector2(uvX2, uvY2), new Vector2(uvX1, uvY2), new Vector2(uvX1, uvY1) }; // Init triangles overlayMesh.SetTriangles(new[] { 0, 1, 2, 0, 2, 3 }, 0); overlayMesh.RecalculateBounds(); overlayMesh.RecalculateNormals(); }
/// <summary> /// This method is called each time the map mesh is updated /// </summary> private void UpdateSides() { // Clear baseboard mesh mesh.Clear(); if (mapMesh == null) { mapMesh = control.GetComponent <MeshFilter>().sharedMesh; } // Initialize arrays for vertices Vector3[] mapVertices = mapMesh.vertices; List <Vector3> left = new List <Vector3>(); List <Vector3> right = new List <Vector3>(); List <Vector3> top = new List <Vector3>(); List <Vector3> bottom = new List <Vector3>(); float minY = float.MaxValue; // Iterate over all map points to find border points for (int i = 0; i < mapVertices.Length; i++) { Vector3 v = mapVertices[i]; if (Mathf.Abs(v.x) <= 0.01f) { left.Add(v); } if (Mathf.Abs(v.x + control.sizeInScene.x) <= 0.01f) { right.Add(v); } if (Mathf.Abs(v.z) <= 0.01f) { top.Add(v); } if (Mathf.Abs(v.z - control.sizeInScene.y) <= 0.01f) { bottom.Add(v); } if (minY > v.y) { minY = v.y; } } // Remove duplicates and sort points of sides left = left.Distinct().OrderBy(v => v.z).ToList(); right = right.Distinct().OrderBy(v => v.z).Reverse().ToList(); top = top.Distinct().OrderBy(v => v.x).ToList(); bottom = bottom.Distinct().OrderBy(v => v.x).Reverse().ToList(); minY -= minHeight; // Initialize lists of vertices, uv, normals and trinagles List <Vector3> vertices = new List <Vector3>(); List <Vector2> uv = new List <Vector2>(); List <Vector3> normals = new List <Vector3>(); List <int> triangles = new List <int>(); // Update each side UpdateSide(left, vertices, uv, normals, triangles, Vector3.left, minY); UpdateSide(top, vertices, uv, normals, triangles, Vector3.forward, minY); UpdateSide(right, vertices, uv, normals, triangles, Vector3.right, minY); UpdateSide(bottom, vertices, uv, normals, triangles, Vector3.back, minY); int countVertices = vertices.Count; // Close bottom if necessary if (closeBottom) { vertices.Add(new Vector3(0, minY, 0)); vertices.Add(new Vector3(0, minY, control.sizeInScene.y)); vertices.Add(new Vector3(-control.sizeInScene.x, minY, control.sizeInScene.y)); vertices.Add(new Vector3(-control.sizeInScene.x, minY, 0)); uv.Add(new Vector2(0, 0)); uv.Add(new Vector2(0, 1)); uv.Add(new Vector2(1, 1)); uv.Add(new Vector2(1, 0)); normals.Add(Vector3.down); normals.Add(Vector3.down); normals.Add(Vector3.down); normals.Add(Vector3.down); mesh.subMeshCount = 2; } // Set vertices, uv, normals and triangles to the baseboard mesh mesh.vertices = vertices.ToArray(); mesh.uv = uv.ToArray(); mesh.normals = normals.ToArray(); mesh.SetTriangles(triangles.ToArray(), 0); if (closeBottom) { mesh.SetTriangles(new[] { countVertices, countVertices + 1, countVertices + 2, countVertices, countVertices + 2, countVertices + 3 }, 1); } }