TargetModelDisplayCtrl.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. namespace NRKernal.NRExamples
  11. {
  12. /// <summary> A target model display control. </summary>
  13. public class TargetModelDisplayCtrl : MonoBehaviour
  14. {
  15. /// <summary> The model target. </summary>
  16. public Transform modelTarget;
  17. /// <summary> The model renderer. </summary>
  18. public MeshRenderer modelRenderer;
  19. /// <summary> The default color. </summary>
  20. public Color defaultColor = Color.white;
  21. /// <summary> The minimum scale. </summary>
  22. public float minScale = 1f;
  23. /// <summary> The maximum scale. </summary>
  24. public float maxScale = 3f;
  25. /// <summary> The around local axis. </summary>
  26. private Vector3 m_AroundLocalAxis = Vector3.down;
  27. /// <summary> The touch scroll speed. </summary>
  28. private float m_TouchScrollSpeed = 10000f;
  29. /// <summary> The previous position. </summary>
  30. private Vector2 m_PreviousPos;
  31. /// <summary> Starts this object. </summary>
  32. void Start()
  33. {
  34. ResetModel();
  35. }
  36. /// <summary> Updates this object. </summary>
  37. private void Update()
  38. {
  39. if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
  40. {
  41. m_PreviousPos = NRInput.GetTouch();
  42. }
  43. else if (NRInput.GetButton(ControllerButton.TRIGGER))
  44. {
  45. UpdateScroll();
  46. }
  47. else if (NRInput.GetButtonUp(ControllerButton.TRIGGER))
  48. {
  49. m_PreviousPos = Vector2.zero;
  50. }
  51. }
  52. /// <summary> Updates the scroll. </summary>
  53. private void UpdateScroll()
  54. {
  55. if (m_PreviousPos == Vector2.zero)
  56. return;
  57. Vector2 deltaMove = NRInput.GetTouch() - m_PreviousPos;
  58. m_PreviousPos = NRInput.GetTouch();
  59. modelTarget.Rotate(m_AroundLocalAxis, deltaMove.x * m_TouchScrollSpeed * Time.deltaTime, Space.Self);
  60. }
  61. /// <summary> Change model color. </summary>
  62. /// <param name="color"> The color.</param>
  63. public void ChangeModelColor(Color color)
  64. {
  65. modelRenderer.material.color = color;
  66. }
  67. /// <summary> Change model scale. </summary>
  68. /// <param name="val"> The value.</param>
  69. public void ChangeModelScale(float val) // 0 ~ 1
  70. {
  71. modelTarget.localScale = Vector3.one * Mathf.SmoothStep(minScale, maxScale, val);
  72. }
  73. /// <summary> Resets the model. </summary>
  74. public void ResetModel()
  75. {
  76. modelTarget.localRotation = Quaternion.identity;
  77. ChangeModelScale(0f);
  78. ChangeModelColor(defaultColor);
  79. }
  80. }
  81. }