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