Exemplo n.º 1
0
    public void DestroyVertex(HairyPlotterVertex vertex)
    {
        if (vertex != null)
        {
            if (uvEditVertex == vertex)
            {
                uvEditVertex = null;
            }

            // Mark dirty
            Dirty = true;

            // Remove from vertices and selection
            vertices.Remove(vertex);
            verticeSelection.Remove(vertex);

            for (int i = 0; i < vertex.TriangleCount; ++i)
            {
                DestroyTriangle(vertex.GetTriangle(i));
            }

            // Update
            UpdateVertexIndexes();
        }
    }
Exemplo n.º 2
0
 public void ReplaceVertex(HairyPlotterVertex vertex, int index)
 {
     vertices[index].RemoveTriangle(this);
     vertices[index] = vertex;
     vertices[index].AddTriangle(this);
     plotter.Dirty = true;
 }
Exemplo n.º 3
0
 public void ClearVertices()
 {
     vertices.Clear();
     verticeSelection.Clear();
     ClearTriangles();
     uvEditVertex = null;
     Dirty        = true;
 }
Exemplo n.º 4
0
    public void AddSelection(HairyPlotterVertex vertex)
    {
        if (vertex == null)
        {
            return;
        }

        verticeSelection.Add(vertex);
        ResetSelectionPosition();
    }
Exemplo n.º 5
0
    public void RemoveSelection(HairyPlotterVertex vertex)
    {
        if (uvEditVertex == vertex)
        {
            uvEditVertex = null;
        }

        verticeSelection.Remove(vertex);
        ResetSelectionPosition();
    }
Exemplo n.º 6
0
    public void SetVertices(HairyPlotterVertex v0, HairyPlotterVertex v1, HairyPlotterVertex v2)
    {
        vertices = new HairyPlotterVertex[3] {
            v0, v1, v2
        };

        v0.AddTriangle(this);
        v1.AddTriangle(this);
        v2.AddTriangle(this);

        plotter.Dirty = true;
    }
Exemplo n.º 7
0
    public HairyPlotterTriangle CreateTriangle(HairyPlotterVertex v0, HairyPlotterVertex v1, HairyPlotterVertex v2)
    {
        // Create tri
        HairyPlotterTriangle triangle = new HairyPlotterTriangle(this);

        // Set vertices for triangle
        triangle.SetVertices(v0, v1, v2);

        // Add to triangle list
        triangles.Add(triangle);

        // Return
        return(triangle);
    }
Exemplo n.º 8
0
    public HairyPlotterVertex CreateVertex(Vector3 pos, Vector2 uv)
    {
        // Create vertex
        HairyPlotterVertex vertex = new HairyPlotterVertex(pos, uv, this);

        // Set index
        vertex.Index = vertices.Count;

        // Add to list
        vertices.Add(vertex);

        // Return
        return(vertex);
    }
Exemplo n.º 9
0
    public void ToggleSelected(HairyPlotterVertex vertex)
    {
        if (vertex == null)
            return;

        if (IsSelected(vertex))
        {
            RemoveSelection(vertex);
        }
        else
        {
            AddSelection(vertex);
        }

        ResetSelectionPosition();
    }
Exemplo n.º 10
0
    public void ToggleSelected(HairyPlotterVertex vertex)
    {
        if (vertex == null)
        {
            return;
        }

        if (IsSelected(vertex))
        {
            RemoveSelection(vertex);
        }
        else
        {
            AddSelection(vertex);
        }

        ResetSelectionPosition();
    }
Exemplo n.º 11
0
    HairyPlotterVertex RaycastVertex()
    {
        // This transform from GUI space to scene camera ray
        Ray       r = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
        Transform t = plotter.transform;         // this for position gizmo vertices and triangles according parent game object world coordinates

        // Iterate over each vertex in plotter
        for (int i = 0; i < plotter.VertexCount; ++i)
        {
            // Grab triangle
            HairyPlotterVertex vertex = plotter.GetVertex(i);

            // Intersect ray
            if (HairyPlotter.RayIntersectsVertex(r, t.TransformPoint(vertex.Position), GizmoSize))
            {
                // Found!
                return(vertex);
            }
        }

        return(null);
    }
