AddNode.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. using UnityEngine.EventSystems;
  13. using Immersal.AR;
  14. namespace Immersal.Samples.Navigation
  15. {
  16. [RequireComponent(typeof(Button))]
  17. public class AddNode : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  18. {
  19. [SerializeField]
  20. private GameObject waypointObject = null;
  21. [SerializeField]
  22. private GameObject targetObject = null;
  23. [SerializeField]
  24. private Material overrideMaterial = null;
  25. private enum NodeToAdd
  26. {
  27. Waypoint, Target
  28. };
  29. [SerializeField]
  30. private NodeToAdd m_NodeToAdd = NodeToAdd.Waypoint;
  31. private Button button = null;
  32. private bool isPressed = false;
  33. private GameObject nodeInstance = null;
  34. private Camera mainCamera = null;
  35. private Vector3 pos = Vector3.zero;
  36. private Quaternion rot = Quaternion.identity;
  37. private Quaternion randomRotation = Quaternion.identity;
  38. private ARSpace arspace = null;
  39. void Start()
  40. {
  41. button = GetComponent<Button>();
  42. mainCamera = Camera.main;
  43. arspace = FindObjectOfType<ARSpace>();
  44. }
  45. void Update()
  46. {
  47. if (isPressed)
  48. {
  49. if (nodeInstance != null)
  50. {
  51. pos = mainCamera.transform.position + mainCamera.transform.forward * 1.5f;
  52. Vector3 x = Vector3.Cross(Vector3.up, mainCamera.transform.forward);
  53. Vector3 z = Vector3.Cross(x, Vector3.up);
  54. rot = Quaternion.LookRotation(z, Vector3.up) * randomRotation;
  55. nodeInstance.transform.position = pos;
  56. nodeInstance.transform.rotation = rot;
  57. }
  58. }
  59. }
  60. public void OnPointerDown(PointerEventData eventData)
  61. {
  62. if (Immersal.Samples.Navigation.NavigationManager.Instance.inEditMode)
  63. {
  64. isPressed = true;
  65. if (nodeInstance == null && waypointObject != null && targetObject != null)
  66. {
  67. switch (m_NodeToAdd)
  68. {
  69. case NodeToAdd.Waypoint:
  70. nodeInstance = Instantiate(waypointObject);
  71. break;
  72. case NodeToAdd.Target:
  73. nodeInstance = Instantiate(targetObject);
  74. break;
  75. default:
  76. nodeInstance = Instantiate(waypointObject);
  77. break;
  78. }
  79. // REPLACE ALL MATERIALS WHILE PLACING THE WAYPOINT
  80. if (overrideMaterial != null)
  81. {
  82. foreach (MeshRenderer rend in nodeInstance.GetComponentsInChildren<MeshRenderer>())
  83. {
  84. if (rend != null)
  85. {
  86. Material[] mats = new Material[rend.materials.Length];
  87. for (int i = 0; i < mats.Length; i++)
  88. {
  89. mats[i] = overrideMaterial;
  90. }
  91. rend.materials = mats;
  92. }
  93. }
  94. }
  95. }
  96. randomRotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
  97. }
  98. }
  99. public void OnPointerUp(PointerEventData eventData)
  100. {
  101. isPressed = false;
  102. if (Immersal.Samples.Navigation.NavigationManager.Instance.inEditMode)
  103. {
  104. if (nodeInstance != null)
  105. {
  106. Destroy(nodeInstance);
  107. }
  108. // ACTUALLY PLACE THE WAYPOINT
  109. if (arspace != null)
  110. {
  111. GameObject nodeInstance;
  112. switch (m_NodeToAdd)
  113. {
  114. case NodeToAdd.Waypoint:
  115. nodeInstance = Instantiate(waypointObject, arspace.transform);
  116. break;
  117. case NodeToAdd.Target:
  118. nodeInstance = Instantiate(targetObject, arspace.transform);
  119. break;
  120. default:
  121. nodeInstance = Instantiate(waypointObject, arspace.transform);
  122. break;
  123. }
  124. nodeInstance.transform.position = pos;
  125. nodeInstance.transform.rotation = rot;
  126. }
  127. }
  128. }
  129. }
  130. }