Esempio n. 1
0
        public override MeshSelection Select(Camera camera, Rect rect, GameObject[] gameObjects, MeshEditorSelectionMode mode)
        {
            Dictionary <ProBuilderMesh, HashSet <Edge> > pickResult = PBUtility.PickEdges(camera, rect, gameObjects);

            if (pickResult.Count == 0)
            {
                return(null);
            }

            MeshSelection selection = new MeshSelection();

            foreach (KeyValuePair <ProBuilderMesh, HashSet <Edge> > kvp in pickResult)
            {
                ProBuilderMesh mesh = kvp.Key;

                m_edgeSelection.FindCoincidentEdges(mesh);
                IList <Edge> edges = m_edgeSelection.GetCoincidentEdges(kvp.Value);

                IList <Edge> selected    = edges.Where(edge => m_edgeSelection.IsSelected(mesh, edge)).ToArray();
                IList <Edge> notSelected = edges.Where(edge => !m_edgeSelection.IsSelected(mesh, edge)).ToArray();

                if (mode == MeshEditorSelectionMode.Substract || mode == MeshEditorSelectionMode.Difference)
                {
                    selection.UnselectedEdges.Add(mesh, selected);
                    m_edgeSelection.Remove(mesh, selected);
                }

                if (mode == MeshEditorSelectionMode.Add || mode == MeshEditorSelectionMode.Difference)
                {
                    selection.SelectedEdges.Add(mesh, notSelected);
                    m_edgeSelection.Add(mesh, notSelected);
                }
            }
            return(selection);
        }
Esempio n. 2
0
        public override void Hover(Camera camera, Vector3 pointer)
        {
            if (m_nextUpdate > Time.time)
            {
                return;
            }

            m_nextUpdate = Time.time + 0.3f;

            if (m_vertexSelection.MeshesCount == 0)
            {
                return;
            }

            float result = PBUtility.PickVertex(camera, pointer, 20, null, m_vertexSelection.Meshes, ref m_selection);

            if (result != Mathf.Infinity)
            {
                m_vertexSelection.Hover(m_selection.mesh, m_selection.vertex);
            }
            else
            {
                m_vertexSelection.Leave();
            }
        }
        public override MeshSelection Select(Camera camera, Vector3 pointer, bool shift, bool ctrl, bool depthTest)
        {
            MeshSelection selection    = null;
            GameObject    pickedObject = PBUtility.PickObject(camera, pointer);
            float         result       = PBUtility.PickVertex(camera, pointer, 20, pickedObject, m_vertexSelection.Meshes, depthTest, ref m_selection);

            if (result != Mathf.Infinity)
            {
                if (m_vertexSelection.IsSelected(m_selection.mesh, m_selection.vertex))
                {
                    if (shift)
                    {
                        List <int> indices = m_selection.mesh.GetCoincidentVertices(new[] { m_selection.vertex });
                        m_vertexSelection.Remove(m_selection.mesh, indices);
                        selection = new MeshSelection();
                        selection.UnselectedIndices.Add(m_selection.mesh.gameObject, indices);
                    }
                    else
                    {
                        List <int> indices = m_selection.mesh.GetCoincidentVertices(new[] { m_selection.vertex });

                        selection = ReadSelection();
                        selection.UnselectedIndices[m_selection.mesh.gameObject] = selection.UnselectedIndices[m_selection.mesh.gameObject].Where(i => i != m_selection.vertex).ToArray();
                        selection.SelectedIndices.Add(m_selection.mesh.gameObject, indices);
                        m_vertexSelection.Clear();
                        m_vertexSelection.Add(m_selection.mesh, indices);
                    }
                }
                else
                {
                    if (shift)
                    {
                        selection = new MeshSelection();
                    }
                    else
                    {
                        selection = ReadSelection();
                        m_vertexSelection.Clear();
                    }

                    List <int> indices = m_selection.mesh.GetCoincidentVertices(new[] { m_selection.vertex });

                    m_vertexSelection.Add(m_selection.mesh, indices);
                    selection.SelectedIndices.Add(m_selection.mesh.gameObject, indices);
                }
            }
            else
            {
                if (!shift)
                {
                    selection = ReadSelection();
                    if (selection.UnselectedIndices.Count == 0)
                    {
                        selection = null;
                    }
                    m_vertexSelection.Clear();
                }
            }
            return(selection);
        }
 private void BuildVertexMesh(ProBuilderMesh mesh, Mesh target, IList <int> indexes)
 {
     if (IsGeometryShadersSupported)
     {
         PBUtility.BuildVertexMesh(mesh.positions, m_color, target, indexes);
     }
     else
     {
         PBUtility.BuildVertexMeshLegacy(mesh.positions, m_color, target, indexes);
     }
 }
