private PickMode PickTriangle(PickMode pickMode) { if (Camera.current == null) { return(PickMode.Undecided); } Ray pickRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); int hitTriIndex = -1; if (RaycastUtil.Raycast(targetMeshCollider, pickRay, out hitTriIndex, 10000.0f)) { if (currentHullPainter.paintingData.HasActiveHull()) { Hull hull = currentHullPainter.paintingData.GetActiveHull(); if (pickMode == PickMode.Additive) { if (!hull.selectedFaces.Contains(hitTriIndex)) { hull.selectedFaces.Add(hitTriIndex); } PickArea(targetMeshCollider, hull, Event.current.mousePosition, true); return(PickMode.Additive); } else if (pickMode == PickMode.Subtractive) { hull.selectedFaces.Remove(hitTriIndex); PickArea(targetMeshCollider, hull, Event.current.mousePosition, false); return(PickMode.Subtractive); } else if (pickMode == PickMode.Undecided) { if (hull.selectedFaces.Contains(hitTriIndex)) { hull.selectedFaces.Remove(hitTriIndex); PickArea(targetMeshCollider, hull, Event.current.mousePosition, false); return(PickMode.Subtractive); } else { hull.selectedFaces.Add(hitTriIndex); PickArea(targetMeshCollider, hull, Event.current.mousePosition, true); return(PickMode.Additive); } } } } return(PickMode.Undecided); }
private void PickArea(MeshCollider targetMeshCollider, Hull hull, Vector2 clickPos, bool asAdditive) { int range = GetBrushPixelSize(); int numRays = range + 1; for (int i = 0; i < numRays; i++) { Vector2 jitteredPos; if (i == 0) { jitteredPos = clickPos; } else { jitteredPos = new Vector2(clickPos.x + Random.Range(-range, range), clickPos.y + Random.Range(-range, range)); } Ray pickRay = HandleUtility.GUIPointToWorldRay(jitteredPos); int hitTriIndex = -1; if (RaycastUtil.Raycast(targetMeshCollider, pickRay, out hitTriIndex, 10000.0f)) { if (asAdditive) { if (!hull.selectedFaces.Contains(hitTriIndex)) { hull.selectedFaces.Add(hitTriIndex); } } else { hull.selectedFaces.Remove(hitTriIndex); } } } }
public bool DoMouseDown(PickMode initialMode) { if (currentToolSelection == ToolSelection.TrianglePainting) { if (targetMeshCollider != null) { if (currentHullPainter != null && currentHullPainter.paintingData != null) { Undo.RecordObject(currentHullPainter.paintingData, "Paint Hull"); pickMode = PickTriangle(initialMode); if (pickMode != PickMode.Undecided) { // Debug.Log ("Start drag"); Sync(); EditorUtility.SetDirty(currentHullPainter.paintingData); isSelectingFaces = true; return(true); } else { // Debug.Log ("Abandon drag"); } } else { // This can happen when unity triggers scene callbacks in an odd order and the currentHullPainter isn't set yet // Debug.LogError("SceneManipulator has no currentHullPainter!"); } } else { Debug.Log("Mouse down but no targetMeshCollider, ignoring"); } } else if (currentToolSelection == ToolSelection.Pipette) { // Raycast against the target mesh collider and see if the triangle we hit is in any current hull bool anyFound = false; Ray pickRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); int hitTriIndex = -1; if (targetMeshCollider != null && RaycastUtil.Raycast(targetMeshCollider, pickRay, out hitTriIndex, 10000.0f)) { for (int i = 0; i < currentHullPainter.paintingData.hulls.Count; i++) { Hull hull = currentHullPainter.paintingData.hulls[i]; if (hull.selectedFaces.Contains(hitTriIndex)) { // Now painting this hull! currentHullPainter.paintingData.activeHull = i; currentToolSelection = ToolSelection.TrianglePainting; anyFound = true; break; } } } if (!anyFound) { currentToolSelection = ToolSelection.TrianglePainting; currentHullPainter.paintingData.activeHull = -1; } } return(false); }