Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Best practice for scaling placed objects with new Ground Plane markerless tracking

Discussion in 'Vuforia' started by AyreGuitar, Jan 19, 2018.

  1. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    What's the best way to deal with placed model scale in Vuforia? Simply scaling the model down is not really an option due to breaking Unity physics and particles.

    I'm trying to transition a project to Vuforia Markerless Tracking / Ground Plane that used the ARInterface scale methods mentioned here (currently working fine with ARKit) so that things like physics and particles work at real world scale but placed model looks smaller, eg for a table top world, by moving the ARCamera further away.

    Does Vuforia have a method to deal with this? Ideally something like NativeVuforiaARInterface that can be slotted into Jimmy Alamparambil's excellent experimental ARInterface example, much like the ones for ARkit and ARCore.

    If not, is it possible to access the device's camera settings through the VuforiaManager without adding an ARCamera to my scene (since such a camera is pretty inflexible thanks to VuforiaBehaviour script that must be present)? I've tried looking through the Vuforia Unity API without much luck.

    Thanks for any help on this.
     
  2. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    Ended up abandoning trying to add Vuforia to ARInterface system and went with a 2 camera system instead.
    The Vuforia Camera handles the device's camera footage and orientation, then a scaled camera shows the Ground Plane placed model at a different distance to simulate scaling it down.

    This unity blog explains it better and has a link to an ARKit Unity project (scroll down to the Mixtures of Scale section). I just adapted it to use Vuforia's ARCamera instead of the ARKit Native camera.
     
  3. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    866
    @Vuforia-Strasza
    pleasea advise, i dont want to abandon vufuria and we need an example project that does scaling and spining/rotation.
     
  4. lqlwle

    lqlwle

    Joined:
    Oct 25, 2017
    Posts:
    1
    This is really important and needs urgently a tutorial/solution!
    I can't figure out how to scale things down with Vuforia.
    @AyreGuitar how did you manage to adapt the 2 camera system? The project in the mentioned blog-post looks horrifyingly difficult :O
     
  5. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    It's not as bad as it seems. Look at the ScaledContent.scene in the Examples folder. The CameraScaler.cs script is the main thing that needs adapting for Vuforia. My version looks like this (see comments for which lines relate to ARKit and how I've changed them to Vuforia)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class VuforiaCameraScaler : MonoBehaviour {
    6.  
    7.     public Camera scaledCamera;
    8.     public Camera vuforiaCamera;
    9.     public GameObject AnchorStage;
    10.     //public Vector3 scaledObjectOrigin; //ARKit version
    11.     public float cameraScale = 1.0f;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         ContentScaleManager.ContentScaleChangedEvent += ContentScaleChanged;
    16.     }
    17.  
    18.     void ContentScaleChanged(float scale, float prevScale)
    19.     {
    20.         cameraScale = scale;
    21.     }
    22.  
    23.  
    24.     void Update() {
    25.         if (scaledCamera != null && cameraScale> 0.0001f && cameraScale < 10000.0f) {
    26.             //Matrix4x4 matrix = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetCameraPose(); //ARKit version
    27.             float invScale = 1.0f/cameraScale;
    28.             //Vector3 cameraPos = UnityARMatrixOps.GetPosition (matrix); //ARKit version
    29.             Vector3 cameraPos = vuforiaCamera.transform.position; //My Vuforia version
    30.             //Vector3 vecAnchorToCamera =  cameraPos - scaledObjectOrigin; //ARKit version
    31.             Vector3 vecAnchorToCamera =  cameraPos - AnchorStage.transform.position; //My Vuforia version
    32.             //scaledCamera.transform.localPosition = scaledObjectOrigin + (vecAnchorToCamera * invScale);
    33.             scaledCamera.transform.localPosition = AnchorStage.transform.position + (vecAnchorToCamera * invScale);
    34.             //scaledCamera.transform.localRotation = UnityARMatrixOps.GetRotation (matrix); //ARKit version
    35.             scaledCamera.transform.localRotation = vuforiaCamera.transform.localRotation; //My Vuforia version
    36.  
    37.  
    38.             //this needs to be adjusted for near/far
    39.             //scaledCamera.projectionMatrix = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetCameraProjection (); //ARKit version
    40.             scaledCamera.projectionMatrix = vuforiaCamera.projectionMatrix; //My Vuforia version
    41.         }
    42.     }
    43. }
    I actually found it easier to do the Vuforia Ground Plane tutorial here, then:
    1. Add another camera (ContentCamera) with the same hierarchy as in the ScaledContent example
    2. Add the above script VuforiaCameraScaler.cs to the ContentCameraManager and the ContentScaleManager.cs to the ContentCameraParent
    3. Make a new Layer called ScaledContent and set your model data to that Layer (ie the Capsule in the Ground Plane which is a child of Ground Plane Stage)
    4. Make the Vuforia ARCamera cull mask show everything except ScaledContent
    5. Make the ContentCamera cull mask only show Scaled Content and Clear Flags as Depth Only
    This way it draws the Vuforia ARCamera first, then draws the ContentCamera at whatever scale is set in VuforiaCameraScaler.cs.

    Hope this helps!
    (Edit - correction in bold - sorry about that!)
     
    Last edited: Feb 1, 2018
    wxxhrt, rocket5tim and Shablagooo like this.
  6. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    @AyreGuitar Thanks for this. I think this is exactly what I need, but I don't understand step 2. Where can I find ContentCameraManager?

    edit: I think I'm missing the examples. Where can I get those?
     
    Last edited: Jan 31, 2018
  7. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    Nevermind I found it. Now my question is, are you grabbing parts of this example project and putting it in your project? If so what parts do I grab from this example?
     
    Last edited: Jan 31, 2018
  8. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    sunnychao - The only thing grabbed from the Scaled Content example is the ContentScaleManager script (corrected in my earlier post now - sorry!). The rest is based on the Vuforia Ground Plane tutorial. You need to make the same hierarchy as the example uses for the ContentCamera, but these can be made from Empty objects in Unity and a Camera.
     
  9. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    @AyreGuitar should the ContentCamera hierarchy live inside ground plane stage? Also, you're supposed to increase the scale of the scaled camera to make the objects smaller right?
     
    Last edited: Feb 1, 2018
  10. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    sunnychao - no ContentCamera hierarchy is totally separate from Ground Plane Stage - shouldn't be child or parent of each other. The VuforiaCameraScaler.cs script has a scale factor to scale your content down (eg use 0.1 to fit a real world scale house to 1/10th scale to fit on a table)
     
  11. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    @AyreGuitar thanks a lot for the help. I got it set up but I don't think the scaling is working. There are no error messages, but everything is life size.

    You said that the VuforiaCameraScaler.cs script has a scale factor to scale your content down, but this script has a field to changed the camera scale. The place to change content scale is in the ContentScaleManager script attached to the ContentCameraParent object. Did you mean to say the latter?

    Nothing appeared on my Ipad when I set the culling mask on the AR camera to not show scaledContent tags so I currently have the AR camera culling mask set to Everything and I see all of my objects. What could be the problem?

    For the VuforiaCameraScaler script, I dragged in my contentCamera into the scaledCamera field, the AR camera into vuforiaCamera field, and Ground Plane Stage into the anchorStage field.
     
    Last edited: Feb 2, 2018
  12. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    @AyreGuitar
    Update: So it looks like the script is working. I changed the camera scale to .1 and see that the camera is moving further away from the ground plane. In the little camera preview, I do see that my character is smaller. Now the problem is after I make a build and run my game on my Ipad, everything stays life size. Is there any other steps that you may have not included in your instructions? I'm guessing my game still uses the AR camera and not the content camera?
     
  13. sunnychao

    sunnychao

    Joined:
    Jun 13, 2017
    Posts:
    6
    @AyreGuitar I got it to work. In the Content Camera object, depth is set to 0 by default. I changed that to 100 and now I see everything.
     
  14. Greken

    Greken

    Joined:
    Nov 16, 2012
    Posts:
    2
    It seems that you have better eyes than me. Where did you find the file ContentScaleManager.cs ?
     
  15. zloph

    zloph

    Joined:
    Sep 20, 2011
    Posts:
    13
  16. Greken

    Greken

    Joined:
    Nov 16, 2012
    Posts:
    2
  17. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    394
    Hi, I got all this working fine in the editor the end - Thank You! However, as soon as I build on an Android device, (ground plane supported) the scaling doesn't work at all. Although all the rotations etc of the ground plane/content seem okay, the content seems to be at its original HUGE scale. Is there anything specific I need to do for Android that is different for the editor?


    Thanks
     
    Last edited: Mar 28, 2018
  18. grimmy

    grimmy

    Joined:
    Feb 2, 2009
    Posts:
    394
    I actually notice that on Android the cameraPos in the VuforiaCameraScaler script is ALWAYS 0,0,0. This variable changes as you move the webcam in the editor version but stays at zero on device. I suspect this is the issue but I dont know why it is the case nor how to fix it. Hopefully someone can help. Thanks
     
  19. walidabazo

    walidabazo

    Joined:
    Jul 8, 2017
    Posts:
    16
    tutorial to create Augmented Reality on unity with vuforia sdk to create multi image target and change size (Scaling ) to game objects (Image or 3D model or Text ) on run time show this video
     
  20. dshewmaker

    dshewmaker

    Joined:
    Oct 31, 2013
    Posts:
    17
    Thanks @AyreGuitar and @sunnychao.

    Got it to work quickly with that same hangup that sunnychao had with depth set to 0. set to 100 and is working. Although it seems like the objects are more detached from the surface than before.
     
  21. dshewmaker

    dshewmaker

    Joined:
    Oct 31, 2013
    Posts:
    17
    Well, this works for the objects, but I have a navigation mesh and on runtime it throws the mesh way out so navmesh does not seem to work with this solution. Even on a new plane navmesh.

    Anyone else have problem with NavMesh and this fix for AR?
     
    Last edited: Apr 5, 2018
  22. FrasloNL

    FrasloNL

    Joined:
    Apr 7, 2017
    Posts:
    1
    I got it working with the solution provided by @AyreGuitar.

    However I also do have the issue that the objects 'hover' a bit more above the surface now, this becomes worse with higher scale values. Also the content 'shakes' a bit, the default AR camera is more stable.

    Does anyone found a solution for those problems? For my project it's unfortunately not good enough to use this for production and I will continue using the very small units for now.
     
  23. dyuldashev

    dyuldashev

    Joined:
    Mar 3, 2016
    Posts:
    61
    Would you please share or explain how you managed to do it in details? I cannot seem to get it done. Maybe, you could post a simple project where you scale and rotate a plain old 3D cube. Thanks.
     
  24. ngjingkang97

    ngjingkang97

    Joined:
    May 15, 2019
    Posts:
    6
    Hey @AyreGuitar I really need help with the implementation. Do you mind guiding me? For the 2Camera use for Vuforia ground plane detection. Thanks!
     
  25. ngjingkang97

    ngjingkang97

    Joined:
    May 15, 2019
    Posts:
    6
    @AyreGuitar I created a terrain which is super freaking huge and I need it to fit inside my Vuforia camera. but I can't scale it down cuz I use sth called River Auto Material which can only be used if the terrain is fully size. So I need to do this scaling down please help!!
     
  26. ngjingkang97

    ngjingkang97

    Joined:
    May 15, 2019
    Posts:
    6
    where do u find contentcameramanager?
     
  27. ngjingkang97

    ngjingkang97

    Joined:
    May 15, 2019
    Posts:
    6
    How do u do the culling mask thingy? and add new layer?
     
  28. AyreGuitar

    AyreGuitar

    Joined:
    Oct 12, 2013
    Posts:
    35
    See zloph's post with link here for where to find ContentCameraManager script.
    Unfortunately I've abandoned this approach and AR/Vuforia in general due to cancellation of a project I was working on, and no longer have the files - sorry! Good luck, though...