Ejemplo n.º 1
0
        private void OnMeshSelected(ManualUVSelection uvSelection)
        {
            PBMesh mesh = uvSelection.Mesh;

            PBEdge[] meshEdges = mesh.Edges;
            Dictionary <int, List <int> > faceToEdge = new Dictionary <int, List <int> >();

            Tuple <int, int>[] edges = new Tuple <int, int> [meshEdges.Length];
            for (int i = 0; i < meshEdges.Length; ++i)
            {
                PBEdge edge = meshEdges[i];
                edges[i] = new Tuple <int, int>(edge.A, edge.B);

                List <int> edgeIndices;
                if (!faceToEdge.TryGetValue(edge.FaceIndex, out edgeIndices))
                {
                    edgeIndices = new List <int>();
                    faceToEdge.Add(edge.FaceIndex, edgeIndices);
                }

                edgeIndices.Add(i);
            }

            ManualUVRenderer uvRenderer = m_meshToRenderer[mesh];

            uvRenderer.UV          = mesh.Textures;
            uvRenderer.IsSelected  = new bool[uvRenderer.UV.Length];
            uvRenderer.Edges       = edges;
            uvRenderer.FaceToEdges = faceToEdge;
            uvRenderer.Faces       = mesh.Faces.Select(f => f.Indexes).ToArray();
            uvRenderer.Refresh(false, false);
        }
 public ManualUVSelection(ManualUVSelection selection)
 {
     GameObject = selection.GameObject;
     Vertices   = new HashSet <int>(selection.Vertices);
     Faces      = new HashSet <int>(selection.Faces);
     Edges      = new HashSet <int>(selection.Edges);
 }
        private void RefreshSelectionAndPivotPoint()
        {
            m_selection.Clear();

            IMeshEditor editor = m_tool.GetEditor();

            if (editor == null)
            {
                return;
            }

            MeshSelection meshSelection = editor.GetSelection();

            if (meshSelection != null)
            {
                foreach (PBMesh mesh in meshSelection.GetSelectedMeshes())
                {
                    ManualUVSelection uvSelection = new ManualUVSelection(mesh.gameObject);
                    m_selection.Add(uvSelection);

                    if (MeshSelected != null)
                    {
                        MeshSelected(uvSelection);
                    }
                }

                Select(meshSelection);
            }

            RefreshPivotPoint();
        }
        public void SelectVertices(Func <Vector2, float> canSelect, bool selectClosest, bool clearSelection)
        {
            m_editor.Undo.BeginRecord();
            for (int index = 0; index < m_selection.Count; ++index)
            {
                ManualUVSelection selection    = m_selection[index];
                ManualUVSelection oldSelection = new ManualUVSelection(selection);

                if (clearSelection)
                {
                    selection.Clear();
                }

                PBMesh    mesh         = selection.Mesh;
                Vector2[] uvs          = mesh.Textures;
                float     minDistance  = float.PositiveInfinity;
                int       closestIndex = -1;
                for (int i = 0; i < uvs.Length; ++i)
                {
                    float distance = canSelect(uvs[i]);
                    if (selectClosest)
                    {
                        if (distance < minDistance)
                        {
                            closestIndex = i;
                            minDistance  = distance;
                        }
                    }
                    else
                    {
                        if (distance <= 0)
                        {
                            if (!selection.Vertices.Contains(i))
                            {
                                selection.Vertices.Add(i);
                            }
                        }
                    }
                }

                if (closestIndex >= 0)
                {
                    if (!selection.Vertices.Contains(closestIndex))
                    {
                        selection.Vertices.Add(closestIndex);
                    }
                }

                RecordSelection(index, oldSelection, new ManualUVSelection(selection));

                if (SelectionChanged != null)
                {
                    SelectionChanged(selection);
                }
            }

            m_editor.Undo.EndRecord();
        }
        private void Move(Vector2 delta)
        {
            for (int i = 0; i < m_selection.Count; ++i)
            {
                ManualUVSelection selection = m_selection[i];
                PBMesh            mesh      = selection.Mesh;
                Vector2[]         textures  = mesh.Textures;
                foreach (int index in m_indexes[i])
                {
                    textures[index] = m_initialUVs[i][index] + delta;
                }
                mesh.Textures = textures;
                mesh.RefreshUV();
            }

            if (UVChanged != null)
            {
                UVChanged();
            }
        }
        public void Scale()
        {
            Vector2 scale = m_pivotPoint.transform.localScale;

            for (int i = 0; i < m_selection.Count; ++i)
            {
                ManualUVSelection selection = m_selection[i];
                PBMesh            mesh      = selection.Mesh;
                Vector2[]         textures  = mesh.Textures;
                foreach (int index in m_indexes[i])
                {
                    textures[index] = m_initialUV + Vector2.Scale((m_initialUVs[i][index] - m_initialUV), scale);
                }
                mesh.Textures = textures;
                mesh.RefreshUV();
            }

            if (UVChanged != null)
            {
                UVChanged();
            }
        }
 private void RecordSelection(int index, ManualUVSelection oldSelection, ManualUVSelection newSelection)
 {
     m_editor.Undo.CreateRecord(record =>
     {
         m_selection[index] = newSelection;
         if (SelectionChanged != null)
         {
             SelectionChanged(newSelection);
         }
         RefreshPivotPoint();
         return(true);
     },
                                record =>
     {
         m_selection[index] = oldSelection;
         if (SelectionChanged != null)
         {
             SelectionChanged(oldSelection);
         }
         RefreshPivotPoint();
         return(true);
     });
 }
        public void Rotate()
        {
            Vector2 uv    = ManualUVRenderer.WorldToUV(m_pivotPoint.transform.position);
            float   angle = -m_pivotPoint.transform.eulerAngles.z;

            for (int i = 0; i < m_selection.Count; ++i)
            {
                ManualUVSelection selection = m_selection[i];
                PBMesh            mesh      = selection.Mesh;
                Vector2[]         textures  = mesh.Textures;
                foreach (int index in m_indexes[i])
                {
                    textures[index] = (m_initialUVs[i][index]).RotateAroundPoint(uv, angle);
                }
                mesh.Textures = textures;
                mesh.RefreshUV();
            }

            if (UVChanged != null)
            {
                UVChanged();
            }
        }
        private void Select(MeshSelection meshSelection)
        {
            for (int index = 0; index < m_selection.Count; ++index)
            {
                ManualUVSelection selection    = m_selection[index];
                ManualUVSelection oldSelection = new ManualUVSelection(selection);

                selection.Clear();

                if (meshSelection.HasVertices)
                {
                    foreach (int i in meshSelection.GetIndices(selection.Mesh))
                    {
                        selection.Vertices.Add(i);
                    }
                }
                else if (meshSelection.HasEdges)
                {
                    foreach (int i in meshSelection.GetEdges(selection.Mesh))
                    {
                        selection.Edges.Add(i);
                    }
                }
                else if (meshSelection.HasFaces)
                {
                    foreach (int i in meshSelection.GetFaces(selection.Mesh))
                    {
                        selection.Faces.Add(i);
                    }
                }

                if (SelectionChanged != null)
                {
                    SelectionChanged(selection);
                }
            }
        }