Esempio n. 5
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 override MeshSelection Select(Camera camera, Rect rect, Rect uiRootRect, GameObject[] gameObjects, bool depthTest, MeshEditorSelectionMode mode)
        {
            Dictionary <ProBuilderMesh, HashSet <int> > pickResult = PBUtility.PickVertices(camera, rect, uiRootRect, gameObjects, depthTest);

            if (pickResult.Count == 0)
            {
                return(null);
            }

            MeshSelection selection = new MeshSelection();

            foreach (KeyValuePair <ProBuilderMesh, HashSet <int> > kvp in pickResult)
            {
                ProBuilderMesh       mesh           = kvp.Key;
                HashSet <int>        sharedIndexes  = kvp.Value;
                IList <SharedVertex> sharedVertices = mesh.sharedVertices;

                HashSet <int> indices = new HashSet <int>();
                foreach (int sharedIndex in sharedIndexes)
                {
                    SharedVertex sharedVertex = sharedVertices[sharedIndex];
                    for (int j = 0; j < sharedVertex.Count; ++j)
                    {
                        if (!indices.Contains(sharedVertex[j]))
                        {
                            indices.Add(sharedVertex[j]);
                        }
                    }
                }

                if (mode == MeshEditorSelectionMode.Substract || mode == MeshEditorSelectionMode.Difference)
                {
                    IList <int> selected = mesh.GetCoincidentVertices(indices).Where(index => m_vertexSelection.IsSelected(mesh, index)).ToArray();
                    selection.UnselectedIndices.Add(mesh.gameObject, selected);
                    m_vertexSelection.Remove(mesh, selected);
                }

                if (mode == MeshEditorSelectionMode.Add || mode == MeshEditorSelectionMode.Difference)
                {
                    IList <int> notSelected = mesh.GetCoincidentVertices(indices).Where(index => !m_vertexSelection.IsSelected(mesh, index)).ToArray();
                    selection.SelectedIndices.Add(mesh.gameObject, notSelected);
                    m_vertexSelection.Add(mesh, notSelected);
                }
            }
            return(selection);
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public bool Click(Camera camera, Vector3 pointer)
        {
            if (!m_isEditing)
            {
                return(false);
            }

            SceneSelection selection = new SceneSelection();
            float          result    = PBUtility.PickVertex(camera, pointer, 20, m_selection.Transform, m_selection.Positions, ref selection);

            if (result != Mathf.Infinity)
            {
                if (m_selection.Positions.Count >= 3)
                {
                    m_selection.Unselect();
                    m_selection.Select(selection.vertex);
                    return(true);
                }
            }
            else
            {
                if (Stage == 0)
                {
                    Ray   ray = camera.ScreenPointToRay(pointer);
                    float enter;

                    Plane plane = new Plane(m_selection.transform.up, m_selection.transform.position);
                    if (plane.Raycast(ray, out enter))
                    {
                        Vector3 position = ray.GetPoint(enter);
                        position = m_selection.Transform.InverseTransformPoint(position);
                        m_selection.Add(position);
                        m_positions.Add(position);
                    }

                    m_target.CreateShapeFromPolygon(m_selection.Positions, 0.001f, false);
                }
            }

            return(false);
        }
Esempio n. 9
0
        public override MeshSelection Select(Camera camera, Vector3 pointer, bool shift)
        {
            MeshSelection selection    = null;
            GameObject    pickedObject = PBUtility.PickObject(camera, pointer);
            float         result       = PBUtility.PickEdge(camera, pointer, 20, pickedObject, m_edgeSelection.Meshes, ref m_selection);

            if (result != Mathf.Infinity)
            {
                if (m_edgeSelection.IsSelected(m_selection.mesh, m_selection.edge))
                {
                    if (shift)
                    {
                        m_edgeSelection.FindCoincidentEdges(m_selection.mesh);

                        IList <Edge> edges = m_edgeSelection.GetCoincidentEdges(new[] { m_selection.edge });
                        m_edgeSelection.Remove(m_selection.mesh, edges);
                        selection = new MeshSelection();
                        selection.UnselectedEdges.Add(m_selection.mesh, edges);
                    }
                    else
                    {
                        m_edgeSelection.FindCoincidentEdges(m_selection.mesh);

                        IList <Edge> edges = m_edgeSelection.GetCoincidentEdges(new[] { m_selection.edge });
                        selection = ReadSelection();
                        selection.UnselectedEdges[m_selection.mesh] = selection.UnselectedEdges[m_selection.mesh].Where(e => e != m_selection.edge).ToArray();
                        selection.SelectedEdges.Add(m_selection.mesh, edges);
                        m_edgeSelection.Clear();
                        m_edgeSelection.Add(m_selection.mesh, edges);
                    }
                }
                else
                {
                    if (shift)
                    {
                        selection = new MeshSelection();
                    }
                    else
                    {
                        selection = ReadSelection();
                        m_edgeSelection.Clear();
                    }

                    m_edgeSelection.FindCoincidentEdges(m_selection.mesh);

                    IList <Edge> edges = m_edgeSelection.GetCoincidentEdges(new[] { m_selection.edge });
                    m_edgeSelection.Add(m_selection.mesh, edges);
                    selection.SelectedEdges.Add(m_selection.mesh, edges);
                }
            }
            else
            {
                if (!shift)
                {
                    selection = ReadSelection();
                    if (selection.UnselectedEdges.Count == 0)
                    {
                        selection = null;
                    }
                    m_edgeSelection.Clear();
                }
            }
            return(selection);
        }
Esempio n. 10
0
        public override MeshSelection Select(Camera camera, Rect rect, GameObject[] gameObjects, MeshEditorSelectionMode mode)
        {
            MeshSelection selection = new MeshSelection();

            m_faceSelection.BeginChange();

            Dictionary <ProBuilderMesh, HashSet <Face> > result = PBUtility.PickFaces(camera, rect, gameObjects);

            if (mode == MeshEditorSelectionMode.Add)
            {
                foreach (KeyValuePair <ProBuilderMesh, HashSet <Face> > kvp in result)
                {
                    ProBuilderMesh mesh        = kvp.Key;
                    IList <Face>   faces       = mesh.faces;
                    IList <int>    faceIndices = kvp.Value.Select(f => faces.IndexOf(f)).ToArray();
                    IList <int>    notSelected = faceIndices.Where(f => !m_faceSelection.IsSelected(f)).ToArray();
                    foreach (int face in notSelected)
                    {
                        m_faceSelection.Add(mesh, face);
                    }

                    selection.SelectedFaces.Add(mesh, notSelected);
                }
            }
            else if (mode == MeshEditorSelectionMode.Substract)
            {
                foreach (KeyValuePair <ProBuilderMesh, HashSet <Face> > kvp in result)
                {
                    ProBuilderMesh mesh        = kvp.Key;
                    IList <Face>   faces       = mesh.faces;
                    IList <int>    faceIndices = kvp.Value.Select(f => faces.IndexOf(f)).ToArray();
                    IList <int>    selected    = faceIndices.Where(f => m_faceSelection.IsSelected(f)).ToArray();
                    foreach (int face in selected)
                    {
                        m_faceSelection.Remove(face);
                    }
                    selection.UnselectedFaces.Add(mesh, selected);
                }
            }
            else if (mode == MeshEditorSelectionMode.Difference)
            {
                foreach (KeyValuePair <ProBuilderMesh, HashSet <Face> > kvp in result)
                {
                    ProBuilderMesh mesh        = kvp.Key;
                    IList <Face>   faces       = mesh.faces;
                    IList <int>    faceIndices = kvp.Value.Select(f => faces.IndexOf(f)).ToArray();

                    IList <int> selected    = faceIndices.Where(f => m_faceSelection.IsSelected(f)).ToArray();
                    IList <int> notSelected = faceIndices.Where(f => !m_faceSelection.IsSelected(f)).ToArray();

                    foreach (int face in selected)
                    {
                        m_faceSelection.Remove(face);
                    }

                    foreach (int face in notSelected)
                    {
                        m_faceSelection.Add(mesh, face);
                    }

                    selection.UnselectedFaces.Add(mesh, selected);
                    selection.SelectedFaces.Add(mesh, notSelected);
                }
            }

            m_faceSelection.EndChange();

            if (selection.SelectedFaces.Count == 0 && selection.UnselectedFaces.Count == 0)
            {
                selection = null;
            }

            return(selection);
        }
        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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
 private void BuildVertexMesh(IList <Vector3> positions, Mesh target)
 {
     PBUtility.BuildVertexMesh(positions, m_vertexColor, target);
 }
        public static Dictionary <ProBuilderMesh, HashSet <Edge> > PickEdgesInRect(
            Camera cam,
            Rect rect,
            Rect uiRootRect,
            IList <ProBuilderMesh> selectable,
            PickerOptions options,
            float pixelsPerPoint = 1f)
        {
            if (/*options.depthTest &&*/ options.rectSelectMode == RectSelectMode.Partial)
            {
                return(m_renderer.PickEdgesInRect(
                           cam,
                           rect,
                           selectable,
                           options.depthTest,
                           (int)(uiRootRect.width / pixelsPerPoint),
                           (int)(uiRootRect.height / pixelsPerPoint)));
            }

            var selected = new Dictionary <ProBuilderMesh, HashSet <Edge> >();

            foreach (var pb in selectable)
            {
                if (!pb.selectable)
                {
                    continue;
                }

                Transform trs           = pb.transform;
                var       selectedEdges = new HashSet <Edge>();

                IList <Face>    faces     = pb.faces;
                IList <Vector3> positions = pb.positions;
                for (int i = 0, fc = pb.faceCount; i < fc; i++)
                {
                    var edges = faces[i].edges;

                    for (int n = 0, ec = edges.Count; n < ec; n++)
                    {
                        var edge = edges[n];

                        var posA = trs.TransformPoint(positions[edge.a]);
                        var posB = trs.TransformPoint(positions[edge.b]);

                        Vector3 a = ScreenToGuiPoint(uiRootRect, cam.WorldToScreenPoint(posA), pixelsPerPoint);
                        Vector3 b = ScreenToGuiPoint(uiRootRect, cam.WorldToScreenPoint(posB), pixelsPerPoint);

                        switch (options.rectSelectMode)
                        {
                        case RectSelectMode.Complete:
                        {
                            // if either of the positions are clipped by the camera we cannot possibly select both, skip it
                            if ((a.z < cam.nearClipPlane || b.z < cam.nearClipPlane))
                            {
                                continue;
                            }

                            if (rect.Contains(a) && rect.Contains(b))
                            {
                                if (!options.depthTest || !PBUtility.PointIsOccluded(cam, pb, (posA + posB) * .5f))
                                {
                                    selectedEdges.Add(edge);
                                }
                            }

                            break;
                        }

                        case RectSelectMode.Partial:
                        {
                            // partial + depth test is covered earlier
                            if (RectIntersectsLineSegment(rect, a, b))
                            {
                                selectedEdges.Add(edge);
                            }

                            break;
                        }
                        }
                    }
                }

                selected.Add(pb, selectedEdges);
            }

            return(selected);
        }