From 5880f9275a74af95b4fde2471749bc6c5c51bcf9 Mon Sep 17 00:00:00 2001 From: shenjianxing <”315615051@qq.com“> Date: Wed, 7 May 2025 11:51:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A6=E6=8E=A7=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Item/Show3DCamera.cs | 184 ++++++++++++++-------------- 1 file changed, 93 insertions(+), 91 deletions(-) diff --git a/Assets/Scripts/Item/Show3DCamera.cs b/Assets/Scripts/Item/Show3DCamera.cs index a4d7f7d9..166fe3e7 100644 --- a/Assets/Scripts/Item/Show3DCamera.cs +++ b/Assets/Scripts/Item/Show3DCamera.cs @@ -31,6 +31,8 @@ public class Show3DCamera : MonoBehaviour private Vector2 mouseDownPosition; // ¼갴ʱλ + private bool isDoubleTouching = false; // ¼˫ָ״̬ + private bool isZooming = false; // ¼˫ָ״̬ public enum RotationType { @@ -44,6 +46,10 @@ public class Show3DCamera : MonoBehaviour Vector3 targetPosition; Vector3 targetRotate; private float prevTouchDistance; // 洢һ֡˫ָ + + // ٵǰת״̬ + private Quaternion currentRotation; + private void Awake() { instance = this; @@ -59,6 +65,7 @@ public class Show3DCamera : MonoBehaviour gameObject.SetActive(false); #endif } + private void OnLockEvent(OnLock islock) { this.lockMove = islock.isLock; @@ -77,16 +84,8 @@ public class Show3DCamera : MonoBehaviour this.targetRotate = target.eulerAngles; yaw = 0; pitch = 0; - // ʼλ this.inputRect = inputRect; this.targetPos = target.transform.position; - //this.rotateSpeed = rotateSpeed; - //this.moveSpeed = moveSpeed; - //this.distance = distance; - //this.distanceMin = distanceMin; - //this.distanceMax = distanceMax; - //this.pitchMinMax = new Vector2(pitchMin, pitchMax); - // ʼλ offset = new Vector3(0, 0, -distance); if (isRenderTexture) @@ -97,11 +96,12 @@ public class Show3DCamera : MonoBehaviour { self.targetTexture = null; } + + // ʼת״̬ + currentRotation = Quaternion.Euler(pitch, yaw, 0); UpdateCameraPosition(moveTime); } - - public void ResetCamera(Transform target, RectTransform inputRect = null, bool isRenderTexture = false) { if (target == null) @@ -114,10 +114,8 @@ public class Show3DCamera : MonoBehaviour this.targetRotate = target.eulerAngles; yaw = 0; pitch = 0; - // ʼλ this.inputRect = inputRect; this.targetPos = target.transform.position; - // ʼλ offset = new Vector3(0, 0, -distance); if (isRenderTexture) @@ -128,51 +126,76 @@ public class Show3DCamera : MonoBehaviour { self.targetTexture = null; } + + // ת״̬ + currentRotation = Quaternion.Euler(pitch, yaw, 0); UpdateCameraPosition(-1); } - void Update() { if (targetPos != null && lockMove == false && EventSystem.current.IsPointerOverGameObject() == false) { // ȴָ if (HandleThreeFingerDrag()) return; + bool isTouching = Input.touchCount > 0; - // תǰȴ˫ָ - bool isZooming = false; + // ˫ָ if (isTouching && Input.touchCount == 2) { HandleTouchZoom(); - isZooming = true; // + isZooming = true; + isDoubleTouching = true; + return; + } + else if (Input.touchCount == 0) + { + isDoubleTouching = false; + isZooming = false; } + bool shouldRotate = false; // ʼų˫ָ - if (isTouching && Input.touchCount == 1) + if (isTouching && Input.touchCount == 1 && !isDoubleTouching) { if (Input.GetTouch(0).phase == TouchPhase.Began) { mouseDownPosition = Input.GetTouch(0).position; + + // ¼ǰת״̬ + if (type == RotationType.Orbit) + { + currentRotation = Quaternion.Euler(pitch, yaw, 0); + } + } + if (Input.GetTouch(0).phase == TouchPhase.Moved) + { + float dragDistance = Vector2.Distance(Input.mousePosition, mouseDownPosition); + shouldRotate = dragDistance > DRAG_THRESHOLD; } } // 갴 - else if (Input.GetMouseButtonDown(0)) + else if (Input.GetMouseButtonDown(0) && !isDoubleTouching) { mouseDownPosition = Input.mousePosition; + + // ¼ǰת״̬ + if (type == RotationType.Orbit) + { + currentRotation = Quaternion.Euler(pitch, yaw, 0); + } } - bool shouldRotate = false; - if (Input.GetMouseButton(0)) + if (Input.GetMouseButton(0) && !isDoubleTouching) { - // ʹвק float dragDistance = Vector2.Distance(Input.mousePosition, mouseDownPosition); shouldRotate = dragDistance > DRAG_THRESHOLD; } - // ޸ĺתų״̬ - if (!isZooming && (isTouching ? (Input.GetTouch(0).phase == TouchPhase.Moved) : shouldRotate)) + + // ת + if (!isZooming && shouldRotate) { - RotateCamera(); } @@ -188,13 +211,15 @@ public class Show3DCamera : MonoBehaviour { MoveTarget(); } + DetectHoveredObject(); } } + // Ψһָbool private bool HandleThreeFingerDrag() { - if (Input.touchCount == 5) + if (Input.touchCount == 3) { // ƽƶ Vector2 totalDelta = Vector2.zero; @@ -221,24 +246,13 @@ public class Show3DCamera : MonoBehaviour return false; } - - // ˫ָŴ - // - private float zoomSmoothVelocity; // ƽٶȻ - [SerializeField] private float zoomSmoothTime = 0.1f; // ƽʱ + // ˫ָŴ private void HandleTouchZoom() { if (Input.touchCount == 2) { Touch touch0 = Input.GetTouch(0); Touch touch1 = Input.GetTouch(1); - // ˫ָʱתر - if (touch0.phase == TouchPhase.Began || touch1.phase == TouchPhase.Began) - { - yaw = transform.eulerAngles.y; // ֵǰתǶ - pitch = transform.eulerAngles.x; - mouseDownPosition = Vector2.zero; - } Vector2 touch0Pos = touch0.position; Vector2 touch1Pos = touch1.position; @@ -246,37 +260,26 @@ public class Show3DCamera : MonoBehaviour // DPIӦ float dpi = Screen.dpi == 0 ? 200 : Screen.dpi; - float zoomFactor = 0.01f * (200 / dpi); // ׼DPIΪ200 + float zoomFactor = 0.01f * (200 / dpi); if (touch0.phase == TouchPhase.Began || touch1.phase == TouchPhase.Began) { prevTouchDistance = currentDistance; - zoomSmoothVelocity = 0; // ƽٶ } else if (touch0.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Moved) { float deltaDistance = currentDistance - prevTouchDistance; - - // ʹƽ - float targetDistance = distance - deltaDistance * zoomFactor; - targetDistance = Mathf.Clamp(targetDistance, distanceMin, distanceMax); - - distance = Mathf.SmoothDamp( - distance, - targetDistance, - ref zoomSmoothVelocity, - zoomSmoothTime - ); - - offset = new Vector3(0, 0, -distance); - UpdateCameraPosition(); - + // жϣ˫ָ仯Сijֵ򲻽Ų + if (Mathf.Abs(deltaDistance) > 1f) + { + float scroll = deltaDistance * zoomFactor; + ZoomCamera(scroll); + } prevTouchDistance = currentDistance; } } } - // ͣ public void DetectHoveredObject() { @@ -292,29 +295,21 @@ public class Show3DCamera : MonoBehaviour RaycastHit raycastHit; if (Physics.Raycast(ray, out raycastHit)) { - //Debug.Log(raycastHit.transform.name); obj = raycastHit.transform.gameObject; - - // еһλе岻ͬʾ߽ if (obj != lastHitObject) { - // ¼ OnMouseEnterObj(obj); - // һλе岻Ϊգ뿪¼ if (lastHitObject != null && lastHitObject != obj) { OnMouseExitObj(lastHitObject); - // һλе lastHitObject = obj; } - } } else { - // ûлκ壬һλе岻Ϊգ뿪¼ if (lastHitObject != null) { OnMouseExitObj(lastHitObject); @@ -323,13 +318,11 @@ public class Show3DCamera : MonoBehaviour } } - /// /// ۽ij /// public void FocusObj(Vector3 target, float distance = 1f, float moveTime = -1) { - // ԸҪ Vector3 cameraPos = target - transform.forward * distance; targetPos = target; @@ -361,13 +354,11 @@ public class Show3DCamera : MonoBehaviour { tip.OnExit(); } - } // ޸ĺƶͳһԴ private void MoveTarget(float mouseX = 0, float mouseY = 0) { - // ԶжԴ if (Mathf.Approximately(mouseX, 0) && Mathf.Approximately(mouseY, 0)) { mouseX = Input.GetAxis("Mouse X") * moveSpeed; @@ -377,11 +368,12 @@ public class Show3DCamera : MonoBehaviour transform.Translate(new Vector3(-mouseX, -mouseY, 0)); targetPos += new Vector3(-mouseX, -mouseY, 0); } + // private void ZoomCamera(float scroll) { - distance -= scroll * 5f; // ٶ - distance = Mathf.Clamp(distance, distanceMin, distanceMax); // ƾ뷶Χ + distance -= scroll * 5f; + distance = Mathf.Clamp(distance, distanceMin, distanceMax); offset = new Vector3(0, 0, -distance); UpdateCameraPosition(); } @@ -389,7 +381,6 @@ public class Show3DCamera : MonoBehaviour // ޸RotateCamera private void RotateCamera() { - // ʹô float deltaX = 0, deltaY = 0; // @@ -402,14 +393,13 @@ public class Show3DCamera : MonoBehaviour // 괦 else { - deltaX = Input.GetAxis("Mouse X") * 10; // ԭ + deltaX = Input.GetAxis("Mouse X") * 10; deltaY = Input.GetAxis("Mouse Y") * 10; - // ۼƶֵʱЧ if (Mathf.Abs(deltaX) < DRAG_THRESHOLD && Mathf.Abs(deltaY) < DRAG_THRESHOLD) return; } - // ӦDPIţؼ + // ӦDPI float dpiScale = Screen.dpi == 0 ? 1 : Screen.dpi / 160f; deltaX *= rotateSpeed * Time.deltaTime / dpiScale; deltaY *= rotateSpeed * Time.deltaTime / dpiScale; @@ -440,24 +430,36 @@ public class Show3DCamera : MonoBehaviour // λúͳ private void UpdateCameraPosition(float moveTime = -1) { - // תƫ - Quaternion rotation = Quaternion.Euler(pitch, yaw, 0); - Vector3 rotatedOffset = rotation * offset; - // λ - if (moveTime != -1) + if (type == RotationType.Orbit) { - transform.DOMove(targetPos + rotatedOffset, moveTime).onUpdate = () => - // ʼճĿ - transform.LookAt(targetPos); - } - else - { - transform.position = targetPos + rotatedOffset; - // ʼճĿ - transform.LookAt(targetPos); - } + // ʹԪתƫ + Quaternion rotation = Quaternion.Euler(pitch, yaw, 0); + Vector3 rotatedOffset = rotation * offset; + if (moveTime != -1) + { + transform.DOMove(targetPos + rotatedOffset, moveTime).onUpdate = () => + transform.LookAt(targetPos); + } + else + { + transform.position = targetPos + rotatedOffset; + transform.LookAt(targetPos); + } + } + else if (type == RotationType.Spherical) + { + // ģʽµ + if (moveTime != -1) + { + transform.DOMove(targetPos + offset, moveTime).onUpdate = () => + transform.LookAt(targetPos); + } + else + { + transform.position = targetPos + offset; + transform.LookAt(targetPos); + } + } } -} - - +} \ No newline at end of file