/// <inheritdoc /> public void BuildMesh(Tile tile, Mesh mesh) { var gameObject = new GameObject(mesh.Name); var uMesh = new UnityEngine.Mesh(); uMesh.vertices = mesh.Vertices; uMesh.triangles = mesh.Triangles; uMesh.colors = mesh.Colors; uMesh.uv = mesh.Uvs; uMesh.uv2 = mesh.Uvs2; uMesh.uv3 = mesh.Uvs3; uMesh.RecalculateNormals(); gameObject.isStatic = true; gameObject.AddComponent <MeshFilter>().mesh = uMesh; // TODO use TextureIndex to select proper material. string texture = tile.QuadKey.LevelOfDetail == 16 ? @"Materials/SurfaceTexturedColored" : @"Materials/SurfaceColored"; gameObject.AddComponent <MeshRenderer>().sharedMaterial = _materialProvider.GetSharedMaterial(texture); gameObject.transform.parent = tile.GameObject.transform; }
/// <inheritdoc /> public void BuildFromMesh(Tile tile, Mesh mesh) { var gameObject = new GameObject(mesh.Name); var uMesh = new UnityEngine.Mesh(); uMesh.vertices = mesh.Vertices; uMesh.triangles = mesh.Triangles; uMesh.colors = mesh.Colors; uMesh.uv = mesh.Uvs; uMesh.uv2 = mesh.Uvs2; uMesh.uv3 = mesh.Uvs3; uMesh.RecalculateNormals(); gameObject.isStatic = true; gameObject.AddComponent <MeshFilter>().mesh = uMesh; gameObject.AddComponent <MeshRenderer>().sharedMaterial = _materialProvider.GetSharedMaterial(mesh.TextureIndex); gameObject.transform.parent = tile.GameObject.transform; }
/// <summary> Builds mesh object and notifies observers. </summary> /// <remarks> Unity has vertex count limit and spliiting meshes here is quite expensive operation. </remarks> private static void BuildMesh(Tile tile, IList <IObserver <MapData> > observers, ITrace trace, string name, Vector3[] worldPoints, int[] triangles, Color[] unityColors, Vector2[] unityUvs, Vector2[] unityUvs2, Vector2[] unityUvs3) { if (worldPoints.Length < VertexLimit) { Mesh mesh = new Mesh(name, 0, worldPoints, triangles, unityColors, unityUvs, unityUvs2, unityUvs3); NotifyObservers(new MapData(tile, new Union <Element, Mesh>(mesh)), observers); return; } trace.Warn(TraceCategory, "Mesh '{0}' has more vertices than allowed by Unity: {1}. Will try to split..", name, worldPoints.Length.ToString()); if (worldPoints.Length != triangles.Length) { // TODO handle this case properly trace.Warn(TraceCategory, "Cannot split mesh {0}: vertecies count != triangles count", name); return; } int parts = (int)Math.Ceiling((float)worldPoints.Length / VertexLimit); for (int i = 0; i < parts; ++i) { var start = i * VertexLimit; var end = Math.Min(start + VertexLimit, worldPoints.Length); Mesh mesh = new Mesh(name + i, 0, worldPoints.Skip(start).Take(end - start).ToArray(), i == 0 ? triangles.Skip(start).Take(end - start).ToArray() : triangles.Skip(start).Take(end - start).Select(tri => tri - start).ToArray(), unityColors.Skip(start).Take(end - start).ToArray(), unityUvs.Skip(start).Take(end - start).ToArray(), unityUvs2.Skip(start).Take(end - start).ToArray(), unityUvs3.Skip(start).Take(end - start).ToArray()); NotifyObservers(new MapData(tile, new Union <Element, Mesh>(mesh)), observers); } }