Search Unity

Scale Prefab to match Image Size

Discussion in 'Handheld AR' started by Voronoi, Sep 13, 2019.

  1. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    263
    I'm using the ARTrackedImageManager to instantiate a Tracked Image Prefab ( a plane ) on top of the image that is being tracked. My expectation would be that grabbing the ARTrackedImage component from this prefab and setting the prefab's localscale to match the image size that I specified would make the plane match the image exactly. That's not happening, the plane is too big and appears to be in front of the image. Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5.  
    6. public class ScaleToTrackedImageSize : MonoBehaviour
    7. {
    8.     public Transform m_plane;
    9.  
    10.     ARTrackedImage m_tracker;
    11.     MeshRenderer m_renderer;
    12.  
    13.     void OnEnable()
    14.     {
    15.         m_renderer = m_plane.GetComponent<MeshRenderer>() as MeshRenderer;
    16.  
    17.         m_renderer.enabled = false;
    18.     }
    19.  
    20.     void Update()
    21.  
    22.     {
    23.         m_tracker = this.GetComponent<ARTrackedImage>() as ARTrackedImage;
    24.         if (m_tracker == null)
    25.             return;
    26.  
    27.  
    28.         EnableAndScale();
    29.     }
    30.  
    31.     void EnableAndScale()
    32.     {
    33.         m_plane.transform.localScale = new Vector3(m_tracker.referenceImage.size.x, m_plane.transform.localScale.y, m_tracker.referenceImage.size.y);
    34.         Debug.Log("Image size is " + m_tracker.referenceImage.size.x + " by " + m_tracker.referenceImage.size.y + " high.");
    35.         m_renderer.enabled = true;
    36.         Debug.Log("Plane size is " + m_plane.transform.localScale.ToString("F4"));
    37.         this.enabled = false;
    38.     }
    39. }
    40.  

    Everything is reporting the numbers I would expect, what am I doing wrong?
     
  2. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    263
    Answering my own question, this same script seems to scale a Cube correctly, but not a Plane (which is too big). Oddly, a VFX graph with a texture as the generator, is scaled exactly twice as large as the image size.