Ejemplo n.º 10
0
        private void OnUVSelectionChanged(ManualUVSelection selection)
        {
            ManualUVRenderer renderer = m_meshToRenderer[selection.Mesh];

            if (m_tool.Mode == ProBuilderToolMode.Vertex)
            {
                Color[] vertexColors = renderer.VertexColors;
                bool[]  isSelected   = renderer.IsSelected;
                for (int i = 0; i < vertexColors.Length; ++i)
                {
                    bool selected = selection.Vertices.Contains(i);
                    vertexColors[i] = selected ? m_selectionColor : Color.white;
                    isSelected[i]   = selected;
                }
            }
            else if (m_tool.Mode == ProBuilderToolMode.Edge)
            {
                PBEdge[] edges      = selection.Mesh.Edges;
                Color[]  edgeColors = renderer.EdgeColors;
                bool[]   isSelected = renderer.IsSelected;
                for (int i = 0; i < isSelected.Length; ++i)
                {
                    isSelected[i] = false;
                }

                for (int i = 0; i < edgeColors.Length; ++i)
                {
                    int  edgeIndex = i / 2;
                    bool selected  = selection.Edges.Contains(edgeIndex);
                    edgeColors[i] = selected ? m_selectionColor : Color.white;

                    PBEdge edge = edges[edgeIndex];
                    isSelected[edge.A] = selected;
                    isSelected[edge.B] = selected;
                }
            }
            else
            {
                Color[] vertexColors = renderer.VertexColors;
                int[][] faces        = renderer.Faces;
                bool[]  isSelected   = renderer.IsSelected;
                for (int i = 0; i < isSelected.Length; ++i)
                {
                    isSelected[i] = false;
                }
                for (int i = 0; i < vertexColors.Length; ++i)
                {
                    bool selected = selection.Faces.Contains(i);
                    vertexColors[i] = selected ? m_selectionColor : Color.white;

                    if (selected)
                    {
                        int[] faceIdexes = faces[i];
                        for (int j = 0; j < faceIdexes.Length; ++j)
                        {
                            int faceIndex = faceIdexes[j];
                            isSelected[faceIndex] = true;
                        }
                    }
                }

                Color[] edgeColors = renderer.EdgeColors;
                for (int i = 0; i < edgeColors.Length; ++i)
                {
                    edgeColors[i] = Color.white;
                }

                Dictionary <int, List <int> > faceToEdges = renderer.FaceToEdges;
                foreach (int faceIndex in selection.Faces)
                {
                    List <int> edgeIndices = faceToEdges[faceIndex];
                    for (int i = 0; i < edgeIndices.Count; ++i)
                    {
                        int edgeIndex = edgeIndices[i];
                        edgeColors[edgeIndex * 2 + 0] = m_selectionColor;
                        edgeColors[edgeIndex * 2 + 1] = m_selectionColor;
                    }
                }
            }

            renderer.Refresh(true, true);
        }
        public void SelectFaces(Func <Vector2, float> canSelect, bool selectClosest, bool clearSelection)
        {
            m_editor.Undo.BeginRecord();
            for (int index = 0; index < m_selection.Count; ++index)
            {
                ManualUVSelection selection    = m_selection[index];
                ManualUVSelection oldSelection = new ManualUVSelection(selection);

                if (clearSelection)
                {
                    selection.Clear();
                }

                PBMesh    mesh         = selection.Mesh;
                Vector2[] meshUV       = mesh.Textures;
                PBFace[]  meshFaces    = mesh.Faces;
                float     minDistance  = float.PositiveInfinity;
                int       closestIndex = -1;
                for (int i = 0; i < meshFaces.Length; ++i)
                {
                    PBFace  face = meshFaces[i];
                    Vector2 uv   = meshUV[face.Indexes[0]];
                    for (int j = 1; j < face.Indexes.Length; ++j)
                    {
                        uv += meshUV[face.Indexes[j]];
                    }
                    uv /= face.Indexes.Length;

                    float distance = canSelect(uv);
                    if (selectClosest)
                    {
                        if (distance < minDistance)
                        {
                            closestIndex = i;
                            minDistance  = distance;
                        }
                    }
                    else
                    {
                        if (distance <= 0)
                        {
                            if (!selection.Faces.Contains(i))
                            {
                                selection.Faces.Add(i);
                            }
                        }
                    }
                }

                if (closestIndex >= 0)
                {
                    if (!selection.Faces.Contains(closestIndex))
                    {
                        selection.Faces.Add(closestIndex);
                    }
                }

                RecordSelection(index, oldSelection, new ManualUVSelection(selection));

                if (SelectionChanged != null)
                {
                    SelectionChanged(selection);
                }
            }

            m_editor.Undo.EndRecord();
        }