Beispiel #1
0
        public override MeshSelection Select(Camera camera, Vector3 pointer, bool shift)
        {
            MeshSelection selection = null;
            MeshAndFace   result    = PBUtility.PickFace(camera, pointer);

            if (result.face != null)
            {
                int faceIndex = result.mesh.faces.IndexOf(result.face);
                if (m_faceSelection.IsSelected(faceIndex))
                {
                    if (shift)
                    {
                        m_faceSelection.Remove(faceIndex);
                        selection = new MeshSelection();
                        selection.UnselectedFaces.Add(result.mesh, new[] { faceIndex });
                    }
                    else
                    {
                        selection = ReadSelection();
                        selection = null;
                    }
                }
                else
                {
                    if (shift)
                    {
                        selection = new MeshSelection();
                    }
                    else
                    {
                        selection = ReadSelection();
                        m_faceSelection.Clear();
                    }

                    m_faceSelection.Add(result.mesh, faceIndex);
                    selection.SelectedFaces.Add(result.mesh, new[] { faceIndex });
                }
            }
            else
            {
                if (!shift)
                {
                    selection = ReadSelection();
                    if (selection.UnselectedFaces.Count == 0)
                    {
                        selection = null;
                    }
                    m_faceSelection.Clear();
                }
            }
            return(selection);
        }
        public static MeshAndFace PickFace(Camera camera, Vector3 mousePosition)
        {
            MeshAndFace res = new MeshAndFace();
            GameObject  go  = PickObject(camera, mousePosition);

            if (go == null || !(res.mesh = go.GetComponent <ProBuilderMesh>()))
            {
                return(res);
            }

            res.face = SelectionPicker.PickFace(camera, mousePosition, res.mesh);
            return(res);
        }
Beispiel #3
0
        public ApplyMaterialResult ApplyMaterial(Material material, MeshSelection selection, Camera camera, Vector3 mousePosition)
        {
            MeshAndFace meshAndFace = PBUtility.PickFace(camera, mousePosition);

            if (meshAndFace.mesh != null && meshAndFace.face != null)
            {
                MeshMaterialsState oldState = new MeshMaterialsState();
                MeshMaterialsState newState = new MeshMaterialsState();

                int faceIndex = meshAndFace.mesh.faces.IndexOf(meshAndFace.face);

                IList <int> faceIndexes;
                if (selection != null && selection.SelectedFaces.TryGetValue(meshAndFace.mesh.gameObject, out faceIndexes))
                {
                    if (faceIndexes.Contains(faceIndex))
                    {
                        return(ApplyMaterial(material, selection));
                    }
                }

                AddAllFacesToState(oldState, meshAndFace.mesh);
                AddMaterialsToState(oldState, meshAndFace.mesh);

                meshAndFace.mesh.SetMaterial(new[] { meshAndFace.face }, material);
                meshAndFace.mesh.Refresh();
                meshAndFace.mesh.ToMesh();

                RemoveUnusedMaterials(meshAndFace.mesh);
                meshAndFace.mesh.Refresh();
                meshAndFace.mesh.ToMesh();

                FaceToSubmeshIndex newFaceToIndex = new FaceToSubmeshIndex(meshAndFace.mesh, meshAndFace.mesh.faces.IndexOf(meshAndFace.face), meshAndFace.face.submeshIndex);
                newState.FaceToSubmeshIndex.Add(newFaceToIndex);
                AddMaterialsToState(newState, meshAndFace.mesh);

                if (MaterialsApplied != null)
                {
                    MaterialsApplied();
                }

                return(new ApplyMaterialResult(oldState, newState));
            }
            return(null);
        }
        public override MeshSelection Select(Camera camera, Vector3 pointer, bool shift)
        {
            MeshSelection selection = null;
            MeshAndFace   result    = PBUtility.PickFace(camera, pointer);

            if (result.face != null)
            {
                if (m_faceSelection.IsSelected(result.face))
                {
                    if (shift)
                    {
                        m_faceSelection.Remove(result.face);
                        selection = new MeshSelection();
                        selection.UnselectedFaces.Add(result.mesh, new[] { result.face });
                    }
                    else
                    {
                        selection = ReadSelection();
                        //if(selection.SelectedFaces.Count > 1)
                        //{
                        //    selection.UnselectedFaces[result.mesh] = selection.UnselectedFaces[result.mesh].Where(f => f != result.face).ToArray();
                        //    selection.SelectedFaces.Add(result.mesh, new[] { result.face });
                        //    m_faceSelection.Clear();
                        //    m_faceSelection.Add(result.mesh, result.face);
                        //}
                        //else
                        {
                            selection = null;
                        }
                    }
                }
                else
                {
                    if (shift)
                    {
                        selection = new MeshSelection();
                    }
                    else
                    {
                        selection = ReadSelection();
                        m_faceSelection.Clear();
                    }

                    m_faceSelection.Add(result.mesh, result.face);
                    selection.SelectedFaces.Add(result.mesh, new[] { result.face });
                }
            }
            else
            {
                if (!shift)
                {
                    selection = ReadSelection();
                    if (selection.UnselectedFaces.Count == 0)
                    {
                        selection = null;
                    }
                    m_faceSelection.Clear();
                }
            }
            return(selection);
        }
