Example #1
0
        public void Add(ProBuilderMesh mesh, IEnumerable <Edge> edges)
        {
            HashSet <Edge> edgesHs;
            List <Edge>    edgesList;
            MeshFilter     edgesSelection;

            m_meshToSelection.TryGetValue(mesh, out edgesSelection);
            m_meshToEdgesList.TryGetValue(mesh, out edgesList);
            if (!m_meshToEdges.TryGetValue(mesh, out edgesHs))
            {
                edgesSelection = CreateEdgesGameObject(mesh);
                edgesSelection.transform.SetParent(mesh.transform, false);

                edgesHs   = new HashSet <Edge>();
                edgesList = new List <Edge>();

                m_meshToSelection.Add(mesh, edgesSelection);
                m_meshToEdges.Add(mesh, edgesHs);
                m_meshToEdgesList.Add(mesh, edgesList);
                m_meshes.Add(mesh);

                PBMesh pbMesh = mesh.GetComponent <PBMesh>();
                if (pbMesh != null)
                {
                    pbMesh.RaiseSelected(true);
                }
                m_pbMeshes.Add(pbMesh);
            }

            int vertexCount = mesh.vertexCount;

            Edge[] notSelectedEdges = edges.Where(edge => !edgesHs.Contains(edge) && vertexCount > edge.a && vertexCount > edge.b).ToArray();
            SetEdgesColor(mesh, edgesSelection, m_selectedColor, notSelectedEdges);
            for (int i = 0; i < notSelectedEdges.Length; ++i)
            {
                edgesHs.Add(notSelectedEdges[i]);
                edgesList.Add(notSelectedEdges[i]);
            }

            if (notSelectedEdges.Length > 0)
            {
                m_lastPosition = GetPosition(mesh, notSelectedEdges.Last());
                m_lastNormal   = GetNormal(mesh, notSelectedEdges.Last());

                for (int i = 0; i < notSelectedEdges.Length; i++)
                {
                    m_selectedEdgesCount++;
                    if (m_selectedEdgesCount == 1)
                    {
                        m_centerOfMass = mesh.transform.TransformPoint(GetPosition(mesh, notSelectedEdges[i]));
                    }
                    else
                    {
                        m_centerOfMass *= (m_selectedEdgesCount - 1) / (float)m_selectedEdgesCount;
                        m_centerOfMass += mesh.transform.TransformPoint(GetPosition(mesh, notSelectedEdges[i])) / m_selectedEdgesCount;
                    }
                }
            }
        }
        public void Add(ProBuilderMesh mesh, IEnumerable <int> indices)
        {
            HashSet <int> indicesHs;
            List <int>    indicesList;
            MeshFilter    vertices;

            m_meshToSelection.TryGetValue(mesh, out vertices);
            m_meshToIndicesList.TryGetValue(mesh, out indicesList);
            if (!m_meshToIndices.TryGetValue(mesh, out indicesHs))
            {
                vertices = CreateVerticesGameObject(mesh, null);
                //vertices.transform.SetParent(transform, false);
                vertices.transform.SetParent(mesh.transform, false);

                indicesHs   = new HashSet <int>();
                indicesList = new List <int>();

                m_meshToSelection.Add(mesh, vertices);
                m_meshToIndices.Add(mesh, indicesHs);
                m_meshToIndicesList.Add(mesh, indicesList);
                m_meshes.Add(mesh);

                PBMesh pbMesh = mesh.GetComponent <PBMesh>();
                if (pbMesh != null)
                {
                    pbMesh.RaiseSelected(false);
                }
                m_pbMeshes.Add(pbMesh);
            }

            int[] notSelectedIndices = indices.Where(i => !indicesHs.Contains(i)).ToArray();
            SetVerticesColor(mesh, vertices, m_selectedColor, notSelectedIndices);
            for (int i = 0; i < notSelectedIndices.Length; ++i)
            {
                indicesHs.Add(notSelectedIndices[i]);
                indicesList.Add(notSelectedIndices[i]);
            }

            if (notSelectedIndices.Length > 0)
            {
                Vertex[] notSelectedVertices = mesh.GetVertices(notSelectedIndices);
                m_lastPosition = notSelectedVertices.Last().position;
                m_lastNormal   = notSelectedVertices.Last().normal;

                for (int i = 0; i < notSelectedIndices.Length; i++)
                {
                    m_selectedVerticesCount++;
                    if (m_selectedVerticesCount == 1)
                    {
                        m_centerOfMass = mesh.transform.TransformPoint(notSelectedVertices[i].position);
                    }
                    else
                    {
                        m_centerOfMass *= (m_selectedVerticesCount - 1) / (float)m_selectedVerticesCount;
                        m_centerOfMass += mesh.transform.TransformPoint(notSelectedVertices[i].position) / m_selectedVerticesCount;
                    }
                }
            }
        }
        public void Add(ProBuilderMesh mesh, int faceIndex)
        {
            Face face = mesh.faces[faceIndex];

            Dictionary <int, Face> faceToSelection;

            if (m_faceToSelectionFace.TryGetValue(mesh, out faceToSelection))
            {
                if (faceToSelection.ContainsKey(faceIndex))
                {
                    return;
                }
            }
            else
            {
                faceToSelection = new Dictionary <int, Face>();
                m_faceToSelectionFace.Add(mesh, faceToSelection);
            }


            int[] indices = new int[face.indexes.Count];
            for (int i = 0; i < indices.Length; ++i)
            {
                indices[i] = m_selectionVertices.Count + i;
            }

            Face selectionFace = new Face(indices);

            faceToSelection.Add(faceIndex, selectionFace);
            m_selectionFaceToMesh.Add(selectionFace, mesh);

            IList <int> indexes = face.indexes;

            Vertex[] vertices = mesh.GetVertices(indexes);
            for (int i = 0; i < vertices.Length; ++i)
            {
                m_selectionVertices.Add(transform.InverseTransformPoint(mesh.transform.TransformPoint(vertices[i].position)));
            }

            m_selectionFaces.Add(selectionFace);
            if (!m_isChanging)
            {
                RebuildSelectionMesh();
            }

            FaceList faceList;

            if (!m_meshToFaces.TryGetValue(mesh, out faceList))
            {
                faceList = new FaceList();
                m_meshToFaces.Add(mesh, faceList);

                PBMesh pbMesh = mesh.GetComponent <PBMesh>();
                if (pbMesh != null)
                {
                    pbMesh.RaiseSelected(false);
                }
                m_pbMeshes.Add(pbMesh);
            }

            for (int i = 0; i < indexes.Count; ++i)
            {
                int index = indexes[i];
                if (!faceList.Indexes.ContainsKey(index))
                {
                    faceList.Indexes.Add(index, 1);
                }
                else
                {
                    faceList.Indexes[index]++;
                }
            }

            faceList.Faces.Add(faceIndex);
            faceList.FaceIndexes.Add(face.indexes.ToArray());
            faceList.SelectionFaces.Add(selectionFace);

            m_lastMesh     = mesh;
            m_lastPosition = GetCenterOfMass(selectionFace);
            m_lastNormal   = GetNormal(selectionFace);

            if (m_selectionFaces.Count == 1)
            {
                m_centerOfMass = m_lastPosition;
            }
            else
            {
                m_centerOfMass *= (m_selectionFaces.Count - 1) / (float)m_selectionFaces.Count;
                m_centerOfMass += m_lastPosition / m_selectionFaces.Count;
            }
        }