PlaceOnScreen.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Spawns an object when you click on the screen at the real-world position clicked.
  6. /// Will only spawn an object if the surface clicked is facing upward.
  7. /// Note that the ZED's newer plane detection feature is usually better for this unless
  8. /// you need something very simple.
  9. /// </summary>
  10. public class PlaceOnScreen : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// Prefab object to be instantiated in the real world on a click.
  14. /// </summary>
  15. public GameObject ObjectToPlace;
  16. /// <summary>
  17. /// The ZEDManager in the scene.
  18. /// </summary>
  19. private ZEDManager zedManager;
  20. /// <summary>
  21. /// The left camera in the ZED rig. Passed to ZEDSupportFunctions for transforming between camera and world space.
  22. /// </summary>
  23. private Camera cam;
  24. // Use this for initialization
  25. void Awake() {
  26. //zedManager = gameObject.transform.parent.GetComponentInChildren<ZEDManager>();
  27. zedManager = ZEDManager.GetInstance(sl.ZED_CAMERA_ID.CAMERA_ID_01);
  28. cam = zedManager.GetMainCamera();
  29. Cursor.visible = true; //Make sure cursor is visible so we can click on the world accurately.
  30. }
  31. // Update is called once per frame
  32. void Update () {
  33. if(!zedManager.zedCamera.IsCameraReady)
  34. {
  35. return;
  36. }
  37. if (Input.GetMouseButtonDown(0)) //Checks for left click.
  38. {
  39. /// Mouse Input gives the screen pixel position
  40. Vector3 ScreenPosition = Input.mousePosition;
  41. //Get Normal and real world position defined by the pixel .
  42. Vector3 Normal;
  43. Vector3 WorldPos;
  44. ZEDSupportFunctions.GetNormalAtPixel(zedManager.zedCamera,ScreenPosition,sl.REFERENCE_FRAME.WORLD,cam,out Normal);
  45. ZEDSupportFunctions.GetWorldPositionAtPixel(zedManager.zedCamera,ScreenPosition, cam,out WorldPos);
  46. //To consider the location as a flat surface, we check that the normal is valid and is closely aligned with gravity.
  47. bool validFloor = Normal.x != float.NaN && Vector3.Dot(Normal, Vector3.up) > 0.85f;
  48. //If we've found a floor to place the object, spawn a copy of the prefab.
  49. if (validFloor)
  50. {
  51. GameObject newgo = Instantiate(ObjectToPlace);
  52. newgo.transform.localPosition = WorldPos;
  53. newgo.transform.LookAt(new Vector3(zedManager.transform.position.x, newgo.transform.position.y, zedManager.transform.position.z), Vector3.up);
  54. }
  55. else
  56. {
  57. if (Normal.x == float.NaN)
  58. Debug.Log ("Cannot place object at this position. Normal vector not detected.");
  59. if (Vector3.Dot(Normal, Vector3.up) <= 0.85f)
  60. Debug.Log ("Cannot place object at this position. Normal vector angled too far from up: "+Mathf.Acos(Vector3.Dot(Normal, Vector3.up))*Mathf.Rad2Deg + "°");
  61. }
  62. }
  63. }
  64. }