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

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

新規登録して質問してみよう
ただいま回答率
85.51%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

Q&A

解決済

1回答

544閲覧

Unity2D Graphics.DrawMeshInstanced 複数の画像

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

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

0グッド

0クリップ

投稿2019/04/25 14:35

編集2019/04/25 14:49

前提・実現したいこと

Graphics.DrawMeshInstancedを使い、一つのマテリアルで複数の画像を描画したい。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Linq; 5 6public class World_P : MonoBehaviour 7{ 8 public Tile_P tile_P; 9 public Tile_P tile_P2; 10 11 public Material material; 12 13 Mesh mesh; 14 15 Matrix4x4[][] matrices; 16 int count; 17 18 int rows; 19 int columns; 20 21 public static World_P instance { get; private set; } 22 23 private List<Vector2> posList = new List<Vector2>(); 24 25 void Awake() 26 { 27 instance = this; 28 } 29 30 private void Start() 31 { 32 CreateTiles(); 33 GenerateMesh(); 34 } 35 36 public void CreateTiles() 37 { 38 tile_P = new Tile_P(Tile_P.Type.Tree); 39 tile_P2 = new Tile_P(Tile_P.Type.Grass); 40 for(var i = 0;i < 100; i++) 41 { 42 for(var j = 0;j < 100; j++) 43 { 44 Vector2 pos = new Vector2(i, j); 45 posList.Add(pos); 46 } 47 } 48 } 49 50 public void GenerateMesh() 51 { 52 mesh = new Mesh(); 53 54 MeshData_P meshData = new MeshData_P(tile_P); 55 mesh.vertices = meshData.vertices.ToArray(); 56 mesh.triangles = meshData.triangles.ToArray(); 57 mesh.uv = meshData.UVs.ToArray(); 58 59 mesh.RecalculateNormals(); 60 mesh.RecalculateBounds(); 61 62 rows = 100; 63 columns = 100; 64 65 count = rows * columns; 66 var flatMatrices = new Matrix4x4[count]; 67 68 for (var i = 0; i < posList.Count; i++) 69 { 70 var index = i; 71 72 var position = posList[i]; 73 74 var rotation = Quaternion.identity; 75 76 var scale = Vector3.one; 77 78 flatMatrices[index] = Matrix4x4.TRS(position, rotation, scale); 79 } 80 81 matrices = flatMatrices.Select((m, i) => (m, i / 1023)) 82 .GroupBy(t => t.Item2) 83 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 84 } 85 86 void Update() 87 { 88 foreach (var m in matrices) 89 { 90 Graphics.DrawMeshInstanced(mesh, 0, material, m); 91 } 92 } 93} 94

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MeshData_P 6{ 7 public List<Vector3> vertices; 8 public List<int> triangles; 9 public List<Vector2> UVs; 10 11 public MeshData_P(Tile_P data) 12 { 13 vertices = new List<Vector3>(); 14 triangles = new List<int>(); 15 UVs = new List<Vector2>(); 16 17 CreateSquare(data); 18 } 19 20 void CreateSquare(Tile_P tile_P) 21 { 22 vertices.Add(new Vector3(0,0)); 23 vertices.Add(new Vector3(1,0)); 24 vertices.Add(new Vector3(0,1)); 25 vertices.Add(new Vector3(1,1)); 26 27 triangles.Add(vertices.Count - 1); 28 triangles.Add(vertices.Count - 3); 29 triangles.Add(vertices.Count - 4); 30 31 triangles.Add(vertices.Count - 2); 32 triangles.Add(vertices.Count - 1); 33 triangles.Add(vertices.Count - 4); 34 35 UVs.AddRange(SpriteLoader_P.instance.GetTileUVs(tile_P)); 36 } 37} 38

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class SpriteLoader_P : MonoBehaviour 6{ 7 public static SpriteLoader_P instance; 8 9 Dictionary<string, Vector2[]> tileUVMap; 10 11 void Awake() 12 { 13 instance = this; 14 15 tileUVMap = new Dictionary<string, Vector2[]>(); 16 17 Sprite[] sprites = Resources.LoadAll<Sprite>("Plants"); 18 19 float imageWidth = 0f; 20 float imageHeight = 0f; 21 22 foreach(Sprite s in sprites) 23 { 24 if(s.rect.x + s.rect.width > imageWidth) 25 { 26 imageWidth = s.rect.x + s.rect.width; 27 } 28 29 if(s.rect.y + s.rect.height > imageHeight) 30 { 31 imageHeight = s.rect.y + s.rect.height; 32 } 33 } 34 35 foreach(Sprite s in sprites) 36 { 37 Vector2[] uvs = new Vector2[4]; 38 39 uvs[0] = new Vector2(s.rect.x / imageWidth, s.rect.y / imageHeight); 40 uvs[1] = new Vector2((s.rect.x + s.rect.width) / imageWidth, s.rect.y / imageHeight); 41 uvs[2] = new Vector2(s.rect.x / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 42 uvs[3] = new Vector2((s.rect.x + s.rect.width) / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 43 44 tileUVMap.Add(s.name, uvs); 45 } 46 } 47 48 public Vector2[] GetTileUVs(Tile_P tile_P) 49 { 50 string key = tile_P.type.ToString(); 51 52 if(tileUVMap.ContainsKey(key) == true) 53 { 54 return tileUVMap[key]; 55 } 56 else 57 { 58 Debug.Log("There is no UV map for tile type : " + key); 59 return tileUVMap["Void"]; 60 } 61 } 62} 63

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Tile_P 6{ 7 public enum Type { Tree,Grass,Void } 8 public Type type; 9 10 public Tile_P(Type type) 11 { 12 this.type = type; 13 } 14} 15

試したこと

Multipleな画像をマテリアルに適応し、World.csで指定することで描画出来るように書きましたが、二種類以上の画像を描画することが出来ません。出来るのは一種類のみです。
Graphics.DrawMeshInstancedを一度だけ使用し、一つのマテリアルで複数の画像を描画出来るように試行錯誤しましたが、どうしても出来なかったため質問しました。

使用した画像↓

イメージ説明

回答お願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

GPUインスタンシングを使って描画するとなると、下記のようなやり方はどうでしょうか?

  • World_PmaterialはUnity組み込みのシェーダーではなく独自シェーダーを使ったものにする
  • 個々のインスタンス(つまり各タイル)ごとに持たせる情報として、描くべき絵の左下隅のUV座標を用意する
  • メッシュのUVsは左下隅が(0, 0)になるよう移動しておいて、実際に使うUV座標はシェーダー内で算出する

World_Pを下記のように変更してみました。Tile_PMeshData_PSpriteLoader_Pには変更を加えていません。
CreateTiles内でタイルの種類を選び、GenerateMesh内でタイルの種類に応じたUV座標を取得し、その左下隅座標だけを集めて配列化し、Update内での描画の際に使用することにしました。

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Linq; 5 6public class World_P : MonoBehaviour 7{ 8 public Material material; 9 10 Mesh mesh; 11 12 // matricesに加えてUVオフセット配列も用意する 13 Matrix4x4[][] matrices; 14 Vector4[][] uvOffsets; 15 int count; 16 17 int rows; 18 int columns; 19 20 public static World_P instance { get; private set; } 21 22 // posListに加え、各位置に割り当てられたタイルを格納するListを追加 23 private List<Vector2> posList = new List<Vector2>(); 24 private List<Tile_P> tileList = new List<Tile_P>(); 25 26 // マテリアルへのデータ投入用のMaterialPropertyBlockを追加 27 private MaterialPropertyBlock materialProperties; 28 29 // プロパティアクセス用の識別番号を追加 30 private static readonly int UvOffsetProperty = Shader.PropertyToID("_UvOffset"); 31 32 void Awake() 33 { 34 instance = this; 35 } 36 37 private void Start() 38 { 39 CreateTiles(); 40 GenerateMesh(); 41 } 42 43 public void CreateTiles() 44 { 45 var tileTree = new Tile_P(Tile_P.Type.Tree); 46 var tileGrass = new Tile_P(Tile_P.Type.Grass); 47 var tileVoid = new Tile_P(Tile_P.Type.Void); 48 49 for(var i = 0;i < 100; i++) 50 { 51 for(var j = 0;j < 100; j++) 52 { 53 Vector2 pos = new Vector2(i, j); 54 posList.Add(pos); 55 56 // とりあえずタイルの連番が13の倍数なら木、7の倍数なら草、それ以外ならVoidとしました 57 // 実際には背景の地形に基づいて適切なタイルを選択するべきかと思います 58 var index = (i * 100) + j; 59 if ((index % 13) == 0) 60 { 61 tileList.Add(tileTree); 62 } 63 else if ((index % 7) == 0) 64 { 65 tileList.Add(tileGrass); 66 } 67 else 68 { 69 tileList.Add(tileVoid); 70 } 71 } 72 } 73 } 74 75 public void GenerateMesh() 76 { 77 mesh = new Mesh(); 78 79 MeshData_P meshData = new MeshData_P(new Tile_P(Tile_P.Type.Void)); 80 81 // メッシュのUVは原点に移動させておく 82 var offset = meshData.UVs[0]; 83 meshData.UVs = meshData.UVs.Select(uv => uv - offset).ToList(); 84 85 mesh.vertices = meshData.vertices.ToArray(); 86 mesh.triangles = meshData.triangles.ToArray(); 87 mesh.uv = meshData.UVs.ToArray(); 88 89 mesh.RecalculateNormals(); 90 mesh.RecalculateBounds(); 91 92 rows = 100; 93 columns = 100; 94 95 count = rows * columns; 96 97 // flatMatricesを配列からListに変更 98 var flatMatrices = new List<Matrix4x4>(); 99 100 // UVオフセット格納用のListを追加 101 var flatUvOffsets = new List<Vector4>(); 102 103 // UVオフセットの再計算を省略するためのDictionary 104 var uvOffsetsForTile = new Dictionary<Tile_P.Type, Vector2>(); 105 106 for (var i = 0; i < posList.Count; i++) 107 { 108 // まずタイルを調べ... 109 var tile = tileList[i]; 110 111 // Voidならこの位置にタイルの描画は不要なのでスキップする 112 if (tile.type == Tile_P.Type.Void) 113 { 114 continue; 115 } 116 117 // タイルのタイプに対応するUVオフセットが計算済みならそれを取得 118 if (!uvOffsetsForTile.TryGetValue(tile.type, out var uvOffset)) 119 { 120 // 未計算なら計算してuvOffsetsForTileに追加 121 uvOffset = new MeshData_P(new Tile_P(tile.type)).UVs[0]; 122 uvOffsetsForTile.Add(tile.type, uvOffset); 123 } 124 125 var position = posList[i]; 126 var rotation = Quaternion.identity; 127 var scale = Vector3.one; 128 129 // モデル行列とUVオフセットをそれぞれListに追加 130 flatMatrices.Add(Matrix4x4.TRS(position, rotation, scale)); 131 flatUvOffsets.Add(uvOffset); 132 } 133 134 matrices = flatMatrices.Select((m, i) => (m, i / 1023)) 135 .GroupBy(t => t.Item2) 136 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 137 138 // matrices同様にuvOffsetsも1023個ごとに切り分ける 139 uvOffsets = flatUvOffsets.Select((m, i) => (m, i / 1023)) 140 .GroupBy(t => t.Item2) 141 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 142 } 143 144 void Update() 145 { 146 if (materialProperties == null) 147 { 148 materialProperties = new MaterialPropertyBlock(); 149 } 150 151 var groupCount = matrices.Length; 152 153 for (var i = 0; i < groupCount; i++) 154 { 155 var m = matrices[i]; 156 var o = uvOffsets[i]; 157 158 // プロパティブロックにUVオフセットデータを投入し、DrawMeshInstancedの引数として渡す 159 materialProperties.SetVectorArray(UvOffsetProperty, o); 160 Graphics.DrawMeshInstanced(mesh, 0, material, m, m.Length, materialProperties); 161 } 162 } 163}

materialにセットするマテリアル用のシェーダーとして下記を作成しました。
デフォルトのUnlitシェーダーをベースに半透明描画用の設定を行い、インスタンシング描画用のマクロ類を仕込みました。
vert内でインスタンスごとのUV左下隅座標を取得して、それをメッシュのUVに加算したものを最終的なUVとしています。

ShaderLab

1Shader "Unlit/Plant" 2{ 3 Properties 4 { 5 _MainTex ("Texture", 2D) = "white" {} 6 } 7 SubShader 8 { 9 // アルファチャンネルを持つ草木を背景の上に描画することを想定したため 10 // 半透明描画向けに設定しました 11 Tags { "Queue"="Transparent" "RenderType"="Transparent" } 12 13 Cull Off 14 ZTest Always 15 ZWrite Off 16 Blend SrcAlpha OneMinusSrcAlpha 17 18 Pass 19 { 20 CGPROGRAM 21 #pragma vertex vert 22 #pragma fragment frag 23 #pragma multi_compile_instancing 24 25 #include "UnityCG.cginc" 26 27 struct appdata 28 { 29 float4 vertex : POSITION; 30 float2 uv : TEXCOORD0; 31 UNITY_VERTEX_INPUT_INSTANCE_ID 32 }; 33 34 struct v2f 35 { 36 float2 uv : TEXCOORD0; 37 float4 vertex : SV_POSITION; 38 }; 39 40 sampler2D _MainTex; 41 float4 _MainTex_ST; 42 43 // インスタンスごとのプロパティとして_UvOffsetを定義し 44 // これを変化させることで表示する絵を選択することにする 45 UNITY_INSTANCING_BUFFER_START(Props) 46 UNITY_DEFINE_INSTANCED_PROP(float2, _UvOffset) 47 UNITY_INSTANCING_BUFFER_END(Props) 48 49 v2f vert (appdata v) 50 { 51 v2f o; 52 UNITY_SETUP_INSTANCE_ID(v) 53 o.vertex = UnityObjectToClipPos(v.vertex); 54 55 // v.uvを_UvOffsetだけずらす 56 o.uv = TRANSFORM_TEX(v.uv + UNITY_ACCESS_INSTANCED_PROP(Props, _UvOffset), _MainTex); 57 58 return o; 59 } 60 61 fixed4 frag (v2f i) : SV_Target 62 { 63 return tex2D(_MainTex, i.uv); 64 } 65 ENDCG 66 } 67 } 68}

描画結果は下図のようになりました。背景は以前のご質問の折に作成したノイズ地形を使っています。

結果

とりあえずの実験ということで草と木を適当に並べましたが、実際にはもっとそれらしく配置してやる必要があるでしょう。
背景のノイズマップを取得して高度に応じた植物を選択したり(水面から草や木が生えるのはいまいちですしね...)、追加のパーリンノイズをもとに分布を不均一にしたりと、いろいろ工夫できる余地がありそうです。

投稿2019/04/26 06:50

Bongo

総合スコア10807

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

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

退会済みユーザー

退会済みユーザー

2019/04/27 13:26

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問