PointableCanvasMesh.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEngine.Serialization;
  4. using Rokid.UXR;
  5. using Rokid.UXR.Utility;
  6. namespace Rokid.UXR.Interaction
  7. {
  8. public class PointableCanvasMesh : PointableElement
  9. {
  10. [SerializeField]
  11. [FormerlySerializedAs("_canvasRenderTextureMesh")]
  12. private CanvasMesh _canvasMesh;
  13. [SerializeField]
  14. private CanvasRenderTexture _canvasRenderTexture;
  15. [SerializeField]
  16. private Cylinder _cylinder;
  17. [SerializeField, Range(0.3f, 10)]
  18. public float _cylinderRadius;
  19. [SerializeField, Tooltip("曲面屏横向放大倍数")]
  20. public float _cylinderCanvasScaleWidth = 1.0f;
  21. [SerializeField, Tooltip("曲面屏纵向放大倍数")]
  22. public float _cylinderCanvasScaleHeight = 1.0f;
  23. [SerializeField, Tooltip("是否修改显示半径")]
  24. public bool _changeViewRadius;
  25. [SerializeField]
  26. private Canvas _unityCanvas;
  27. private float ori_radius;
  28. private float ori_scale;
  29. private bool ori_changeViewRadius;
  30. public float CylinderRadius { get { return _cylinderRadius; } set { _cylinderRadius = value; } }
  31. protected override void Start()
  32. {
  33. base.Start();
  34. Assert.IsNotNull(_canvasMesh);
  35. ori_radius = _cylinder.Radius;
  36. ori_scale = _unityCanvas.transform.localScale.x;
  37. ori_changeViewRadius = _changeViewRadius;
  38. UpdateRaidus();
  39. }
  40. public override void ProcessPointerEvent(PointerEvent evt)
  41. {
  42. RKLog.Debug($"====PointableCanvasMesh==== Before ProcessPointerEvent {evt.Pose.position}");
  43. Vector3 transformPosition =
  44. _canvasMesh.ImposterToCanvasTransformPoint(evt.Pose.position);
  45. Pose transformedPose = new Pose(transformPosition, evt.Pose.rotation);
  46. RKLog.Debug($"====PointableCanvasMesh==== After ProcessPointerEvent {transformedPose.position}");
  47. base.ProcessPointerEvent(new PointerEvent(evt.Identifier, evt.Type, transformedPose));
  48. }
  49. private void Update()
  50. {
  51. if (_cylinderRadius != ori_radius || _unityCanvas.transform.localScale.x != ori_scale || ori_changeViewRadius != _changeViewRadius)
  52. {
  53. UpdateRaidus();
  54. }
  55. }
  56. private void UpdateRaidus()
  57. {
  58. _cylinder.Radius = _cylinderRadius;
  59. _cylinder.CylinderCanvasScaleWidth = _cylinderCanvasScaleWidth;
  60. _cylinder.CylinderCanvasScaleHeight = _cylinderCanvasScaleHeight;
  61. if (_changeViewRadius)
  62. {
  63. transform.position = MainCameraCache.mainCamera.transform.TransformPoint(new Vector3(0, 0, _cylinderRadius));
  64. }
  65. _unityCanvas.transform.localPosition = Vector3.zero;
  66. _cylinder.transform.localPosition = new Vector3(0, 0, -_cylinderRadius);
  67. _canvasMesh.UpdateImposter();
  68. _canvasRenderTexture.UpdateCamera();
  69. ori_radius = _cylinderRadius;
  70. ori_scale = _unityCanvas.transform.localScale.x;
  71. ori_changeViewRadius = _changeViewRadius;
  72. }
  73. }
  74. }