void CalculateGridPlacement(SceneView view) { var cam = view.camera; bool wasOrtho = GridIsOrthographic; GridIsOrthographic = cam.orthographic && Snapping.IsRounded(view.rotation.eulerAngles.normalized); m_CameraDirection = Snapping.Sign(m_Pivot - cam.transform.position); if (GridIsOrthographic && !wasOrtho || GridIsOrthographic != menuIsOrtho) { OnSceneBecameOrtho(view == SceneView.lastActiveSceneView); } if (!GridIsOrthographic && wasOrtho) { OnSceneBecamePersp(view == SceneView.lastActiveSceneView); } if (GridIsOrthographic) { return; } if (FullGridEnabled) { m_Pivot = m_GridIsLocked || Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position; } else { Vector3 sceneViewPlanePivot = m_Pivot; Ray ray = new Ray(cam.transform.position, cam.transform.forward); Plane plane = new Plane(Vector3.up, m_Pivot); // the only time a locked grid should ever move is if it's m_Pivot is out // of the camera's frustum. if ((m_GridIsLocked && !cam.InFrustum(m_Pivot)) || !m_GridIsLocked || view != SceneView.lastActiveSceneView) { float dist; if (plane.Raycast(ray, out dist)) { sceneViewPlanePivot = ray.GetPoint(Mathf.Min(dist, m_PlaneGridDrawDistance / 2f)); } else { sceneViewPlanePivot = ray.GetPoint(Mathf.Min(cam.farClipPlane / 2f, m_PlaneGridDrawDistance / 2f)); } } if (m_GridIsLocked) { m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(m_Pivot, m_RenderPlane); } else { m_Pivot = Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position; if (Selection.activeTransform == null || !cam.InFrustum(m_Pivot)) { m_Pivot = EnumExtension.InverseAxisMask(sceneViewPlanePivot, m_RenderPlane) + EnumExtension.AxisMask(Selection.activeTransform == null ? m_Pivot : Selection.activeTransform.position, m_RenderPlane); } } } }
static void DrawOrthographic(Camera camera, Axis camAxis, Color color, float snapValue, float angle) { Color previousColor = Handles.color; Color primaryColor = new Color(color.r, color.g, color.b, color.a + s_AlphaBump); Vector3 bottomLeft = Snapping.Floor(camera.ScreenToWorldPoint(Vector2.zero), snapValue); Vector3 bottomRight = Snapping.Floor(camera.ScreenToWorldPoint(new Vector2(camera.pixelWidth, 0f)), snapValue); Vector3 topLeft = Snapping.Floor(camera.ScreenToWorldPoint(new Vector2(0f, camera.pixelHeight)), snapValue); Vector3 topRight = Snapping.Floor(camera.ScreenToWorldPoint(new Vector2(camera.pixelWidth, camera.pixelHeight)), snapValue); Vector3 axis = EnumExtension.VectorWithAxis(camAxis); float width = Vector3.Distance(bottomLeft, bottomRight); float height = Vector3.Distance(bottomRight, topRight); // Shift lines to 10m forward of the camera var camTrs = camera.transform; var camForward = camTrs.forward; var camRight = camTrs.right; var camUp = camTrs.up; bottomLeft += axis * Mathf.Sign(Vector3.Dot(camForward, axis)) * 10f; topRight += axis * Mathf.Sign(Vector3.Dot(camForward, axis)) * 10f; bottomRight += axis * Mathf.Sign(Vector3.Dot(camForward, axis)) * 10f; topLeft += axis * Mathf.Sign(Vector3.Dot(camForward, axis)) * 10f; // Draw Vertical Lines float snapValueAtResolution = snapValue; int segs = (int)Mathf.Ceil(width / snapValueAtResolution) + 2; float n = 2f; while (segs > k_MaxLines) { snapValueAtResolution = snapValueAtResolution * n; segs = (int)Mathf.Ceil(width / snapValueAtResolution) + 2; n++; } // Screen start and end Vector3 bl = (camRight.x + camRight.y + camRight.z) > 0 ? Snapping.Floor(bottomLeft, camRight, snapValueAtResolution * k_PrimaryColorIncrement) : Snapping.Ceil(bottomLeft, camRight, snapValueAtResolution * k_PrimaryColorIncrement); Vector3 start = bl - camUp * (height + snapValueAtResolution * 2); Vector3 end = bl + camUp * (height + snapValueAtResolution * 2); segs += k_PrimaryColorIncrement; // The current line start and end Vector3 lineStart; Vector3 lineEnd; for (int i = -1; i < segs; i++) { lineStart = start + (i * (camRight * snapValueAtResolution)); lineEnd = end + (i * (camRight * snapValueAtResolution)); Handles.color = i % k_PrimaryColorIncrement == 0 ? primaryColor : color; Handles.DrawLine(lineStart, lineEnd); } // Draw Horizontal Lines segs = (int)Mathf.Ceil(height / snapValueAtResolution) + 2; n = 2; while (segs > k_MaxLines) { snapValueAtResolution = snapValueAtResolution * n; segs = (int)Mathf.Ceil(height / snapValueAtResolution) + 2; n++; } Vector3 tl = (camUp.x + camUp.y + camUp.z) > 0 ? Snapping.Ceil(topLeft, camUp, snapValueAtResolution * k_PrimaryColorIncrement) : Snapping.Floor(topLeft, camUp, snapValueAtResolution * k_PrimaryColorIncrement); start = tl - camRight * (width + snapValueAtResolution * 2); end = tl + camRight * (width + snapValueAtResolution * 2); segs += (int)k_PrimaryColorIncrement; for (int i = -1; i < segs; i++) { lineStart = start + (i * (-camUp * snapValueAtResolution)); lineEnd = end + (i * (-camUp * snapValueAtResolution)); Handles.color = i % k_PrimaryColorIncrement == 0 ? primaryColor : color; Handles.DrawLine(lineStart, lineEnd); } if (angle > 0f) { Vector3 cen = Snapping.Round(((topRight + bottomLeft) / 2f), snapValue); float half = (width > height) ? width : height; float opposite = Mathf.Tan(Mathf.Deg2Rad * angle) * half; Vector3 up = camera.transform.up * opposite; Vector3 right = camera.transform.right * half; Vector3 bottomLeftAngle = cen - (up + right); Vector3 topRightAngle = cen + (up + right); Vector3 bottomRightAngle = cen + (right - up); Vector3 topLeftAngle = cen + (up - right); Handles.color = primaryColor; // y = 1x+1 Handles.DrawLine(bottomLeftAngle, topRightAngle); // y = -1x-1 Handles.DrawLine(topLeftAngle, bottomRightAngle); } Handles.color = previousColor; }
public float SnapValueInUnityUnits() { return(SnapValue * EnumExtension.SnapUnitValue(SnapUnit) * SnapMultiplierFrac()); }