質問編集履歴

1

コードの追加

2020/06/24 09:14

投稿

rtag
rtag

スコア5

test CHANGED
File without changes
test CHANGED
@@ -29,3 +29,301 @@
29
29
 
30
30
 
31
31
  よろしくお願いいたします。
32
+
33
+
34
+
35
+ 追記
36
+
37
+
38
+
39
+ 以下のコードを使用いたしました。
40
+
41
+ 最終目標としては、カメラをズームアウト、インしても、太さがほぼ変わらないものを目指しています。
42
+
43
+
44
+
45
+ ```cs
46
+
47
+ using System.Collections;
48
+
49
+ using System.Collections.Generic;
50
+
51
+ using UnityEngine;
52
+
53
+
54
+
55
+ public class TestGrid : MonoBehaviour
56
+
57
+ {
58
+
59
+
60
+
61
+ [SerializeField]
62
+
63
+ private UnityEngine.Grid m_grid;
64
+
65
+
66
+
67
+ [SerializeField]
68
+
69
+ private MeshRenderer m_meshRenderer;
70
+
71
+
72
+
73
+ [SerializeField]
74
+
75
+ private MeshFilter m_meshFilter;
76
+
77
+
78
+
79
+ private Mesh m_gridMesh = null;
80
+
81
+
82
+
83
+ [SerializeField]
84
+
85
+ private Material m_gridMaterial;
86
+
87
+
88
+
89
+
90
+
91
+ private const float k_GridGizmoDistanceFalloff = 50f;
92
+
93
+
94
+
95
+
96
+
97
+ void Start()
98
+
99
+ {
100
+
101
+ m_gridMesh = Generate(m_grid, Color.white, 0.0f, new RectInt(0, 0, 10, 10));
102
+
103
+
104
+
105
+ m_meshFilter.sharedMesh = m_gridMesh;
106
+
107
+
108
+
109
+ m_meshRenderer.material = m_gridMaterial;
110
+
111
+ }
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+ public static Mesh Generate(GridLayout gridLayout, Color color, float screenPixelSize, RectInt bounds)
120
+
121
+ {
122
+
123
+ Mesh mesh = new Mesh();
124
+
125
+ mesh.hideFlags = HideFlags.HideAndDontSave;
126
+
127
+
128
+
129
+ int vertex = 0;
130
+
131
+
132
+
133
+ int totalVertices = 4 * (bounds.size.x + bounds.size.y);
134
+
135
+
136
+
137
+ Vector3 horizontalPixelOffset = new Vector3(screenPixelSize, 0f, 0f);
138
+
139
+ Vector3 verticalPixelOffset = new Vector3(0f, screenPixelSize, 0f);
140
+
141
+
142
+
143
+ Vector3[] vertices = new Vector3[totalVertices];
144
+
145
+ Vector2[] uvs2 = new Vector2[totalVertices];
146
+
147
+
148
+
149
+ Vector3 cellStride = gridLayout.cellSize + gridLayout.cellGap;
150
+
151
+ Vector3Int minPosition = new Vector3Int(0, bounds.min.y, 0);
152
+
153
+ Vector3Int maxPosition = new Vector3Int(0, bounds.max.y, 0);
154
+
155
+
156
+
157
+ Vector3 cellGap = Vector3.zero;
158
+
159
+ if (!Mathf.Approximately(cellStride.x, 0f))
160
+
161
+ {
162
+
163
+ cellGap.x = gridLayout.cellSize.x / cellStride.x;
164
+
165
+ }
166
+
167
+
168
+
169
+ for (int x = bounds.min.x; x < bounds.max.x; x++)
170
+
171
+ {
172
+
173
+ minPosition.x = x;
174
+
175
+ maxPosition.x = x;
176
+
177
+
178
+
179
+ vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
180
+
181
+ vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
182
+
183
+ uvs2[vertex + 0] = Vector2.zero;
184
+
185
+ uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
186
+
187
+
188
+
189
+ vertex += 2;
190
+
191
+
192
+
193
+ vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
194
+
195
+ vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
196
+
197
+ uvs2[vertex + 0] = Vector2.zero;
198
+
199
+ uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
200
+
201
+
202
+
203
+ vertex += 2;
204
+
205
+ }
206
+
207
+
208
+
209
+ minPosition = new Vector3Int(bounds.min.x, 0, 0);
210
+
211
+ maxPosition = new Vector3Int(bounds.max.x, 0, 0);
212
+
213
+ cellGap = Vector3.zero;
214
+
215
+ if (!Mathf.Approximately(cellStride.y, 0f))
216
+
217
+ {
218
+
219
+ cellGap.y = gridLayout.cellSize.y / cellStride.y;
220
+
221
+ }
222
+
223
+
224
+
225
+ for (int y = bounds.min.y; y < bounds.max.y; y++)
226
+
227
+ {
228
+
229
+ minPosition.y = y;
230
+
231
+ maxPosition.y = y;
232
+
233
+
234
+
235
+ vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
236
+
237
+ vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
238
+
239
+ uvs2[vertex + 0] = Vector2.zero;
240
+
241
+ uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
242
+
243
+
244
+
245
+ vertex += 2;
246
+
247
+
248
+
249
+ vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
250
+
251
+ vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
252
+
253
+ uvs2[vertex + 0] = Vector2.zero;
254
+
255
+ uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
256
+
257
+
258
+
259
+ vertex += 2;
260
+
261
+ }
262
+
263
+
264
+
265
+ var uv0 = new Vector2(k_GridGizmoDistanceFalloff, 0f);
266
+
267
+ var uvs = new Vector2[vertex];
268
+
269
+ var indices = new int[vertex];
270
+
271
+ var colors = new Color[vertex];
272
+
273
+ var normals = new Vector3[totalVertices];
274
+
275
+ var uvs3 = new Vector2[totalVertices];
276
+
277
+
278
+
279
+ for (int i = 0; i < vertex; i++)
280
+
281
+ {
282
+
283
+ uvs[i] = uv0;
284
+
285
+ indices[i] = i;
286
+
287
+ colors[i] = color;
288
+
289
+ var alternate = i + ((i % 2) == 0 ? 1 : -1);
290
+
291
+ normals[i] = vertices[alternate];
292
+
293
+ uvs3[i] = uvs2[alternate];
294
+
295
+ }
296
+
297
+
298
+
299
+ mesh.vertices = vertices;
300
+
301
+ mesh.uv = uvs;
302
+
303
+ mesh.uv2 = uvs2;
304
+
305
+ mesh.uv3 = uvs3;
306
+
307
+ mesh.colors = colors;
308
+
309
+ mesh.normals = normals;
310
+
311
+ mesh.SetIndices(indices, MeshTopology.Lines, 0);
312
+
313
+
314
+
315
+ return mesh;
316
+
317
+ }
318
+
319
+
320
+
321
+
322
+
323
+ }
324
+
325
+
326
+
327
+
328
+
329
+ ```