static Vector3 GetPositionForNewBrush() { Vector3 newPosition = Vector3.zero; if (SceneView.lastActiveSceneView != null) { Transform cameraTransform = SceneView.lastActiveSceneView.camera.transform; Ray ray = new Ray(cameraTransform.position, cameraTransform.forward); // First of all try to cast against existing brushes List <PolygonRaycastHit> hits = csgModel.RaycastBrushesAll(ray, false); if (hits.Count > 0) { newPosition = hits[0].Point; // Back a unit, since the brush is around 2 units in each dimensions newPosition += hits[0].Normal; newPosition -= csgModel.GetComponent <Transform>().position; if (CurrentSettings.PositionSnappingEnabled) { float snapDistance = CurrentSettings.PositionSnapDistance; newPosition = MathHelper.RoundVector3(newPosition, snapDistance); } } else { // Couldn't hit an existing brush, so try to cast against the grid float hitDistance = 0; Plane activePlane = GetActivePlane(); if (activePlane.Raycast(ray, out hitDistance)) { newPosition = ray.GetPoint(hitDistance); // Back a unit, since the brush is around 2 units in each dimensions newPosition += activePlane.normal; newPosition -= csgModel.GetComponent <Transform>().position; if (CurrentSettings.PositionSnappingEnabled) { float snapDistance = CurrentSettings.PositionSnapDistance; newPosition = MathHelper.RoundVector3(newPosition, snapDistance); } } else { // Couldn't hit the grid, probably because they're looking up at the sky, so fallback to the camera pivot point newPosition = SceneView.lastActiveSceneView.pivot; if (CurrentSettings.PositionSnappingEnabled) { float snapDistance = CurrentSettings.PositionSnapDistance; newPosition = MathHelper.RoundVector3(newPosition, snapDistance); } } } } return(newPosition); }
static void OnSelectedCreateOption(object userData) { if (userData.GetType() == typeof(PrimitiveBrushType)) { GameObject newBrushObject = csgModel.CreateBrush((PrimitiveBrushType)userData); if (SceneView.lastActiveSceneView != null) { Transform cameraTransform = SceneView.lastActiveSceneView.camera.transform; Ray ray = new Ray(cameraTransform.position, cameraTransform.forward); List <PolygonRaycastHit> hits = csgModel.RaycastBrushesAll(ray); if (hits.Count > 0) { Vector3 newPosition = hits[0].Point; // Back a unit, since the brush is around 2 units in each dimensions newPosition += hits[0].Normal; if (CurrentSettings.PositionSnappingEnabled) { float snapDistance = CurrentSettings.PositionSnapDistance; newPosition = MathHelper.RoundVector3(newPosition, snapDistance); newBrushObject.transform.position = newPosition; } } else { Vector3 newPosition = SceneView.lastActiveSceneView.pivot; if (CurrentSettings.PositionSnappingEnabled) { float snapDistance = CurrentSettings.PositionSnapDistance; newPosition = MathHelper.RoundVector3(newPosition, snapDistance); newBrushObject.transform.position = newPosition; } } } newBrushObject.GetComponent <Brush>().Invalidate(true); // Set the selection to the new object Selection.activeGameObject = newBrushObject; Undo.RegisterCreatedObjectUndo(newBrushObject, "Create Brush"); } }