Exemple #1
0
        // TODO: make selecting variants work when selecting in hierarchy/rect-select too
        public static void DoSelectionClick(SceneView sceneView, Vector2 mousePosition)
        {
            CSGTreeBrushIntersection intersection;
            var gameobject = CSGClickSelectionManager.PickClosestGameObject(Event.current.mousePosition, out intersection);

            // If we're a child of an operation that has a "handle as one" flag set, return that instead
            gameobject = CSGSceneQuery.GetContainerGameObject(gameobject);

            var selectionType = GetCurrentSelectionType();

            var selectedObjectsOnClick = new List <int>(Selection.instanceIDs);

            switch (selectionType)
            {
            case SelectionType.Additive:
            {
                if (!gameobject)
                {
                    break;
                }

                CSGSyncSelection.SelectBrushVariant(intersection.brush, uniqueSelection: false);
                var instanceID = gameobject.GetInstanceID();
                selectedObjectsOnClick.Add(instanceID);
                Selection.instanceIDs = selectedObjectsOnClick.ToArray();
                break;
            }

            case SelectionType.Subtractive:
            {
                if (!gameobject)
                {
                    break;
                }

                Undo.RecordObject(CSGSyncSelection.Instance, "Deselected brush variant");
                CSGSyncSelection.DeselectBrushVariant(intersection.brush);
                // Can only deselect brush if all it's synchronized brushes have also been deselected
                if (!CSGSyncSelection.IsAnyBrushVariantSelected(intersection.brush))
                {
                    var instanceID = gameobject.GetInstanceID();
                    selectedObjectsOnClick.Remove(instanceID);
                }
                Selection.instanceIDs = selectedObjectsOnClick.ToArray();
                return;
            }

            default:
            {
                Undo.RecordObject(CSGSyncSelection.Instance, "Selected brush variant");
                CSGSyncSelection.SelectBrushVariant(intersection.brush, uniqueSelection: true);
                Selection.activeGameObject = gameobject;
                break;
            }
            }
        }
Exemple #2
0
        public static PlaneIntersection GetPlaneIntersection(Vector2 mousePosition)
        {
            CSGTreeBrushIntersection brushIntersection;
            var intersectionObject = CSGClickSelectionManager.PickClosestGameObject(mousePosition, out brushIntersection);

            if (intersectionObject &&
                intersectionObject.activeInHierarchy)
            {
                if (brushIntersection.brushUserID != -1)
                {
                    var brush = CSGNodeHierarchyManager.FindCSGNodeByInstanceID(brushIntersection.brush.UserID);
                    var model = CSGNodeHierarchyManager.FindCSGNodeByInstanceID(brushIntersection.tree.UserID) as CSGModel;
                    return(new PlaneIntersection(brushIntersection, brush, model));
                }

                var meshFilter = intersectionObject.GetComponent <MeshFilter>();
                if (meshFilter)
                {
                    var        mesh     = meshFilter.sharedMesh;
                    var        mouseRay = UnityEditor.HandleUtility.GUIPointToWorldRay(mousePosition);
                    RaycastHit hit;
                    if (CSGClickSelectionManager.IntersectRayMesh(mouseRay, mesh, intersectionObject.transform.localToWorldMatrix, out hit))
                    {
                        var meshRenderer = intersectionObject.GetComponent <MeshRenderer>();
                        if (meshRenderer.enabled)
                        {
                            return(new PlaneIntersection(hit.point, hit.normal));
                        }
                    }
                }
            }
            else
            {
                var gridPlane = UnitySceneExtensions.Grid.ActiveGrid.PlaneXZ;
                var mouseRay  = UnityEditor.HandleUtility.GUIPointToWorldRay(mousePosition);
                var dist      = 0.0f;
                if (gridPlane.UnsignedRaycast(mouseRay, out dist))
                {
                    return(new PlaneIntersection(mouseRay.GetPoint(dist), gridPlane));
                }
            }
            return(null);
        }