ScaleLogic.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ScaleLogic
  5. {
  6. private Vector3 startObjectScale;
  7. private float startHandDistanceMeters;
  8. /// <summary>
  9. /// Initialize system with source info from controllers/hands
  10. /// </summary>
  11. /// <param name="handsPressedArray">Array with positions of down pointers</param>
  12. /// <param name="manipulationRoot">Transform of gameObject to be manipulated</param>
  13. public virtual void Setup(Transform[] handsPressedArray, Transform manipulationRoot)
  14. {
  15. startHandDistanceMeters = GetMinDistanceBetweenHands(handsPressedArray);
  16. startObjectScale = manipulationRoot.transform.localScale;
  17. }
  18. public virtual void Setup(Vector3[] handsPressedArray, Transform manipulationRoot)
  19. {
  20. startHandDistanceMeters = GetMinDistanceBetweenHands(handsPressedArray);
  21. startObjectScale = manipulationRoot.transform.localScale;
  22. }
  23. /// <summary>
  24. /// update GameObject with new Scale state
  25. /// </summary>
  26. /// <param name="handsPressedArray">Array with positions of down pointers, order should be the same as handsPressedArray provided in Setup</param>
  27. /// <returns>a Vector3 describing the new Scale of the object being manipulated</returns>
  28. public virtual Vector3 UpdateMap(Transform[] handsPressedArray)
  29. {
  30. var ratioMultiplier = GetMinDistanceBetweenHands(handsPressedArray) / startHandDistanceMeters;
  31. return startObjectScale * ratioMultiplier;
  32. }
  33. public virtual Vector3 UpdateMap(Vector3[] handsPressedArray)
  34. {
  35. var ratioMultiplier = GetMinDistanceBetweenHands(handsPressedArray) / startHandDistanceMeters;
  36. return startObjectScale * ratioMultiplier;
  37. }
  38. private float GetMinDistanceBetweenHands(Transform[] handsPressedArray)
  39. {
  40. var result = float.MaxValue;
  41. for (int i = 0; i < handsPressedArray.Length; i++)
  42. {
  43. for (int j = i + 1; j < handsPressedArray.Length; j++)
  44. {
  45. var distance = Vector3.Distance(handsPressedArray[i].position, handsPressedArray[j].position);
  46. if (distance < result)
  47. {
  48. result = distance;
  49. }
  50. }
  51. }
  52. return result;
  53. }
  54. private float GetMinDistanceBetweenHands(Vector3[] handsPressedArray)
  55. {
  56. var result = float.MaxValue;
  57. for (int i = 0; i < handsPressedArray.Length; i++)
  58. {
  59. for (int j = i + 1; j < handsPressedArray.Length; j++)
  60. {
  61. var distance = Vector3.Distance(handsPressedArray[i], handsPressedArray[j]);
  62. if (distance < result)
  63. {
  64. result = distance;
  65. }
  66. }
  67. }
  68. return result;
  69. }
  70. }