예제 #1
0
    void UvToolbox()
    {
        if (plotter.CurrentAction == HairyPlotterActions.VertexUvEdit)
        {
            GUI.color = VertexToolbarColor;

            if (plotter.UvEditVertex == null)
            {
                GUILayout.Label("UV Editor", EditorStyles.boldLabel);
                GUILayout.Label("Select one or more vertices to edit UV coords", EditorStyles.miniLabel);
                GUI.color = Color.white;
                return;
            }

            float x = 0;
            float y = 0;

            HairyPlotterVertex vertex = plotter.UvEditVertex;
            GUILayout.Label("UV Editor for #" + vertex.Index, EditorStyles.boldLabel);

            EditorGUILayout.BeginHorizontal();
            Vector2 value = EditVector2(vertex.Uv);
            EditorGUILayout.EndHorizontal();

            if (vertex.Uv != value)
            {
                vertex.Uv     = value;
                plotter.Dirty = true;
            }

            x = value.x;
            y = value.y;

            GUI.color = Color.white;

            Renderer plotterRenderer = plotter.GetComponent <Renderer>();

            if (plotterRenderer && plotterRenderer.sharedMaterial && plotterRenderer.sharedMaterial.mainTexture)
            {
                float ratio = (float)uvMarkerTexture.width / (float)uvMarkerTexture.height;
                float w     = 500f;
                float h     = w * (1f / ratio);
                Rect  r     = GUILayoutUtility.GetRect(w, h);

                GUI.DrawTexture(r, plotterRenderer.sharedMaterial.mainTexture);

                Rect marker = new Rect(r);

                marker.xMin   = r.xMin + (r.width * x);
                marker.yMin   = r.yMin + (r.height * (1f - y));
                marker.width  = 8;
                marker.height = 8;

                GUI.DrawTexture(marker, uvMarkerTexture);

                if (Event.current.type == EventType.MouseUp)
                {
                    Vector2 mpos = Event.current.mousePosition;

                    // Make sure we're within bounds
                    if (mpos.y - r.yMin >= 0 & mpos.y - r.yMin <= r.height && mpos.x >= 0 && mpos.x <= r.width)
                    {
                        x = (mpos.x / r.width);
                        y = 1f - (Mathf.Clamp(mpos.y - r.yMin, 0, float.MaxValue) / r.height);

                        if (x != vertex.Uv.x || y != vertex.Uv.y)
                        {
                            vertex.Uv     = new Vector2(x, y);
                            plotter.Dirty = true;
                        }
                    }
                }
            }
        }
    }