Beispiel #5
0
        public override MeshSelection Select(Camera camera, Vector3 pointer, bool shift, bool ctrl)
        {
            MeshSelection selection = null;
            MeshAndFace   result    = PBUtility.PickFace(camera, pointer);

            if (result.face != null)
            {
                if (ctrl)
                {
                    int          submeshIndex      = result.face.submeshIndex;
                    IList <Face> faces             = result.mesh.faces;
                    List <int>   sameMaterialFaces = new List <int>();

                    bool wasSelected   = false;
                    bool wasUnselected = false;

                    m_faceSelection.BeginChange();
                    for (int i = 0; i < faces.Count; ++i)
                    {
                        Face face = faces[i];
                        if (face.submeshIndex == submeshIndex)
                        {
                            sameMaterialFaces.Add(i);

                            if (!m_faceSelection.IsSelected(result.mesh, i))
                            {
                                m_faceSelection.Add(result.mesh, i);
                                wasUnselected = true;
                            }
                            else
                            {
                                wasSelected = true;
                            }
                        }
                    }

                    if (wasSelected && !wasUnselected)
                    {
                        for (int i = 0; i < sameMaterialFaces.Count; ++i)
                        {
                            m_faceSelection.Remove(result.mesh, sameMaterialFaces[i]);
                        }

                        selection = new MeshSelection();
                        selection.UnselectedFaces.Add(result.mesh, sameMaterialFaces.ToArray());
                    }
                    else
                    {
                        selection = new MeshSelection();
                        selection.SelectedFaces.Add(result.mesh, sameMaterialFaces.ToArray());
                    }

                    m_faceSelection.EndChange();
                }
                else
                {
                    int faceIndex = result.mesh.faces.IndexOf(result.face);
                    if (m_faceSelection.IsSelected(result.mesh, faceIndex))
                    {
                        if (shift)
                        {
                            m_faceSelection.Remove(result.mesh, faceIndex);
                            selection = new MeshSelection();
                            selection.UnselectedFaces.Add(result.mesh, new[] { faceIndex });
                        }
                        else
                        {
                            //selection = ReadSelection();
                            selection = null;
                        }
                    }
                    else
                    {
                        if (shift)
                        {
                            selection = new MeshSelection();
                        }
                        else
                        {
                            selection = ReadSelection();
                            m_faceSelection.Clear();
                        }

                        m_faceSelection.Add(result.mesh, faceIndex);
                        selection.SelectedFaces.Add(result.mesh, new[] { faceIndex });
                    }
                }
            }
            else
            {
                if (!shift)
                {
                    selection = ReadSelection();
                    if (selection.UnselectedFaces.Count == 0)
                    {
                        selection = null;
                    }
                    m_faceSelection.Clear();
                }
            }
            return(selection);
        }