質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2912閲覧

Unity Graphics.DrawMesh 処理が重い

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2019/11/15 13:25

前提・実現したいこと

Graphics.DrawMeshの処理が重い問題を解決したい

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ExampleClass : MonoBehaviour 6{ 7 List<Vector3> posList = new List<Vector3>(); 8 List<Vector3> posList2 = new List<Vector3>(); 9 List<Vector3> posList3 = new List<Vector3>(); 10 public Material material; 11 private void Start() 12 { 13 for(int i = 0;i < 5; i++) 14 { 15 for(int j = 0;j < 5; j++) 16 { 17 Vector3 pos = new Vector3(i, j, 0); 18 posList.Add(pos); 19 } 20 } 21 for(int i = 0;i < 5; i++) 22 { 23 for(int j = 5;j < 10; j++) 24 { 25 Vector3 pos = new Vector3(i, j, 0); 26 posList2.Add(pos); 27 } 28 } 29 for (int i = 5; i < 10; i++) 30 { 31 for (int j = 0; j < 5; j++) 32 { 33 Vector3 pos = new Vector3(i, j, 0); 34 posList3.Add(pos); 35 } 36 } 37 } 38 public static Mesh NewPlaneMesh() 39 { 40 Vector3[] array = new Vector3[4]; 41 Vector2[] array2 = new Vector2[4]; 42 int[] array3 = new int[6]; 43 44 array[0] = new Vector3(0, 0, 0); 45 array[1] = new Vector3(0, 1, 0); 46 array[2] = new Vector3(1, 0, 0); 47 array[3] = new Vector3(1, 1, 0); 48 49 array2[0] = new Vector2(0, 0); 50 array2[1] = new Vector2(0, 1); 51 array2[2] = new Vector2(1, 0); 52 array2[3] = new Vector2(1, 1); 53 54 array3[0] = 0; 55 array3[1] = 1; 56 array3[2] = 2; 57 array3[3] = 1; 58 array3[4] = 3; 59 array3[5] = 2; 60 61 Mesh mesh = new Mesh(); 62 mesh.name = "NewPlaneMesh()"; 63 mesh.vertices = array; 64 mesh.uv = array2; 65 mesh.SetTriangles(array3, 0); 66 mesh.RecalculateNormals(); 67 mesh.RecalculateBounds(); 68 return mesh; 69 } 70 private void Update() 71 { 72 for(int i = 0;i < posList.Count; i++) 73 { 74 if (CheckScreenOut(posList[i]) == false) 75 { 76 Graphics.DrawMesh(NewPlaneMesh(), posList[i], Quaternion.identity, material, 0); 77 } 78 } 79 for (int i = 0; i < posList2.Count; i++) 80 { 81 if (CheckScreenOut(posList2[i]) == false) 82 { 83 Graphics.DrawMesh(NewPlaneMesh(), posList2[i], Quaternion.identity, material, 0); 84 } 85 } 86 for (int i = 0; i < posList3.Count; i++) 87 { 88 if (CheckScreenOut(posList3[i]) == false) 89 { 90 Graphics.DrawMesh(NewPlaneMesh(), posList3[i], Quaternion.identity, material, 0); 91 } 92 } 93 if (Input.GetKey("up")) 94 { 95 Vector3 pos = transform.position; 96 pos.y += 0.5f; 97 transform.position = pos; 98 } 99 if (Input.GetKey("down")) 100 { 101 Vector3 pos = transform.position; 102 pos.y -= 0.5f; 103 transform.position = pos; 104 } 105 if (Input.GetKey("right")) 106 { 107 Vector3 pos = transform.position; 108 pos.x += 0.5f; 109 transform.position = pos; 110 } 111 if (Input.GetKey("left")) 112 { 113 Vector3 pos = transform.position; 114 pos.x -= 0.5f; 115 transform.position = pos; 116 } 117 } 118 bool CheckScreenOut(Vector3 pos) 119 { 120 Vector3 viewPos = Camera.main.WorldToViewportPoint(pos); 121 if(viewPos.x < -0.1f || 122 viewPos.x > 1.1f || 123 viewPos.y < -0.1f || 124 viewPos.y > 1.1f) 125 { 126 //範囲外 127 return true; 128 } 129 //範囲内 130 return false; 131 } 132} 133

試したこと

カメラの範囲内に入ったメッシュだけをGraphics.DrawMeshするように書いたのですが、FPSが一桁台にまで頻繁に落ちます。本当は100×100のメッシュを描画したいのですが、分割して描画しても重いので、到底出来そうもないです。
どのようにすればFPSを保って描画出来るのでしょうか? 教えてもらえるとありがたいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Meshの新規生成は比較的高コストな部類の処理でしょうから、毎フレーム何度も行うのはいかがなものかと思います...
NewPlaneMeshが作るメッシュは常に同じ形のようですから、一回作れば十分ではないでしょうか?
たとえば下記のようにメッシュを再利用してみてはいかがでしょう。

C#

1 static Mesh planeMesh; 2 3 static Mesh PlaneMesh 4 { 5 get 6 { 7 // planeMeshが未生成ならNewPlaneMeshで生成し... 8 if (planeMesh == null) 9 { 10 planeMesh = NewPlaneMesh(); 11 } 12 13 // 以降はいちいちNewPlaneMeshを再実行せず、生成済みのメッシュを再利用する 14 return planeMesh; 15 } 16 } 17 18 private void Update() 19 { 20 for(int i = 0;i < this.posList.Count; i++) 21 { 22 if (this.CheckScreenOut(this.posList[i]) == false) 23 { 24 // NewPlaneMeshの代わりにPlaneMeshを使う 25 Graphics.DrawMesh(PlaneMesh, this.posList[i], Quaternion.identity, this.material, 0); 26 } 27 } 28 for (int i = 0; i < this.posList2.Count; i++) 29 { 30 if (this.CheckScreenOut(this.posList2[i]) == false) 31 { 32 // NewPlaneMeshの代わりにPlaneMeshを使う 33 Graphics.DrawMesh(PlaneMesh, this.posList2[i], Quaternion.identity, this.material, 0); 34 } 35 } 36 for (int i = 0; i < this.posList3.Count; i++) 37 { 38 if (this.CheckScreenOut(this.posList3[i]) == false) 39 { 40 // NewPlaneMeshの代わりにPlaneMeshを使う 41 Graphics.DrawMesh(PlaneMesh, this.posList3[i], Quaternion.identity, this.material, 0); 42 } 43 } 44 if (Input.GetKey("up")) 45 { 46 Vector3 pos = this.transform.position; 47 pos.y += 0.5f; 48 this.transform.position = pos; 49 } 50 if (Input.GetKey("down")) 51 { 52 Vector3 pos = this.transform.position; 53 pos.y -= 0.5f; 54 this.transform.position = pos; 55 } 56 if (Input.GetKey("right")) 57 { 58 Vector3 pos = this.transform.position; 59 pos.x += 0.5f; 60 this.transform.position = pos; 61 } 62 if (Input.GetKey("left")) 63 { 64 Vector3 pos = this.transform.position; 65 pos.x -= 0.5f; 66 this.transform.position = pos; 67 } 68 }

投稿2019/11/15 15:18

Bongo

総合スコア10807

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/11/16 13:58

回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問