Search Unity

Scaling object to ARPlane size

Discussion in 'AR' started by rxmarccall, Aug 14, 2018.

  1. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    For dealing with objects being too small / large for a user, I want to instead scale my object so it will fit on whatever size of ground plane has been found.

    I've come up with the following after some google searches but am still not doing the math correctly it seems. Any help / guidance would be appreciated.

    Code (CSharp):
    1.  
    2. public void ScaleObjectToPlaneSize (GameObject obj, BoundedPlane plane)
    3.     {
    4.             // First get all colliders found in the obj
    5.             var colliders = obj.GetComponentsInChildren<Collider>();
    6.  
    7.             // Iterate over all colliders and encapsulate the bounds
    8.             var bounds = colliders[0].bounds;
    9.             foreach (var c in colliders)
    10.             {
    11.                 bounds.Encapsulate(c.bounds);
    12.             }
    13.  
    14.             var sizeA = bounds.size;
    15.             var sizeB = plane.Size;
    16.             var scale = new Vector2(sizeA.x / sizeB.x, sizeA.y / sizeB.y); //, sizeA.z / sizeB.z);
    17.  
    18.             obj.transform.localScale = scale;
    19.         }
    The other issue I have is that a BoundedPlane.size only returns a Vector2, is this because its only x,z positions with no y sizing?
     
  2. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Your code will only gather the bounds of things that also have colliders, so it might miss objects that only have a render mesh, for example. Scaling the content is not recommended except in the case of trivially simple content, like a cube. That you have multiple child colliders suggests you have more complex content. MakeContentAppearAt is the recommended solution.

    BoundedPlane.Size are the dimensions of the 2D plane. The x and y values do not necessarily map to world space x and y. In the case of a horizontal plane, for instance, they would refer to the world space X and Z dimensions. Not sure if non uniform scaling is what you want. If you do end up scaling the content, you might want to uniformly scale it by the smallest dimension of the BoundedPlane.
     
  3. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    @timmunity
    Thanks for the response.
    MakeContentAppearAt only affects the visual positioning of an object correct?
    Are you suggesting I make my spawned object appear to be far away from the camera view so it "appears" smaller in scale?
     
  4. tdmowrer

    tdmowrer

    Joined:
    Apr 21, 2017
    Posts:
    605
    Yes, it only affects the visual positioning, but it also helps to use this if you plan to scale the ARSessionOrigin, which is what I was suggesting, though I didn't actually say that :)

    If you plan to scale the ARSessionOrigin (the recommended approach), then you need to define the point around which things scale. MakeContentAppearAt effectively sets the ARSessionOrigin to your content's origin, and then adds an extra node in the scene hierarchy to offset the AR camera. I know you've already read my longer explanation of this, but I just wanted to clarify that MakeContentAppearAt is very useful for scaling, since it defines the center about which things are scaled. And you're probably going to want to "place" the content on the detected plane anyway, so this aids with that as well.
     
    rxmarccall likes this.
  5. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    353
    @timmunity
    So I've tested changing my ARSessionOrigin to have a scale of (5,5,5) for example, hoping to make the objects I spawn in the AR space "smaller" visually. After doing this I called "MakeContentAppearAt" with the transform of the object I spawned, but when I do this then rotation and positioning of of the object compared to the ground plane is wacky and doesn't feel connected.

    Am I missing a step?
    Should I be calling MakeContentAppearAt FIRST and then scale my ARSessionOrigin?
     
  6. tredpro

    tredpro

    Joined:
    Nov 18, 2013
    Posts:
    515
    Did that work?