前提・実現したいこと
Graphics.DrawMeshを使って生成しているメッシュを結合して、FPSを改善させたい。
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ExampleWorld : MonoBehaviour 6{ 7 public int width; 8 public int height; 9 10 public ExampleTile[,] tiles; 11 12 public Material material; 13 14 Mesh mesh; 15 16 // Use this for initialization 17 void Start() 18 { 19 CreateTiles(); 20 GenerateMesh(); 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0); 27 } 28 29 void CreateTiles() 30 { 31 tiles = new ExampleTile[width, height]; 32 33 for (int i = 0; i < width; i++) 34 { 35 for (int j = 0; j < height; j++) 36 { 37 tiles[i, j] = new ExampleTile(ExampleTile.Type.Stone); 38 } 39 } 40 } 41 42 void GenerateMesh() 43 { 44 ExampleMeshData data = new ExampleMeshData(tiles); 45 mesh = new Mesh(); 46 mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; 47 mesh.vertices = data.vertices.ToArray(); 48 mesh.triangles = data.triangles.ToArray(); 49 mesh.RecalculateNormals(); 50 mesh.RecalculateBounds(); 51 } 52} 53
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ExampleMeshData 6{ 7 public List<Vector3> vertices; 8 public List<int> triangles; 9 10 public ExampleMeshData(ExampleTile[,] data) 11 { 12 vertices = new List<Vector3>(); 13 triangles = new List<int>(); 14 15 for (int i = 0; i < data.GetLength(0); i++) 16 { 17 for (int j = 0; j < data.GetLength(1); j++) 18 { 19 CreateSquare(data[i, j], i, j); 20 } 21 } 22 } 23 24 void CreateSquare(ExampleTile tile, int x, int y) 25 { 26 vertices.Add(new Vector3(x + 0, y + 0)); 27 vertices.Add(new Vector3(x + 1, y + 0)); 28 vertices.Add(new Vector3(x + 0, y + 1)); 29 vertices.Add(new Vector3(x + 1, y + 1)); 30 31 triangles.Add(vertices.Count - 1); 32 triangles.Add(vertices.Count - 3); 33 triangles.Add(vertices.Count - 4); 34 35 triangles.Add(vertices.Count - 2); 36 triangles.Add(vertices.Count - 1); 37 triangles.Add(vertices.Count - 4); 38 } 39 40} 41
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ExampleTile { 6 public enum Type { Stone } 7 public Type type; 8 9 public ExampleTile(Type type) 10 { 11 this.type = type; 12 } 13} 14
試したこと
Graphics.DrawMeshを使って動的に生成しているメッシュを結合して一つにする方法を調べたのですが、出て来なかったので質問しました。
プロジェクトで使っているコードはもっと大量にメッシュを生成しているため、SetPass Callは2なのですが、saved by batchingが約30000以上あります。そのため、30~40FPS程度しか出ないので、動かすとカクカクしている状態です。
300×300のマップをそのコードで生成しようとすると、出力するのに10分以上かかってしまっています。
どのようにすればFPSを上げることが出来るのでしょうか? メッシュを結合すればいいのではないか、と自分は考えました。載せているコードは、最小限のものです。
回答お願いします。
実際のコードで生成した場合↓
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/07/07 06:09