teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追加

2020/06/24 09:14

投稿

rtag
rtag

スコア5

title CHANGED
File without changes
body CHANGED
@@ -13,4 +13,153 @@
13
13
  画面の解像度を指定しても綺麗に表示するためにはどうしたらよろしいでしょうか?
14
14
  私の知識では解像度によって太さを変えるという手法しか思いつきませんでした・・・
15
15
 
16
- よろしくお願いいたします。
16
+ よろしくお願いいたします。
17
+
18
+ 追記
19
+
20
+ 以下のコードを使用いたしました。
21
+ 最終目標としては、カメラをズームアウト、インしても、太さがほぼ変わらないものを目指しています。
22
+
23
+ ```cs
24
+ using System.Collections;
25
+ using System.Collections.Generic;
26
+ using UnityEngine;
27
+
28
+ public class TestGrid : MonoBehaviour
29
+ {
30
+
31
+ [SerializeField]
32
+ private UnityEngine.Grid m_grid;
33
+
34
+ [SerializeField]
35
+ private MeshRenderer m_meshRenderer;
36
+
37
+ [SerializeField]
38
+ private MeshFilter m_meshFilter;
39
+
40
+ private Mesh m_gridMesh = null;
41
+
42
+ [SerializeField]
43
+ private Material m_gridMaterial;
44
+
45
+
46
+ private const float k_GridGizmoDistanceFalloff = 50f;
47
+
48
+
49
+ void Start()
50
+ {
51
+ m_gridMesh = Generate(m_grid, Color.white, 0.0f, new RectInt(0, 0, 10, 10));
52
+
53
+ m_meshFilter.sharedMesh = m_gridMesh;
54
+
55
+ m_meshRenderer.material = m_gridMaterial;
56
+ }
57
+
58
+
59
+
60
+ public static Mesh Generate(GridLayout gridLayout, Color color, float screenPixelSize, RectInt bounds)
61
+ {
62
+ Mesh mesh = new Mesh();
63
+ mesh.hideFlags = HideFlags.HideAndDontSave;
64
+
65
+ int vertex = 0;
66
+
67
+ int totalVertices = 4 * (bounds.size.x + bounds.size.y);
68
+
69
+ Vector3 horizontalPixelOffset = new Vector3(screenPixelSize, 0f, 0f);
70
+ Vector3 verticalPixelOffset = new Vector3(0f, screenPixelSize, 0f);
71
+
72
+ Vector3[] vertices = new Vector3[totalVertices];
73
+ Vector2[] uvs2 = new Vector2[totalVertices];
74
+
75
+ Vector3 cellStride = gridLayout.cellSize + gridLayout.cellGap;
76
+ Vector3Int minPosition = new Vector3Int(0, bounds.min.y, 0);
77
+ Vector3Int maxPosition = new Vector3Int(0, bounds.max.y, 0);
78
+
79
+ Vector3 cellGap = Vector3.zero;
80
+ if (!Mathf.Approximately(cellStride.x, 0f))
81
+ {
82
+ cellGap.x = gridLayout.cellSize.x / cellStride.x;
83
+ }
84
+
85
+ for (int x = bounds.min.x; x < bounds.max.x; x++)
86
+ {
87
+ minPosition.x = x;
88
+ maxPosition.x = x;
89
+
90
+ vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
91
+ vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
92
+ uvs2[vertex + 0] = Vector2.zero;
93
+ uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
94
+
95
+ vertex += 2;
96
+
97
+ vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
98
+ vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
99
+ uvs2[vertex + 0] = Vector2.zero;
100
+ uvs2[vertex + 1] = new Vector2(0f, cellStride.y * bounds.size.y);
101
+
102
+ vertex += 2;
103
+ }
104
+
105
+ minPosition = new Vector3Int(bounds.min.x, 0, 0);
106
+ maxPosition = new Vector3Int(bounds.max.x, 0, 0);
107
+ cellGap = Vector3.zero;
108
+ if (!Mathf.Approximately(cellStride.y, 0f))
109
+ {
110
+ cellGap.y = gridLayout.cellSize.y / cellStride.y;
111
+ }
112
+
113
+ for (int y = bounds.min.y; y < bounds.max.y; y++)
114
+ {
115
+ minPosition.y = y;
116
+ maxPosition.y = y;
117
+
118
+ vertices[vertex + 0] = gridLayout.CellToLocal(minPosition);
119
+ vertices[vertex + 1] = gridLayout.CellToLocal(maxPosition);
120
+ uvs2[vertex + 0] = Vector2.zero;
121
+ uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
122
+
123
+ vertex += 2;
124
+
125
+ vertices[vertex + 0] = gridLayout.CellToLocalInterpolated(minPosition + cellGap);
126
+ vertices[vertex + 1] = gridLayout.CellToLocalInterpolated(maxPosition + cellGap);
127
+ uvs2[vertex + 0] = Vector2.zero;
128
+ uvs2[vertex + 1] = new Vector2(cellStride.x * bounds.size.x, 0f);
129
+
130
+ vertex += 2;
131
+ }
132
+
133
+ var uv0 = new Vector2(k_GridGizmoDistanceFalloff, 0f);
134
+ var uvs = new Vector2[vertex];
135
+ var indices = new int[vertex];
136
+ var colors = new Color[vertex];
137
+ var normals = new Vector3[totalVertices];
138
+ var uvs3 = new Vector2[totalVertices];
139
+
140
+ for (int i = 0; i < vertex; i++)
141
+ {
142
+ uvs[i] = uv0;
143
+ indices[i] = i;
144
+ colors[i] = color;
145
+ var alternate = i + ((i % 2) == 0 ? 1 : -1);
146
+ normals[i] = vertices[alternate];
147
+ uvs3[i] = uvs2[alternate];
148
+ }
149
+
150
+ mesh.vertices = vertices;
151
+ mesh.uv = uvs;
152
+ mesh.uv2 = uvs2;
153
+ mesh.uv3 = uvs3;
154
+ mesh.colors = colors;
155
+ mesh.normals = normals;
156
+ mesh.SetIndices(indices, MeshTopology.Lines, 0);
157
+
158
+ return mesh;
159
+ }
160
+
161
+
162
+ }
163
+
164
+
165
+ ```