質問編集履歴

1

追記

2020/08/16 02:53

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,14 @@
32
32
 
33
33
 
34
34
 
35
+ public Material material;
36
+
37
+
38
+
39
+ HashSet<Matrix4x4> matriHash = new HashSet<Matrix4x4>();
40
+
41
+
42
+
35
43
  private void Start()
36
44
 
37
45
  {
@@ -84,6 +92,8 @@
84
92
 
85
93
  flatMatrix[index] = Matrix4x4.TRS(pos, rotation, scale);
86
94
 
95
+ matriHash.Add(flatMatrix[index]);
96
+
87
97
  }
88
98
 
89
99
  }
@@ -92,7 +102,155 @@
92
102
 
93
103
 
94
104
 
105
+ foreach (Matrix4x4 hash in matriHash)
106
+
107
+ {
108
+
109
+ var position = new Vector3(hash[0, 3], hash[1, 3], hash[2, 3]);
110
+
111
+ Vector2 pos = new Vector2(position.x, position.y);
112
+
113
+ posHash.Add(pos);
114
+
115
+ }
116
+
117
+ }
118
+
119
+
120
+
121
+ Vector2 getScreenDownLeft()
122
+
123
+ {
124
+
125
+ Vector2 pos = new Vector2(0, 0);
126
+
127
+ Vector2 downLeft = Camera.main.ScreenToWorldPoint(pos);
128
+
129
+ return downLeft;
130
+
131
+ }
132
+
133
+
134
+
135
+ Vector2 getScreenUpRight()
136
+
137
+ {
138
+
139
+ Vector2 pos = new Vector2(Screen.width, Screen.height);
140
+
141
+ Vector2 upRight = Camera.main.ScreenToWorldPoint(pos);
142
+
143
+ return upRight;
144
+
145
+ }
146
+
147
+
148
+
149
+ HashSet<Vector2> getScreenPos()
150
+
151
+ {
152
+
153
+ HashSet<Vector2> hash = new HashSet<Vector2>();
154
+
155
+
156
+
157
+ for (int x = (int)getScreenDownLeft().x - 5; x < (int)getScreenUpRight().x + 5; x++)
158
+
159
+ {
160
+
161
+ for (int y = (int)getScreenDownLeft().y - 5; y < (int)getScreenUpRight().y + 5; y++)
162
+
163
+ {
164
+
165
+ Vector2 pos = new Vector2(x, y);
166
+
167
+ hash.Add(pos);
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+ return hash;
176
+
177
+ }
178
+
179
+
180
+
181
+ Matrix4x4[] array = new Matrix4x4[1023];
182
+
183
+
184
+
185
+ HashSet<Matrix4x4> hashMat = new HashSet<Matrix4x4>();
186
+
187
+
188
+
189
+ HashSet<Vector2> posHash = new HashSet<Vector2>();
190
+
191
+
192
+
193
+ private void Update()
194
+
195
+ {
196
+
197
+ foreach (Vector2 item in getScreenPos())
198
+
199
+ {
200
+
201
+ GetMatrix(item);
202
+
203
+ }
204
+
205
+
206
+
207
+ foreach (var m in matrix4X4s)
208
+
209
+ {
210
+
211
+ Graphics.DrawMeshInstanced(MeshGenerator.basicMesh, 0, material, m);
212
+
213
+ }
214
+
215
+ }
216
+
217
+
218
+
219
+ Matrix4x4[][] GetMatrix(Vector2 pos)
220
+
221
+ {
222
+
223
+ if (posHash.Contains(pos))
224
+
225
+ {
226
+
227
+ foreach(Matrix4x4 item in matriHash)
228
+
229
+ {
230
+
231
+ var position = new Vector2(item[0, 3], item[1, 3]);
232
+
233
+ Vector2 matriPos = new Vector2(position.x, position.y);
234
+
235
+ if(pos == matriPos)
236
+
237
+ {
238
+
239
+ hashMat.Add(item);
240
+
241
+ }
242
+
243
+ }
244
+
245
+ }
246
+
247
+
248
+
249
+ array = hashMat.ToArray();
250
+
251
+
252
+
95
- matrix4X4s = flatMatrix.Select((m, i) => (m, i / 1023))
253
+ matrix4X4s = array.Select((m, i) => (m, i / 1023))
96
254
 
97
255
  .GroupBy(t => t.Item2)
98
256
 
@@ -100,83 +258,7 @@
100
258
 
101
259
 
102
260
 
103
- Debug.Log(matrix4X4s.Length);
104
-
105
- }
106
-
107
-
108
-
109
- Vector2 getScreenDownLeft()
110
-
111
- {
112
-
113
- Vector2 pos = new Vector2(0, 0);
114
-
115
- Vector2 downLeft = Camera.main.ScreenToWorldPoint(pos);
116
-
117
- return downLeft;
118
-
119
- }
120
-
121
-
122
-
123
- Vector2 getScreenUpRight()
124
-
125
- {
126
-
127
- Vector2 pos = new Vector2(Screen.width, Screen.height);
128
-
129
- Vector2 upRight = Camera.main.ScreenToWorldPoint(pos);
130
-
131
- return upRight;
261
+ return matrix4X4s;
132
-
133
- }
134
-
135
-
136
-
137
- HashSet<Vector2> getScreenPos()
138
-
139
- {
140
-
141
- HashSet<Vector2> hash = new HashSet<Vector2>();
142
-
143
-
144
-
145
- for (int x = (int)getScreenDownLeft().x - 5; x < (int)getScreenUpRight().x + 5; x++)
146
-
147
- {
148
-
149
- for (int y = (int)getScreenDownLeft().y - 5; y < (int)getScreenUpRight().y + 5; y++)
150
-
151
- {
152
-
153
- Vector2 pos = new Vector2(x, y);
154
-
155
- hash.Add(pos);
156
-
157
- }
158
-
159
- }
160
-
161
-
162
-
163
- return hash;
164
-
165
- }
166
-
167
-
168
-
169
- private void Update()
170
-
171
- {
172
-
173
- foreach (Vector2 item in getScreenPos())
174
-
175
- {
176
-
177
-
178
-
179
- }
180
262
 
181
263
  }
182
264
 
@@ -349,3 +431,15 @@
349
431
 
350
432
 
351
433
  カメラに映っているメッシュだけをGraphics.DrawMeshInstancedを使って描画したかったのですが、カメラに映っているワールド座標と、変換行列に代入されている座標を一致しているか確認する方法が、分からなかったため、質問しました。
434
+
435
+
436
+
437
+ 【追記】
438
+
439
+
440
+
441
+ 回答者様の回答をもとに、スクリプトを変更しました。
442
+
443
+ ですが、カメラに映っているメッシュだけを描画はしているのですが、0.1~0.3FPSしか出ません。
444
+
445
+ Graphics.DrawMeshのほうが軽いくらいです……。どのように書き換えたらよいでしょうか?