public void UpdateDrag() { var selectAllSurfaces = UnityEngine.Event.current.shift; s_TempChiselBrushMaterials.Clear(); s_TempNodes.Clear(); ChiselClickSelectionManager.FindBrushMaterials(Event.current.mousePosition, s_TempChiselBrushMaterials, s_TempNodes, selectAllSurfaces); var surfaces = s_TempChiselBrushMaterials.ToArray(); s_TempChiselBrushMaterials.Clear(); if (!Equals(prevBrushMaterials, surfaces)) { UndoPrevSurface(); // Handle situation where we're hovering over a selected surface, then apply to all selected surfaces if (!selectAllSurfaces) { surfaces = AddSelectedSurfaces(surfaces); } ApplyMaterialToSurface(s_TempNodes.ToArray(), surfaces); } s_TempChiselBrushMaterials.Clear(); s_TempNodes.Clear(); }
// TODO: make selecting variants work when selecting in hierarchy/rect-select too public static void DoSelectionClick(SceneView sceneView, Vector2 mousePosition) { ChiselIntersection intersection; var gameobject = ChiselClickSelectionManager.PickClosestGameObject(mousePosition, out intersection); // If we're a child of an operation that has a "handle as one" flag set, return that instead gameobject = ChiselSceneQuery.FindSelectionBase(gameobject); var selectionType = GetCurrentSelectionType(); var selectedObjectsOnClick = new List <int>(Selection.instanceIDs); switch (selectionType) { case SelectionType.Additive: { if (!gameobject) { break; } ChiselSyncSelection.SelectBrushVariant(intersection.brushIntersection.brush, uniqueSelection: false); var instanceID = gameobject.GetInstanceID(); selectedObjectsOnClick.Add(instanceID); ChiselClickSelectionManager.ignoreSelectionChanged = true; Selection.instanceIDs = selectedObjectsOnClick.ToArray(); break; } case SelectionType.Subtractive: { if (!gameobject) { break; } Undo.RecordObject(ChiselSyncSelection.Instance, "Deselected brush variant"); ChiselSyncSelection.DeselectBrushVariant(intersection.brushIntersection.brush); // Can only deselect brush if all it's synchronized brushes have also been deselected if (!ChiselSyncSelection.IsAnyBrushVariantSelected(intersection.brushIntersection.brush)) { var instanceID = gameobject.GetInstanceID(); selectedObjectsOnClick.Remove(instanceID); } ChiselClickSelectionManager.ignoreSelectionChanged = true; Selection.instanceIDs = selectedObjectsOnClick.ToArray(); return; } default: { Undo.RecordObject(ChiselSyncSelection.Instance, "Selected brush variant"); ChiselSyncSelection.SelectBrushVariant(intersection.brushIntersection.brush, uniqueSelection: true); ChiselClickSelectionManager.ignoreSelectionChanged = true; Selection.activeGameObject = gameobject; break; } } }
static void Commit(Event evt, Rect dragArea, ref List <Vector3> points) { s_CurrentPointIndex++; UpdatePoints(points, GetPointAtPosition(evt.mousePosition, dragArea)); // reset the starting position s_StartIntersection = ChiselClickSelectionManager.GetPlaneIntersection(evt.mousePosition, dragArea); evt.Use(); }
static void Commit(Event evt, Rect dragArea, ref List <Vector3> points) { var newPoint = GetPointAtPosition(s_MousePosition, dragArea); if (!newPoint.HasValue) { Cancel(evt, ref points); return; } s_CurrentPointIndex++; UpdatePoints(points, newPoint); // reset the starting position s_StartIntersection = ChiselClickSelectionManager.GetPlaneIntersection(s_MousePosition, dragArea); evt.Use(); }
public static PlaneIntersection GetPlaneIntersection(Vector2 mousePosition) { CSGTreeBrushIntersection brushIntersection; var intersectionObject = ChiselClickSelectionManager.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 ChiselModel; 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 (ChiselClickSelectionManager.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); }
public void UpdateDrag() { var selectAllSurfaces = UnityEngine.Event.current.shift; ChiselBrushContainerAsset[] brushContainerAssets; ChiselBrushMaterial[] surfaces; ChiselClickSelectionManager.FindBrushMaterials(Event.current.mousePosition, out surfaces, out brushContainerAssets, selectAllSurfaces); if (!Equals(prevBrushMaterials, surfaces)) { UndoPrevSurface(); // Handle situation where we're hovering over a selected surface, then apply to all selected surfaces if (!selectAllSurfaces) { surfaces = AddSelectedSurfaces(surfaces); } ApplyMaterialToSurface(brushContainerAssets, surfaces); } }
static PlaneIntersection GetPlaneIntersection(Vector2 mousePosition) { ChiselIntersection brushIntersection; var intersectionObject = ChiselClickSelectionManager.PickClosestGameObject(mousePosition, out brushIntersection); if (intersectionObject && intersectionObject.activeInHierarchy) { if (brushIntersection.node != null) { return(new PlaneIntersection(brushIntersection)); } if (intersectionObject.TryGetComponent <MeshFilter>(out var meshFilter)) { var mesh = meshFilter.sharedMesh; var mouseRay = UnityEditor.HandleUtility.GUIPointToWorldRay(mousePosition); RaycastHit hit; if (ChiselClickSelectionManager.IntersectRayMesh(mouseRay, mesh, intersectionObject.transform.localToWorldMatrix, out hit)) { if (intersectionObject.TryGetComponent <MeshRenderer>(out var meshRenderer) && 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.SignedRaycast(mouseRay, out dist)) { return(new PlaneIntersection(mouseRay.GetPoint(dist), gridPlane)); } } return(null); }
static bool UpdateHoverSurfaces(Vector2 mousePosition, Rect dragArea, SelectionType selectionType, bool clearHovering) { try { hoverIntersection = null; hoverSurfaceReference = null; bool modified = false; if (clearHovering || !InEditCameraMode) { if (hoverSurfaces.Count != 0) { hoverSurfaces.Clear(); modified = true; } } if (!dragArea.Contains(mousePosition)) { return(modified); } if (!InEditCameraMode) { return(modified); } CSGTreeBrushIntersection intersection; SurfaceReference surfaceReference; var foundSurfaces = ChiselClickSelectionManager.FindSurfaceReference(mousePosition, false, out intersection, out surfaceReference); if (foundSurfaces == null) { modified = (hoverSurfaces != null) || modified; hoverIntersection = null; return(modified); } if (!float.IsInfinity(intersection.surfaceIntersection.distance)) { intersection.surfaceIntersection.worldIntersection = SnapIntersection(intersection.surfaceIntersection.worldIntersection, surfaceReference, out pointHasSnapped); } hoverIntersection = intersection; hoverSurfaceReference = surfaceReference; if (foundSurfaces.Length == hoverSurfaces.Count) { modified = !hoverSurfaces.ContainsAll(foundSurfaces) || modified; } else { modified = true; } if (foundSurfaces.Length > 0) { hoverSurfaces.AddRange(foundSurfaces); } return(modified); } finally { ChiselSurfaceSelectionManager.SetHovering(selectionType, hoverSurfaces); } }
static Vector3?GetPointAtPosition(Vector2 mousePosition, Rect dragArea) { UnitySceneExtensions.Grid.HoverGrid = null; if (s_CurrentPointIndex == 0) { s_StartIntersection = ChiselClickSelectionManager.GetPlaneIntersection(mousePosition, dragArea); if (s_StartIntersection != null) { // TODO: try to cache this .. var activeGridUp = UnitySceneExtensions.Grid.ActiveGrid.Up; var activeGridForward = UnitySceneExtensions.Grid.ActiveGrid.Forward; var activeGridCenter = UnitySceneExtensions.Grid.ActiveGrid.Center; var surfaceGridPlane = s_StartIntersection.plane; var surfaceGridUp = surfaceGridPlane.normal; var surfaceGridForward = MathExtensions.CalculateBinormal(surfaceGridUp); var activeGridFromWorldRotation = Quaternion.LookRotation(activeGridUp, activeGridForward); var worldFromActiveGridRotation = Quaternion.Inverse(activeGridFromWorldRotation); var surfaceGridFromWorldRotation = Quaternion.LookRotation(surfaceGridUp, surfaceGridForward); var activeGridToSurfaceGridRotation = surfaceGridFromWorldRotation * worldFromActiveGridRotation; // Make sure the center of the new grid is as close to the active grid center as possible Vector3 surfaceGridCenter = activeGridCenter; var forwardRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.up); var backRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.down); var leftRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.left); var rightRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.right); var upRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.forward); var downRay = new Ray(activeGridCenter, worldFromActiveGridRotation * Vector3.back); var bestDist = float.PositiveInfinity; float dist; if (surfaceGridPlane.SignedRaycast(forwardRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = forwardRay.GetPoint(dist); } } if (surfaceGridPlane.SignedRaycast(backRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = backRay.GetPoint(dist); } } if (surfaceGridPlane.SignedRaycast(leftRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = leftRay.GetPoint(dist); } } if (surfaceGridPlane.SignedRaycast(rightRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = rightRay.GetPoint(dist); } } if (bestDist > 100000) // prefer rays on the active-grid, only go up/down from the active-grid when we have no other choice { if (surfaceGridPlane.SignedRaycast(upRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = upRay.GetPoint(dist); } } if (surfaceGridPlane.SignedRaycast(downRay, out dist)) { var abs_dist = Mathf.Abs(dist); if (abs_dist < bestDist) { bestDist = abs_dist; surfaceGridCenter = downRay.GetPoint(dist); } } } // TODO: try to snap the new surface grid point in other directions on the active-grid? (do we need to?) s_Transform = Matrix4x4.TRS(surfaceGridCenter - activeGridCenter, activeGridToSurfaceGridRotation, Vector3.one) * UnitySceneExtensions.Grid.ActiveGrid.GridToWorldSpace; s_InvTransform = s_Transform.inverse; s_Snapping2D.Initialize(new UnitySceneExtensions.Grid(s_Transform), mousePosition, s_StartIntersection.point, UnityEditor.Handles.matrix); } } if (s_StartIntersection != null) { if (s_Snapping2D.DragTo(mousePosition, SnappingMode.Always)) { UnitySceneExtensions.Grid.HoverGrid = s_Snapping2D.WorldSlideGrid; return(s_Snapping2D.WorldSnappedPosition); } } return(null); }