diff --git a/Assets/Samples/Turing 3D Core/1.0.0/Samples/Scripts/Draggable.cs b/Assets/Samples/Turing 3D Core/1.0.0/Samples/Scripts/Draggable.cs new file mode 100644 index 00000000..01a0b8e6 --- /dev/null +++ b/Assets/Samples/Turing 3D Core/1.0.0/Samples/Scripts/Draggable.cs @@ -0,0 +1,203 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Beijing HiteVision Turing Intelligent Technology Co.,Ltd. All rights reserved. +// +//----------------------------------------------------------------------- + +using Turing.Core.EventSystems; +using Turing.Core.TuringInput; +using Turing.Core.Utility; +using UnityEngine; +using UnityEngine.EventSystems; + + +namespace Turing.Samples +{ + public class Draggable : + TuringPointerInteractable, IBeginDragHandler, IDragHandler, IEndDragHandler + { + [Tooltip("Change Drag Button")] + public PointerEventData.InputButton dragTargetButton = PointerEventData.InputButton.Left; + + //////////////////////////////////////////////////////////////////////// + // Private Members + //////////////////////////////////////////////////////////////////////// + + private Vector3 _initialGrabOffset = Vector3.zero; + private Quaternion _initialGrabRotation = Quaternion.identity; + private bool _isKinematic = false; + + private Transform originParent; + + private Transform grab; + private Transform grabRotate; + + private float distance; + //////////////////////////////////////////////////////////////////////// + // Public Methods + //////////////////////////////////////////////////////////////////////// + + public override TuringPointer.DragPolicy GetDragPolicy(TuringPointer pointer) + { + if (pointer is TuringMouse) + { + return TuringPointer.DragPolicy.LockToScreenAlignedPlane; + } + + if (pointer is TuringTouch) + { + return TuringPointer.DragPolicy.LockToScreenAlignedPlane; + } + + if (pointer is TuringStylus) + { + return TuringPointer.DragPolicy.LockHitPosition; + } + + return base.GetDragPolicy(pointer); + } + + public virtual void OnBeginDrag(PointerEventData eventData) + { + TuringPointerEventData pointerEventData = eventData as TuringPointerEventData; + + if (pointerEventData == null) + { + return; + } + + if (pointerEventData.button == dragTargetButton) + { + if (pointerEventData.Pointer is TuringStylus) + { + Pose pose = pointerEventData.Pointer.EndPointWorldPose; + this._initialGrabOffset = + Quaternion.Inverse(this.transform.rotation) * + (this.transform.position - pose.position); + // this._initialGrabRotation = this.transform.localRotation; + grab = new GameObject("Grab").GetComponent(); + grabRotate = new GameObject("GrabRotate").GetComponent(); + grabRotate.position = pose.position; + grabRotate.rotation *= TuringProvider.CurrentFrame.DisplayAligner.transform.rotation; + grab.position = pose.position; + originParent = transform.parent; + grab.LookAt(pointerEventData.Pointer.transform); + distance = Vector3.Distance(grab.position, pointerEventData.Pointer.transform.position); + transform.SetParent(grabRotate); + grabRotate.SetParent(grab); + this._initialGrabRotation = grabRotate.localRotation; + } + else + { + Pose pose = pointerEventData.Pointer.EndPointWorldPose; + + // Cache the initial grab state. + this._initialGrabOffset = + Quaternion.Inverse(this.transform.rotation) * + (this.transform.position - pose.position); + + this._initialGrabRotation = + Quaternion.Inverse(pose.rotation) * + this.transform.rotation; + } + + + // If the grabbable object has a rigidbody component, + // mark it as kinematic during the grab. + var rigidbody = this.GetComponent(); + if (rigidbody != null) + { + this._isKinematic = rigidbody.isKinematic; + rigidbody.isKinematic = true; + } + + // Capture pointer events. + pointerEventData.Pointer.CapturePointer(this.gameObject); + } + } + + public virtual void OnDrag(PointerEventData eventData) + { + TuringPointerEventData pointerEventData = eventData as TuringPointerEventData; + if (pointerEventData == null) + { + return; + } + + if (pointerEventData.pointerDrag != this.gameObject) + { + return; + } + + if (pointerEventData.button == dragTargetButton) + { + if (pointerEventData.Pointer is TuringStylus) + { + TuringStylus stylus = (TuringStylus)pointerEventData.Pointer; + grabRotate.localRotation = _initialGrabRotation * Quaternion.Euler(stylus.StylueEulerAngle); + // Debug.Log("------------"+_initialGrabRotation.eulerAngles+" "+stylus.StylueEulerAngle); + grab.transform.position = pointerEventData.Pointer.transform.position + (stylus._target.Points[0] - stylus._target.Points[1]).normalized * distance; + grab.LookAt(pointerEventData.Pointer.transform); + } + else + { + HandleLeftDrag(pointerEventData); + } + } + } + + private void HandleLeftDrag(TuringPointerEventData pointerEventData) + { + Pose pose = pointerEventData.Pointer.EndPointWorldPose; + + // Update the grab object's rotation. + this.transform.rotation = + pose.rotation * this._initialGrabRotation; + + // Update the grab object's position. + this.transform.position = + pose.position + + (this.transform.rotation * this._initialGrabOffset); + } + + + public virtual void OnEndDrag(PointerEventData eventData) + { + TuringPointerEventData pointerEventData = eventData as TuringPointerEventData; + if (pointerEventData == null || + pointerEventData.button != dragTargetButton) + { + return; + } + + Debug.Log("EndDrag"); + + if (pointerEventData.Pointer is TuringStylus) + { + // Pose pose = pointerEventData.Pointer.EndPointWorldPose; + // this._initialGrabOffset = + // Quaternion.Inverse(this.transform.rotation) * + // (this.transform.position - pose.position); + // grab = new GameObject("Grab").GetComponent(); + // grab.transform.position = pose.position; + // originParent = transform.parent; + // grab.LookAt(pointerEventData.Pointer.transform); + // transform.SetParent(grab); + transform.SetParent(originParent); + Destroy(grab.gameObject); + Destroy(grabRotate.gameObject); + } + + // Release the pointer. + pointerEventData.Pointer.CapturePointer(null); + + // If the grabbable object has a rigidbody component, + // restore its original isKinematic state. + var rigidbody = this.GetComponent(); + if (rigidbody != null) + { + rigidbody.isKinematic = this._isKinematic; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Item/Body3DOjbItem.cs b/Assets/Scripts/Item/Body3DOjbItem.cs index 6787e4b2..9cde6fe1 100644 --- a/Assets/Scripts/Item/Body3DOjbItem.cs +++ b/Assets/Scripts/Item/Body3DOjbItem.cs @@ -3,6 +3,8 @@ using QFramework.Example; using System; using System.Collections; using System.Collections.Generic; +using Turing.Samples; +using UnityEditor; using UnityEngine; using XMLTool; @@ -11,22 +13,27 @@ public class Body3DOjbItem : MonoBehaviour public Body3D.Body body; public ObjectToggle objToggle; - ObjDrag objDrag; + IObjDrag objDrag; // ¼Ç¼ÉÏÒ»´ÎÊó±ê°´ÏµÄʱ¼ä private float lastClickTime; // Ë«»÷µÄʱ¼ä¼ä¸ôãÐÖµ private const float doubleClickTimeThreshold = 0.3f; Shader shader; - + private void Awake() { shader = GetComponent()?.material.shader; TypeEventSystem.Global.Register(OnChangeMatEvent).UnRegisterWhenGameObjectDestroyed(this); +#if VR +#if Turing + gameObject.GetOrAddComponent(); +#endif +#endif } private void OnChangeMatEvent(OnChangeMat t) { - + if (t.shader != null) { GetComponent().material.shader = t.shader; @@ -85,11 +92,20 @@ public class Body3DOjbItem : MonoBehaviour colorToggle.SetColor(ObjectColorToggle.State.Off); } } +#if VR +#if Turing + objDrag = gameObject.GetOrAddComponent(); + +#endif +#else objDrag = gameObject.GetOrAddComponent(); +#endif objDrag.OnDragEnd.AddListener(obj => { Body3DController.Instance.AddMoveObj(gameObject); }); + + RefreshDrag(); if (isOn) { @@ -125,7 +141,7 @@ public class Body3DOjbItem : MonoBehaviour { if (Body3DController.Instance.CheckStatus(Body3DController.Status.Drag)) { - var drag = gameObject.GetComponent(); + var drag = gameObject.GetComponent(); if (drag != null) { drag.OnDoubleClick(); diff --git a/Assets/Scripts/Item/ObjDrag.cs b/Assets/Scripts/Item/ObjDrag.cs index 26252c07..e6ac196a 100644 --- a/Assets/Scripts/Item/ObjDrag.cs +++ b/Assets/Scripts/Item/ObjDrag.cs @@ -4,14 +4,28 @@ using System; using UnityEngine; using UnityEngine.Events; -public class ObjDrag : MonoBehaviour +public class ObjDrag : MonoBehaviour, IObjDrag { private Vector3 offset; - public bool isOn = false; + bool isOn = false; Vector3 startPosition; - public UnityEvent OnDragEnd = new UnityEvent(); + UnityEvent OnDragEnd = new UnityEvent(); + + bool IObjDrag.isOn { get => isOn; set => isOn = value; } + UnityEvent IObjDrag.OnDragEnd + { + get + { + if (OnDragEnd == null) + { + OnDragEnd = new UnityEvent(); + } + return OnDragEnd; + } + } + void Start() { startPosition = gameObject.transform.position; diff --git a/Assets/Scripts/Item/ObjDragBase.cs b/Assets/Scripts/Item/ObjDragBase.cs new file mode 100644 index 00000000..e37dd72d --- /dev/null +++ b/Assets/Scripts/Item/ObjDragBase.cs @@ -0,0 +1,13 @@ + +using UnityEngine.Events; +using UnityEngine; + +public interface IObjDrag +{ + bool isOn { get; set; } + + UnityEvent OnDragEnd { get; } + void OnDoubleClick(); + +} + diff --git a/Assets/Scripts/Item/ObjDragBase.cs.meta b/Assets/Scripts/Item/ObjDragBase.cs.meta new file mode 100644 index 00000000..ecfd8623 --- /dev/null +++ b/Assets/Scripts/Item/ObjDragBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 358938c106d6aa247b25e7055aa7ff5f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/TuringVR.meta b/Assets/Scripts/TuringVR.meta new file mode 100644 index 00000000..214fcdff --- /dev/null +++ b/Assets/Scripts/TuringVR.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1d41e85e3c4f62342a3afbb84c9c74fc +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/TuringVR/Dragbble.cs b/Assets/Scripts/TuringVR/Dragbble.cs new file mode 100644 index 00000000..25704a8b --- /dev/null +++ b/Assets/Scripts/TuringVR/Dragbble.cs @@ -0,0 +1,80 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using Turing.Samples; +using Turing.Core.EventSystems; +using UnityEngine.EventSystems; +using QFramework; +using UnityEngine.Events; +using DG.Tweening; +public class TuringDraggableEx : Draggable, IObjDrag +{ + bool isOn = false; + UnityEvent OnDragEnd; + bool IObjDrag.isOn { get => isOn; set => isOn = value; } + + Vector3 beginPos; + UnityEvent IObjDrag.OnDragEnd + { + get + { + if (OnDragEnd == null) + { + OnDragEnd = new UnityEvent(); + } + return OnDragEnd; + } + } + + + Vector3 startPosition; + private void Start() + { + startPosition = gameObject.transform.position; + } + public override void OnBeginDrag(PointerEventData eventData) + { + beginPos = gameObject.Position(); + if (isOn == false) + { + gameObject.transform.position = beginPos; + } + else + { + base.OnBeginDrag(eventData); + } + + } + + + + public override void OnDrag(PointerEventData eventData) + { + if (isOn) + { + base.OnDrag(eventData); + } + else + { + gameObject.transform.position = beginPos; + } + } + + public override void OnEndDrag(PointerEventData eventData) + { + if (isOn) + { + base.OnEndDrag(eventData); + OnDragEnd?.Invoke(gameObject); + } + } + + + public void OnDoubleClick() + { + transform.DOMove(startPosition, 0.1f); + } + + + +} diff --git a/Assets/Scripts/TuringVR/Dragbble.cs.meta b/Assets/Scripts/TuringVR/Dragbble.cs.meta new file mode 100644 index 00000000..c6e2f520 --- /dev/null +++ b/Assets/Scripts/TuringVR/Dragbble.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bf0c5616c5204c148a7021b627412be1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/UI/UIBody3D.cs b/Assets/Scripts/UI/UIBody3D.cs index e37a221f..3b76c267 100644 --- a/Assets/Scripts/UI/UIBody3D.cs +++ b/Assets/Scripts/UI/UIBody3D.cs @@ -77,7 +77,7 @@ namespace QFramework.Example GameObject obj = Body3DController.Instance.PopMoveObj(); if (obj != null) { - obj.GetComponent().OnDoubleClick(); + obj.GetComponent().OnDoubleClick(); } }); @@ -110,7 +110,7 @@ namespace QFramework.Example GameObject obj = Body3DController.Instance.PopMoveObj(); if (obj != null) { - obj.GetComponent().OnDoubleClick(); + obj.GetComponent().OnDoubleClick(); } } });