Unity Graphics.DrawMesh 処理が重い
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 647

退会済みユーザー
前提・実現したいこと
Graphics.DrawMeshの処理が重い問題を解決したい
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
List<Vector3> posList = new List<Vector3>();
List<Vector3> posList2 = new List<Vector3>();
List<Vector3> posList3 = new List<Vector3>();
public Material material;
private void Start()
{
for(int i = 0;i < 5; i++)
{
for(int j = 0;j < 5; j++)
{
Vector3 pos = new Vector3(i, j, 0);
posList.Add(pos);
}
}
for(int i = 0;i < 5; i++)
{
for(int j = 5;j < 10; j++)
{
Vector3 pos = new Vector3(i, j, 0);
posList2.Add(pos);
}
}
for (int i = 5; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
Vector3 pos = new Vector3(i, j, 0);
posList3.Add(pos);
}
}
}
public static Mesh NewPlaneMesh()
{
Vector3[] array = new Vector3[4];
Vector2[] array2 = new Vector2[4];
int[] array3 = new int[6];
array[0] = new Vector3(0, 0, 0);
array[1] = new Vector3(0, 1, 0);
array[2] = new Vector3(1, 0, 0);
array[3] = new Vector3(1, 1, 0);
array2[0] = new Vector2(0, 0);
array2[1] = new Vector2(0, 1);
array2[2] = new Vector2(1, 0);
array2[3] = new Vector2(1, 1);
array3[0] = 0;
array3[1] = 1;
array3[2] = 2;
array3[3] = 1;
array3[4] = 3;
array3[5] = 2;
Mesh mesh = new Mesh();
mesh.name = "NewPlaneMesh()";
mesh.vertices = array;
mesh.uv = array2;
mesh.SetTriangles(array3, 0);
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
private void Update()
{
for(int i = 0;i < posList.Count; i++)
{
if (CheckScreenOut(posList[i]) == false)
{
Graphics.DrawMesh(NewPlaneMesh(), posList[i], Quaternion.identity, material, 0);
}
}
for (int i = 0; i < posList2.Count; i++)
{
if (CheckScreenOut(posList2[i]) == false)
{
Graphics.DrawMesh(NewPlaneMesh(), posList2[i], Quaternion.identity, material, 0);
}
}
for (int i = 0; i < posList3.Count; i++)
{
if (CheckScreenOut(posList3[i]) == false)
{
Graphics.DrawMesh(NewPlaneMesh(), posList3[i], Quaternion.identity, material, 0);
}
}
if (Input.GetKey("up"))
{
Vector3 pos = transform.position;
pos.y += 0.5f;
transform.position = pos;
}
if (Input.GetKey("down"))
{
Vector3 pos = transform.position;
pos.y -= 0.5f;
transform.position = pos;
}
if (Input.GetKey("right"))
{
Vector3 pos = transform.position;
pos.x += 0.5f;
transform.position = pos;
}
if (Input.GetKey("left"))
{
Vector3 pos = transform.position;
pos.x -= 0.5f;
transform.position = pos;
}
}
bool CheckScreenOut(Vector3 pos)
{
Vector3 viewPos = Camera.main.WorldToViewportPoint(pos);
if(viewPos.x < -0.1f ||
viewPos.x > 1.1f ||
viewPos.y < -0.1f ||
viewPos.y > 1.1f)
{
//範囲外
return true;
}
//範囲内
return false;
}
}
試したこと
カメラの範囲内に入ったメッシュだけをGraphics.DrawMeshするように書いたのですが、FPSが一桁台にまで頻繁に落ちます。本当は100×100のメッシュを描画したいのですが、分割して描画しても重いので、到底出来そうもないです。
どのようにすればFPSを保って描画出来るのでしょうか? 教えてもらえるとありがたいです。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
Mesh
の新規生成は比較的高コストな部類の処理でしょうから、毎フレーム何度も行うのはいかがなものかと思います...
NewPlaneMesh
が作るメッシュは常に同じ形のようですから、一回作れば十分ではないでしょうか?
たとえば下記のようにメッシュを再利用してみてはいかがでしょう。
static Mesh planeMesh;
static Mesh PlaneMesh
{
get
{
// planeMeshが未生成ならNewPlaneMeshで生成し...
if (planeMesh == null)
{
planeMesh = NewPlaneMesh();
}
// 以降はいちいちNewPlaneMeshを再実行せず、生成済みのメッシュを再利用する
return planeMesh;
}
}
private void Update()
{
for(int i = 0;i < this.posList.Count; i++)
{
if (this.CheckScreenOut(this.posList[i]) == false)
{
// NewPlaneMeshの代わりにPlaneMeshを使う
Graphics.DrawMesh(PlaneMesh, this.posList[i], Quaternion.identity, this.material, 0);
}
}
for (int i = 0; i < this.posList2.Count; i++)
{
if (this.CheckScreenOut(this.posList2[i]) == false)
{
// NewPlaneMeshの代わりにPlaneMeshを使う
Graphics.DrawMesh(PlaneMesh, this.posList2[i], Quaternion.identity, this.material, 0);
}
}
for (int i = 0; i < this.posList3.Count; i++)
{
if (this.CheckScreenOut(this.posList3[i]) == false)
{
// NewPlaneMeshの代わりにPlaneMeshを使う
Graphics.DrawMesh(PlaneMesh, this.posList3[i], Quaternion.identity, this.material, 0);
}
}
if (Input.GetKey("up"))
{
Vector3 pos = this.transform.position;
pos.y += 0.5f;
this.transform.position = pos;
}
if (Input.GetKey("down"))
{
Vector3 pos = this.transform.position;
pos.y -= 0.5f;
this.transform.position = pos;
}
if (Input.GetKey("right"))
{
Vector3 pos = this.transform.position;
pos.x += 0.5f;
this.transform.position = pos;
}
if (Input.GetKey("left"))
{
Vector3 pos = this.transform.position;
pos.x -= 0.5f;
this.transform.position = pos;
}
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.22%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2019/11/16 22:58