Search Unity

AR Foundation place content front camera

Discussion in 'Handheld AR' started by Edur-Games, Sep 24, 2018.

  1. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Hi!
    Can the content be placed in front of the camera (Like Mid-Air in Vuforia) in ARFoudation?
    No need for ground plane

    Thanks!
     
  2. Daniel4362

    Daniel4362

    Joined:
    Dec 7, 2017
    Posts:
    4
    Hi Edur-Games,

    I managed to place content floating in space and not anchored to any detected plane using ARFoundation. There is a convenience method on ARSessionOrigin called MakeContentAppearAt(). This does not scale any of the content, but instead scales and positions the ARSessionOrigin to make the content appear like is at the provided location.

    There is a really good forum post about it here: https://forum.unity.com/threads/scaling-with-arfoundation.543953/

    I just get the position 1 meter in front of the camera and call this method providing that position and you have your object floating in front of the camera.

    Hope this could help.
     
  3. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    It looks great. I'll prove it. Thank you so much for your help. Congratulations!

    Thank you!
     
  4. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Hello!
    I tried to place the content in front of the camera like you said.

    What happens once placed seems to move with me while I move the camera. He's not totally still.
    I put the piece of code in case I'm doing something wrong
    Code (CSharp):
    1.  
    2.  
    3. void OnPositionContent()
    4.     {
    5.         if (Input.touchCount > 0)
    6.         {
    7.             Touch touch = Input.GetTouch(0);
    8.  
    9.             if (spawnedObject == null)
    10.             {
    11.                 spawnedObject = Instantiate(m_PlacedPrefab);
    12.                [COLOR=#ff4d4d] Vector3 pos = new Vector3(oTransform.position.x, oTransform.position.y, oTransform.position.z + 1);[/COLOR]
    13.                 [COLOR=#ff4d4d]m_SessionOrigin.MakeContentAppearAt(spawnedObject.transform, pos, Quaternion.identity);[/COLOR]
    14.                 DisablePlanes();
    15.                 contentPlaced = true;
    16.                 SetScaleContent();
    17.                 StartGame();
    18.             }
    19.         }
    20.  
    21.     }
    The first line in red, I want to take the position that is one meter more than the front camera

    I don't know if you've done it any other way and you can lend me a hand:)

    Thanks a lot!
    Edu
     
  5. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Okay, the problem is solved. It stays well in front of the camera.

    Thank you very much for your help
     
  6. newguy123

    newguy123

    Joined:
    Aug 22, 2018
    Posts:
    154
    What did you do to solve it?
     
  7. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Nothing special.
    With this piece of code, it keeps me well in front of the camera.
    I don't know if the ambient light estimate and the content scaling value can affect the behavior.
    I have not tried very thoroughly my project but at first glance seems to be correct and you can move around it without strange movements

    Code (CSharp):
    1.   void Update()
    2.     {
    3.         if (!contentPlaced)
    4.         {
    5.             OnPositionContent();
    6.         }
    7.     }
    8.  
    9.     void OnPositionContent()
    10.     {
    11.         if (Input.touchCount > 0)
    12.         {
    13.             Touch touch = Input.GetTouch(0);
    14.  
    15.             if (spawnedObject == null)
    16.             {
    17.                 spawnedObject = Instantiate(m_PlacedPrefab);
    18.                 Vector3 pos = new Vector3(oTransform.position.x, oTransform.position.y - 0.5f, oTransform.position.z + 1);
    19.                 m_SessionOrigin.MakeContentAppearAt(spawnedObject.transform, pos, Quaternion.identity);
    20.                 DisablePlanes();
    21.                 contentPlaced = true;
    22.                 //SetScaleContent();
    23.                 //StartGame();
    24.             }
    25.         }
    26.  
    27.     }
    28.  
    29.     void DisablePlanes()
    30.     {
    31.         ARPlaneManager planeManager = GetComponent<ARPlaneManager>();
    32.         List<ARPlane> allPlanes = new List<ARPlane>();
    33.         planeManager.GetAllPlanes(allPlanes);
    34.         planeManager.enabled = false;
    35.         foreach (ARPlane plane in allPlanes)
    36.         {
    37.             plane.gameObject.SetActive(false);
    38.         }
    39.     }
    oTransform Is my reference transform of this object (ARSessionOrigin) parent of ARCamera
     
    newguy123 likes this.
  8. jatreutlein

    jatreutlein

    Joined:
    Dec 14, 2018
    Posts:
    6
    Hi @Edur-Games,
    I just want to clarify what you mean with "oTransform Is my reference transform of this object (ARSessionOrigin) parent of ARCamera." Since the script runs as a component of the ARSessionOrigin, oTransform would just be:
    Transform oTransform = transform;

    This doesn't seem to work for me, could you please post the line of code you use to define oTransform if you can. Much appreciated, thanks!
     
  9. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Hi!
    Forgive me for my English, because it is not very fluent! :(

    I mean this

    Code (CSharp):
    1. private Transform oTransform;
    2.     GameObject spawnedObject;
    3.     ARSessionOrigin m_SessionOrigin;
    4.  
    5.     void Awake()
    6.     {
    7.         oTransform = transform;
    8.         m_SessionOrigin = GetComponent<ARSessionOrigin>();
    9.         contentPlaced = false;
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (!contentPlaced)
    16.         {
    17.             OnPositionContent();
    18.         }
    19.     }
    20.  
    21.     void OnPositionContent()
    22.     {
    23.         if (Input.touchCount > 0)
    24.         {
    25.             Touch touch = Input.GetTouch(0);
    26.  
    27.             if (spawnedObject == null)
    28.             {
    29.                 spawnedObject = Instantiate(m_PlacedPrefab);
    30.                 Vector3 pos = new Vector3(oTransform.position.x, oTransform.position.y - 0.5f, oTransform.position.z + 1);
    31.                 m_SessionOrigin.MakeContentAppearAt(spawnedObject.transform, pos, Quaternion.identity);
    32.                 DisablePlanes();
    33.                 contentPlaced = true;
    34.                 //SetScaleContent();
    35.                 //StartGame();
    36.             }
    37.         }
    38.  
    39.     }
    40.  
    41.     void DisablePlanes()
    42.     {
    43.         ARPlaneManager planeManager = GetComponent<ARPlaneManager>();
    44.         List<ARPlane> allPlanes = new List<ARPlane>();
    45.         planeManager.GetAllPlanes(allPlanes);
    46.         planeManager.enabled = false;
    47.         foreach (ARPlane plane in allPlanes)
    48.         {
    49.             plane.gameObject.SetActive(false);
    50.         }
    51.     }
     
  10. jatreutlein

    jatreutlein

    Joined:
    Dec 14, 2018
    Posts:
    6
    Thanks so much for the help!:) Don't worry your English is good I just wanted to see the code to make sure
     
    Edur-Games likes this.
  11. violetforest

    violetforest

    Joined:
    May 26, 2015
    Posts:
    7
    Hi, I am using this code but I am still getting sliding of the object when instantiated and placed. Is this a problem for anyone else? I am using ARFoundation 1.0 @jatreutlein
     
  12. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
    Hello! I have been testing two ways to place the content.

    1. Placing on a detected plane
    2. Placing in front of the camera without detecting any plane.

    In both cases I use a Pose to establish the desired position and place the content in that Pose.
    I also use MakeContentAppearAt in the content on the ARSessionOrigin.
    It is true that the first option is much more stable than the second. The second option has improved but is still a bit unstable.

    Has anyone tried this technique?
     
  13. Edur-Games

    Edur-Games

    Joined:
    Nov 21, 2017
    Posts:
    44
  14. violetforest

    violetforest

    Joined:
    May 26, 2015
    Posts:
    7
    Hey, thank you. Which script or example of his is the "script on how to place objects without a flat surface PlacementController"? I can't seem to find it. Thank you!