static float VertexRaycast(Vector3 mousePosition, ScenePickerPreferences pickerOptions, bool allowUnselected, SceneSelection selection)
        {
            Camera cam = SceneView.lastActiveSceneView.camera;

            selection.Clear();
            s_NearestVertices.Clear();
            selection.gameObject = HandleUtility.PickGameObject(mousePosition, false);
            float          maxDistance = ScenePickerPreferences.maxPointerDistance * ScenePickerPreferences.maxPointerDistance;
            ProBuilderMesh hoveredMesh = selection.gameObject != null?selection.gameObject.GetComponent <ProBuilderMesh>() : null;

            if (allowUnselected && selection.gameObject != null)
            {
                if (hoveredMesh != null && hoveredMesh.selectable && !MeshSelection.Contains(hoveredMesh))
                {
                    GetNearestVertices(hoveredMesh, mousePosition, s_NearestVertices, maxDistance, 1);
                }
            }

            if (selection.mesh == null)
            {
                foreach (var mesh in MeshSelection.topInternal)
                {
                    if (!mesh.selectable)
                    {
                        continue;
                    }

                    GetNearestVertices(mesh, mousePosition, s_NearestVertices, maxDistance, hoveredMesh == mesh || hoveredMesh == null ? 1.0f : ScenePickerPreferences.offPointerMultiplier);
                }
            }

            s_NearestVertices.Sort((x, y) => x.screenDistance.CompareTo(y.screenDistance));

            for (int i = 0; i < s_NearestVertices.Count; i++)
            {
                if (!UnityEngine.ProBuilder.HandleUtility.PointIsOccluded(cam, s_NearestVertices[i].mesh, s_NearestVertices[i].worldPosition))
                {
                    selection.gameObject = s_NearestVertices[i].mesh.gameObject;
                    selection.mesh       = s_NearestVertices[i].mesh;
                    selection.SetSingleVertex(s_NearestVertices[i].vertex);

                    return(Mathf.Sqrt(s_NearestVertices[i].screenDistance));
                }
            }

            return(Mathf.Infinity);
        }
Exemplo n.º 2
0
        static float VertexRaycast(Vector3 mousePosition, ScenePickerPreferences pickerOptions, bool allowUnselected, SceneSelection selection)
        {
            Camera cam = SceneView.lastActiveSceneView.camera;

            selection.Clear();
            s_NearestVertices.Clear();
            selection.gameObject = HandleUtility.PickGameObject(mousePosition, false);
            float maxDistance = pickerOptions.maxPointerDistance * pickerOptions.maxPointerDistance;

            if (allowUnselected && selection.gameObject != null)
            {
                var mesh = selection.gameObject.GetComponent <ProBuilderMesh>();

                if (mesh != null && mesh.selectable && !MeshSelection.Contains(mesh))
                {
                    var matches = GetNearestVertices(mesh, mousePosition, s_NearestVertices, maxDistance);

                    for (int i = 0; i < matches; i++)
                    {
                        // Append `maxDistance` so that selected meshes are favored
                        s_NearestVertices[i] = new VertexPickerEntry()
                        {
                            mesh           = s_NearestVertices[i].mesh,
                            vertex         = s_NearestVertices[i].vertex,
                            screenDistance = s_NearestVertices[i].screenDistance + maxDistance,
                            worldPosition  = s_NearestVertices[i].worldPosition
                        };
                    }
                }
            }

            if (selection.mesh == null)
            {
                foreach (var mesh in MeshSelection.topInternal)
                {
                    if (!mesh.selectable)
                    {
                        continue;
                    }

                    GetNearestVertices(mesh, mousePosition, s_NearestVertices, maxDistance);
                }
            }

            s_NearestVertices.Sort((x, y) => x.screenDistance.CompareTo(y.screenDistance));

            for (int i = 0; i < s_NearestVertices.Count; i++)
            {
                if (!UnityEngine.ProBuilder.HandleUtility.PointIsOccluded(cam, s_NearestVertices[i].mesh, s_NearestVertices[i].worldPosition))
                {
                    selection.gameObject = s_NearestVertices[i].mesh.gameObject;
                    selection.mesh       = s_NearestVertices[i].mesh;
                    selection.vertex     = s_NearestVertices[i].vertex;

                    // If mesh was unselected, remove the distance modifier
                    if (s_NearestVertices[i].screenDistance > maxDistance)
                    {
                        return(Mathf.Sqrt(s_NearestVertices[i].screenDistance - maxDistance));
                    }

                    return(Mathf.Sqrt(s_NearestVertices[i].screenDistance));
                }
            }

            return(Mathf.Infinity);
        }