Exemplo n.º 12
0
    public void SwitchVertices(HairyPlotterVertex a, HairyPlotterVertex b)
    {
        int aIndex = Array.IndexOf(vertices, a);
        int bIndex = Array.IndexOf(vertices, b);

        if (aIndex == -1 && bIndex == -1)
        {
            return;
        }

        // Switch both
        if (aIndex > -1 && bIndex > -1)
        {
            vertices[bIndex] = a;
            vertices[aIndex] = b;
        }

        // Switch a for b
        if (aIndex > -1)
        {
            vertices[aIndex] = b;

            a.RemoveTriangle(this);
            b.AddTriangle(this);
        }

        // Switch b for a
        if (bIndex > -1)
        {
            vertices[bIndex] = a;

            b.RemoveTriangle(this);
            a.AddTriangle(this);
        }

        plotter.Dirty = true;
    }
Exemplo n.º 13
0
    void OnSceneGUI()
    {
        if (!plotter)
        {
            return;
        }

        plotter.InitEditing();

        if (plotter.CurrentAction == HairyPlotterActions.None)
        {
            // don't hover triangle when moving vertices
            if (allowMoveVerts)
            {
                plotter.SetTriangleHover(null);
            }
            else
            {
                plotter.SetTriangleHover(HoverTriangle());
            }

            if (plotter.ToggleSelected(PickTriangle()) && !allowMoveVerts)
            {
                Repaint();
            }

            if (plotter.HoveredTriangle != null)
            {
                SceneView.lastActiveSceneView.Repaint();
            }

            // detect if dragging a single vertex or selected vertices and update position/s according mouse position
            if (Event.current.button == 0)
            {
                KeyCode   _keyCode = Event.current.keyCode;
                EventType _type    = Event.current.type;

                if (_type == EventType.MouseDown)
                {
                    // pick selected vertices?
                    if (_keyCode == KeyCode.LeftShift || _keyCode == KeyCode.RightShift)
                    {
                        // use plotter.SelectedVertices
                    }
                    // or pick single vertex
                    else
                    {
                        hoveredVertex = PickVertex();
                    }
                    // set flag for allow moving vertices
                    allowMoveVerts = true;
                }
                // on mouse up finish the edition
                else if (_type == EventType.MouseUp)
                {
                    allowMoveVerts = false;
                    hoveredVertex  = null;
                }
                // while dragging mouse update vertex position
                else if (_type == EventType.MouseDrag && allowMoveVerts)
                {
                    Ray       r = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                    Transform t = plotter.transform;                     // this for position gizmo vertices and triangles according parent game object world coordinates
                    Vector2   p = t.InverseTransformPoint(r.origin);     // use inverse trasnform because then we transform again for displaying

                    // moving selected vertices?
                    if (_keyCode == KeyCode.LeftShift || _keyCode == KeyCode.RightShift)
                    {
                        for (int i = 0, c = plotter.SelectedVertices.Count; i < c; ++i)
                        {
                            plotter.SelectedVertices[i].Position.x = p.x;
                            plotter.SelectedVertices[i].Position.y = p.y;
                            if (HairyPlotterEditor.KeepUVOnVertsMove)
                            {
                                plotter.SelectedVertices[i].Uv.x = 0.5f + p.x;
                                plotter.SelectedVertices[i].Uv.y = 0.5f + p.y;
                            }
                        }
                    }
                    // or single vertex?
                    else if (hoveredVertex != null)
                    {
                        hoveredVertex.Position.x = p.x;
                        hoveredVertex.Position.y = p.y;
                        if (HairyPlotterEditor.KeepUVOnVertsMove)
                        {
                            hoveredVertex.Uv.x = 0.5f + p.x;
                            hoveredVertex.Uv.y = 0.5f + p.y;
                        }
                    }
                    plotter.Dirty = true;
                    plotter.ResetSelectionPosition();
                }
            }
        }

        if (plotter.CurrentAction == HairyPlotterActions.TriangleSwitch)
        {
            HairyPlotterTriangle triangle = PickTriangle();

            if (triangle != null)
            {
                List <HairyPlotterVertex> vertices = plotter.SelectedVertices;

                if (vertices.Count == 2)
                {
                    triangle.SwitchVertices(vertices[0], vertices[1]);
                    plotter.ClearVertexSelection();
                }
                else
                {
                    Debug.LogWarning("Switch In Triangle requires exactly two selected vertices");
                }
            }
        }

        DrawTriangles();
    }
