Advertisement
Muk99

UVViewer

Sep 22nd, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class UVPreview : EditorWindow {
  5.  
  6.     private Mesh mesh;
  7.     private Texture2D texture;
  8.     private int selectedUV;
  9.  
  10.     private Rect rect;
  11.     private Rect bounds;
  12.  
  13.     private float zoom = 1f;
  14.     private Vector2 pan;
  15.  
  16.     private Event evt;
  17.     private int triangle;
  18.  
  19.     private static Vector2 startSize = new Vector2(500, 560);
  20.  
  21.     private static Color edgeColor = new Color(0.3f, 0.8f, 1f, 1f);
  22.     private static Color faceColor = new Color(0.3f, 0.8f, 1f, 0.25f);
  23.  
  24.     private static string[] uvLabels {
  25.         get {
  26.             return new string[] { "UV", "UV2", "UV3", "UV4" };
  27.         }
  28.     }
  29.  
  30.     private bool drawEdges {
  31.         get {
  32.             return EditorPrefs.GetBool("DrawEdges");
  33.         }
  34.         set {
  35.             EditorPrefs.SetBool("DrawEdges", value);
  36.         }
  37.     }
  38.     private bool drawFaces {
  39.         get {
  40.             return EditorPrefs.GetBool("DrawFaces");
  41.         }
  42.         set {
  43.             EditorPrefs.SetBool("DrawFaces", value);
  44.         }
  45.     }
  46.  
  47.     [MenuItem("Window/UV Preview")]
  48.     private static void Init() {
  49.         var window = GetWindow<UVPreview>();
  50.         window.titleContent = new GUIContent("UV Preview");
  51.         window.wantsMouseMove = true;
  52.         window.minSize = startSize;
  53.         window.maxSize = startSize * 2;
  54.         window.Show();
  55.     }
  56.  
  57.     private void OnGUI() {
  58.         EditorGUILayout.Separator();
  59.         EditorGUILayout.BeginVertical("HelpBox");
  60.         EditorGUILayout.LabelField("UV Preview", EditorStyles.boldLabel);
  61.         EditorGUILayout.Separator();
  62.         EditorGUILayout.BeginHorizontal();
  63.         EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200f));
  64.  
  65.         mesh = EditorGUILayout.ObjectField(mesh, typeof(Mesh), true) as Mesh;
  66.  
  67.         GUI.enabled = mesh;
  68.  
  69.         texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), true) as Texture2D;
  70.  
  71.         EditorGUILayout.EndVertical();
  72.         EditorGUILayout.BeginVertical(GUILayout.MaxWidth(200f));
  73.         selectedUV = GUILayout.Toolbar(selectedUV, uvLabels);
  74.  
  75.         if(GUILayout.Button("Set default position")) {
  76.             zoom = 1f;
  77.             pan = Vector2.zero;
  78.         }
  79.  
  80.         EditorGUILayout.EndVertical();
  81.         GUILayout.FlexibleSpace();
  82.         EditorGUILayout.BeginVertical();
  83.  
  84.         drawEdges = EditorGUILayout.ToggleLeft("Edges", drawEdges, GUILayout.Width(50f));
  85.         drawFaces = EditorGUILayout.ToggleLeft("Faces", drawFaces, GUILayout.Width(50f));
  86.  
  87.         EditorGUILayout.EndVertical();
  88.         EditorGUILayout.EndHorizontal();
  89.         EditorGUILayout.EndVertical();
  90.         GUI.enabled = true;
  91.  
  92.         Vector2[] uv = new Vector2[0];
  93.         if(mesh) {
  94.             if(!mesh.isReadable) {
  95.                 EditorGUILayout.HelpBox("Mesh is not readable, you can change this on import settings", MessageType.Error);
  96.                 return;
  97.             }
  98.             switch(selectedUV) {
  99.                 case 1:
  100.                 uv = mesh.uv2;
  101.                 break;
  102.                 case 2:
  103.                 uv = mesh.uv3;
  104.                 break;
  105.                 case 3:
  106.                 uv = mesh.uv4;
  107.                 break;
  108.                 default:
  109.                 uv = mesh.uv;
  110.                 break;
  111.             }
  112.         }
  113.         else {
  114.             EditorGUILayout.HelpBox("No mesh selected", MessageType.Info);
  115.             return;
  116.         }
  117.  
  118.         if(uv.Length == 0) {
  119.             EditorGUILayout.HelpBox(string.Format("Mesh doesn't have UV{0}", selectedUV + 1), MessageType.Warning);
  120.             return;
  121.         }
  122.  
  123.         EditorGUILayout.BeginVertical("HelpBox");
  124.         bounds = EditorGUILayout.GetControlRect(GUILayout.ExpandHeight(true));
  125.         PanAndZoom();
  126.  
  127.         if(texture)
  128.             GUI.DrawTexture(bounds, texture);
  129.  
  130.         DrawUV(uv, mesh.triangles, rect);
  131.         EditorGUILayout.EndVertical();
  132.     }
  133.  
  134.     private void DrawUV(Vector2[] uv, int[] tris, Rect rect) {
  135.         for(triangle = 0; triangle < tris.Length; triangle += 3) {
  136.             var vertice0 = uv[tris[triangle + 0]];
  137.             var vertice1 = uv[tris[triangle + 1]];
  138.             var vertice2 = uv[tris[triangle + 2]];
  139.  
  140.             vertice0 = RelativeUVPosition(vertice0);
  141.             vertice1 = RelativeUVPosition(vertice1);
  142.             vertice2 = RelativeUVPosition(vertice2);
  143.  
  144.             Handles.color = edgeColor;
  145.             if(drawEdges)
  146.                 Handles.DrawAAPolyLine(vertice0, vertice1, vertice2, vertice0);
  147.             Handles.color = faceColor;
  148.             if(drawFaces)
  149.                 Handles.DrawAAConvexPolygon(vertice0, vertice1, vertice2);
  150.         }
  151.     }
  152.  
  153.     private void PanAndZoom() {
  154.         rect = bounds;
  155.         evt = Event.current;
  156.  
  157.         switch(evt.type) {
  158.             case EventType.ScrollWheel:
  159.             zoom += -evt.delta.y * 0.05f;
  160.             zoom = Mathf.Clamp(zoom, 0.3f, 2f);
  161.             evt.Use();
  162.             break;
  163.  
  164.             case EventType.MouseDrag:
  165.             if(bounds.Contains(evt.mousePosition)) {
  166.                 EditorGUIUtility.AddCursorRect(bounds, MouseCursor.Pan);
  167.                 pan += evt.delta;
  168.                 pan.x = Mathf.Clamp(pan.x, -bounds.width / 2f, bounds.width / 2f);
  169.                 pan.y = Mathf.Clamp(pan.y, -bounds.height / 2f, bounds.height / 2f);
  170.                 evt.Use();
  171.             }
  172.             break;
  173.         }
  174.  
  175.         rect.position += pan;
  176.         var center = rect.center;
  177.         rect.size *= zoom;
  178.         rect.center = center;
  179.     }
  180.  
  181.     private Vector3 RelativeUVPosition(Vector2 vertice) {
  182.         vertice.x *= rect.width;
  183.         vertice.y *= rect.height;
  184.         vertice.y *= -1f;
  185.         vertice.y += rect.height;
  186.  
  187.         vertice += rect.position;
  188.  
  189.         vertice.x = Mathf.Clamp(vertice.x, bounds.xMin, bounds.xMax);
  190.         vertice.y = Mathf.Clamp(vertice.y, bounds.yMin, bounds.yMax);
  191.  
  192.         return vertice;
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement