Tanoda
UVChecker.cs
Go to the documentation of this file.
1using System.Text;
2using UnityEditor;
3using UnityEngine;
4
6{
10 public class UVChecker : EditorWindow
11 {
12 private GameObject targetGameObject;
13 private MeshFilter targetMeshFilter;
14 private Texture2D tex;
15
16 [MenuItem("Window/InkPainter/UVChecker")]
17 private static void Open()
18 {
19 GetWindow<UVChecker>();
20 }
21
22 private void OnGUI()
23 {
24 targetGameObject = EditorGUILayout.ObjectField("TargetMesh", targetGameObject, typeof(GameObject), true) as GameObject;
25 if(GUILayout.Button("Execute"))
26 {
27 targetMeshFilter = targetGameObject.GetComponent<MeshFilter>();
28 tex = new Texture2D(256, 256);
29 var mesh = targetMeshFilter.sharedMesh;
30 DrawUV(mesh);
31 }
32 if(tex != null)
33 {
34 EditorGUI.DrawPreviewTexture(new Rect(10, 50, tex.width, tex.height), tex);
35 }
36 }
37
38 private void DrawUV(Mesh mesh)
39 {
40 var uvs = mesh.uv;
41 var tri = mesh.triangles;
42
43 for(int i_base = 0; i_base < tri.Length; i_base += 3)
44 {
45 int i_1 = i_base;
46 int i_2 = i_base + 1;
47 int i_3 = i_base + 2;
48
49 Vector2 uv1 = uvs[tri[i_1]];
50 Vector2 uv2 = uvs[tri[i_2]];
51 Vector2 uv3 = uvs[tri[i_3]];
52
53 DrawLine(uv1, uv2);
54 DrawLine(uv2, uv3);
55 DrawLine(uv3, uv1);
56 }
57
58 tex.Apply(false);
59
60 UVLog(uvs);
61 }
62
63 private void UVLog(Vector2[] uvs)
64 {
65 StringBuilder sb = new StringBuilder();
66 foreach(var uv in uvs)
67 {
68 sb.AppendLine(uv.ToString());
69 }
70
71 Debug.Log(sb.ToString());
72 }
73
74 private void DrawLine(Vector2 from, Vector2 to)
75 {
76 int x0 = Mathf.RoundToInt(from.x * tex.width);
77 int y0 = Mathf.RoundToInt(from.y * tex.height);
78 int x1 = Mathf.RoundToInt(to.x * tex.width);
79 int y1 = Mathf.RoundToInt(to.y * tex.height);
80
81 DrawLine(x0, y0, x1, y1, Color.red);
82 }
83
84 private void DrawLine(int x0, int y0, int x1, int y1, Color col)
85 {
86 int dy = y1 - y0;
87 int dx = x1 - x0;
88 int stepx, stepy;
89
90 if(dy < 0)
91 { dy = -dy; stepy = -1; }
92 else
93 { stepy = 1; }
94 if(dx < 0)
95 { dx = -dx; stepx = -1; }
96 else
97 { stepx = 1; }
98 dy <<= 1;
99 dx <<= 1;
100
101 float fraction = 0;
102
103 tex.SetPixel(x0, y0, col);
104 if(dx > dy)
105 {
106 fraction = dy - (dx >> 1);
107 while(Mathf.Abs(x0 - x1) > 1)
108 {
109 if(fraction >= 0)
110 {
111 y0 += stepy;
112 fraction -= dx;
113 }
114 x0 += stepx;
115 fraction += dy;
116 tex.SetPixel(x0, y0, col);
117 }
118 }
119 else
120 {
121 fraction = dx - (dy >> 1);
122 while(Mathf.Abs(y0 - y1) > 1)
123 {
124 if(fraction >= 0)
125 {
126 x0 += stepx;
127 fraction -= dy;
128 }
129 y0 += stepy;
130 fraction += dx;
131 tex.SetPixel(x0, y0, col);
132 }
133 }
134 }
135 }
136}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
Editor window to check UV.
Definition: UVChecker.cs:11