Exemplo n.º 14
0
 public void ClearVertices()
 {
     vertices.Clear();
     verticeSelection.Clear();
     ClearTriangles();
     uvEditVertex = null;
     Dirty = true;
 }
Exemplo n.º 15
0
    public void DestroyVertex(HairyPlotterVertex vertex)
    {
        if (vertex != null)
        {
            if (uvEditVertex == vertex)
                uvEditVertex = null;

            // Mark dirty
            Dirty = true;

            // Remove from vertices and selection
            vertices.Remove(vertex);
            verticeSelection.Remove(vertex);

            for (int i = 0; i < vertex.TriangleCount; ++i)
            {
                DestroyTriangle(vertex.GetTriangle(i));
            }

            // Update
            UpdateVertexIndexes();
        }
    }
Exemplo n.º 16
0
    public HairyPlotterTriangle CreateTriangle(HairyPlotterVertex v0, HairyPlotterVertex v1, HairyPlotterVertex v2)
    {
        // Create tri
        HairyPlotterTriangle triangle = new HairyPlotterTriangle(this);

        // Set vertices for triangle
        triangle.SetVertices(v0, v1, v2);

        // Add to triangle list
        triangles.Add(triangle);

        // Return
        return triangle;
    }
Exemplo n.º 17
0
    public HairyPlotterVertex CreateVertex(Vector3 pos, Vector2 uv)
    {
        // Create vertex
        HairyPlotterVertex vertex = new HairyPlotterVertex(pos, uv, this);

        // Set index
        vertex.Index = vertices.Count;

        // Add to list
        vertices.Add(vertex);

        // Return
        return vertex;
    }
Exemplo n.º 18
0
 public void SetUvEditVertex(HairyPlotterVertex vertex)
 {
     uvEditVertex = vertex;
 }
Exemplo n.º 19
0
 public void ClearVertexSelection()
 {
     uvEditVertex = null;
     verticeSelection.Clear();
     ResetSelectionPosition();
 }
Exemplo n.º 20
0
    public void RemoveSelection(HairyPlotterVertex vertex)
    {
        if (uvEditVertex == vertex)
            uvEditVertex = null;

        verticeSelection.Remove(vertex);
        ResetSelectionPosition();
    }
Exemplo n.º 21
0
    public void AddSelection(HairyPlotterVertex vertex)
    {
        if (vertex == null)
            return;

        verticeSelection.Add(vertex);
        ResetSelectionPosition();
    }
Exemplo n.º 22
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;
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 23
0
 public void ClearVertexSelection()
 {
     uvEditVertex = null;
     verticeSelection.Clear();
     ResetSelectionPosition();
 }
Exemplo n.º 24
0
    public void SwitchVertices(HairyPlotterVertex a, HairyPlotterVertex b)
    {
        int aIndex = Array.IndexOf(vertices, a);
        int bIndex = Array.IndexOf(vertices, b);

        if (aIndex == -1 && bIndex == -1)
        {
            return;
        }

        // Switch both
        if (aIndex > -1 && bIndex > -1)
        {
            vertices[bIndex] = a;
            vertices[aIndex] = b;
        }

        // Switch a for b
        if (aIndex > -1)
        {
            vertices[aIndex] = b;

            a.RemoveTriangle(this);
            b.AddTriangle(this);
        }

        // Switch b for a
        if (bIndex > -1)
        {
            vertices[bIndex] = a;

            b.RemoveTriangle(this);
            a.AddTriangle(this);
        }

        plotter.Dirty = true;
    }
Exemplo n.º 25
0
    public void SetVertices(HairyPlotterVertex v0, HairyPlotterVertex v1, HairyPlotterVertex v2)
    {
        vertices = new HairyPlotterVertex[3] { v0, v1, v2 };

        v0.AddTriangle(this);
        v1.AddTriangle(this);
        v2.AddTriangle(this);

        plotter.Dirty = true;
    }
Exemplo n.º 26
0
 public void SetUvEditVertex(HairyPlotterVertex vertex)
 {
     uvEditVertex = vertex;
 }
Exemplo n.º 27
0
 public void ReplaceVertex(HairyPlotterVertex vertex, int index)
 {
     vertices[index].RemoveTriangle(this);
     vertices[index] = vertex;
     vertices[index].AddTriangle(this);
     plotter.Dirty = true;
 }
Exemplo n.º 28
0
    void TriangleSelectionToolbox()
    {
        if (plotter.CurrentAction != HairyPlotterActions.VertexUvEdit)
        {
            if (plotter.TriangleSelectionCount > 0)
            {
                EditorGUILayout.LabelField("Triangle Selection: " + plotter.TriangleSelectionCount, EditorStyles.boldLabel);
                EditorGUILayout.BeginHorizontal();

                GUI.color = TriangleSelectionToolbarColor;

                if (GUILayout.Button("Create Unique Vertices", EditorStyles.miniButton))
                {
                    HashSet <HairyPlotterTriangle> selectedTriangles = new HashSet <HairyPlotterTriangle>(plotter.SelectedTriangles);
                    HashSet <HairyPlotterVertex>   uniqueVertices    = new HashSet <HairyPlotterVertex>(selectedTriangles.SelectMany(x => x.VertexObjects).Distinct());

                    // Iterate over each vertex that the selectedTriangles use
                    foreach (HairyPlotterVertex vertex in uniqueVertices)
                    {
                        // Iterate over each triangle the vertex blongs to
                        foreach (HairyPlotterTriangle vertexTriangle in vertex.TriangleSet)
                        {
                            // If the triangle is NOT in the selected triangles
                            if (!selectedTriangles.Contains(vertexTriangle))
                            {
                                // Create a clone of the vertex
                                HairyPlotterVertex vertexClone = plotter.CreateVertex(vertex.Position, vertex.Uv);

                                // And step through each selected triangle, and switch out the shared vertex for the clone
                                foreach (HairyPlotterTriangle selectedTriangle in selectedTriangles)
                                {
                                    selectedTriangle.SwitchVertices(vertex, vertexClone);
                                }

                                // We're done
                                break;
                            }
                        }
                    }

                    plotter.Dirty = true;
                    plotter.UpdateVertexIndexes();
                }

                if (GUILayout.Button("Select Vertices", EditorStyles.miniButton))
                {
                    HashSet <HairyPlotterTriangle> selectedTriangles         = new HashSet <HairyPlotterTriangle>(plotter.SelectedTriangles);
                    HashSet <HairyPlotterVertex>   selectedTrianglesVertices = new HashSet <HairyPlotterVertex>(selectedTriangles.SelectMany(x => x.VertexObjects));

                    plotter.ClearVertexSelection();

                    foreach (HairyPlotterVertex vertex in selectedTrianglesVertices)
                    {
                        plotter.AddSelection(vertex);
                    }

                    // Clear triangle selection
                    plotter.ClearTriangleSelection();
                }

                if (GUILayout.Button("Clear Selection", EditorStyles.miniButton))
                {
                    plotter.ClearTriangleSelection();
                }

                if (plotter.CurrentAction == HairyPlotterActions.TriangleDelete)
                {
                    if (GUILayout.Button("Yes!", EditorStyles.miniButton))
                    {
                        foreach (HairyPlotterTriangle triangle in plotter.SelectedTriangles)
                        {
                            plotter.DestroyTriangle(triangle);
                        }

                        plotter.CurrentAction = HairyPlotterActions.None;
                    }

                    if (GUILayout.Button("No!", EditorStyles.miniButton))
                    {
                        plotter.CurrentAction = HairyPlotterActions.None;
                    }
                }
                else
                {
                    if (GUILayout.Button("Delete Selected", EditorStyles.miniButton))
                    {
                        plotter.CurrentAction = HairyPlotterActions.TriangleDelete;
                    }
                }

                GUI.color = Color.white;
                EditorGUILayout.EndHorizontal();
            }
        }
    }
Exemplo n.º 29
0
 public bool IsSelected(HairyPlotterVertex vertex)
 {
     return vertex != null && verticeSelection.Contains(vertex);
 }
Exemplo n.º 30
0
 public bool IsSelected(HairyPlotterVertex vertex)
 {
     return(vertex != null && verticeSelection.Contains(vertex));
 }