Search code examples
c#imageunity-game-engineanchorarcore

Is there a way to stabilize the 3D object on the Augmented Image


I'm setting up a scene and need my object to appear on one corner of the augmented image.

I've created a script to find the augmented image and set it as an anchor. domPrefab is instantiated on the anchors' position

            if (image.TrackingState == TrackingState.Tracking && visualizer == null)
            {
                //First: detect the augmented image

                Anchor imageAnchor = image.CreateAnchor(image.CenterPose);
                visualizer = Instantiate(AugmentedImageVisualizerPrefab, imageAnchor.transform, imageAnchor);
                visualizer.Image = image;
                m_Visualizers.Add(image.DatabaseIndex, visualizer);

                //Second: Instantiate the prefabs

            GameObject prefab = domPrefab;
            var domObject = Instantiate(prefab, anchor.transform.position, anchor.transform.rotation);
            domObject.transform.Rotate(0, k_ModelRotation, 0, Space.Self);
            domObject.transform.SetParent(anchor.transform, true);
            }

I expect the domPrefab to be instantiated in the center of the augmented image, and when I will learn how to place it in the corner, then have the domPrefab appear in the corner of the image.

The actual result is: the domPrefab appear in the middle of the image but it's not stable. When camera looks around it moves a bit and its' scale changes.


Solution

  • Because you are creating the anchor in the center of the image with this line

    Anchor imageAnchor = image.CreateAnchor(image.CenterPose);

    Then instantiate your visualizer at imageAnchor position and then try to rotate move or something but instead you can do this in your AugmentedImageVisualizer. To do this

    1. Create an empty object and attach AugmentedImageVisualizer script to it.
    2. Create a public GameObject myModel in AugmentedImageVisualizer
    3. Put your model as a child to this object and drag it to myModel in inspector.
    4. Create a prefab with this object
    5. Modify your AugmentedImageVisualizer like this:

      public AugmentedImage Image;
      public GameObject myModel;
      
      public void Update()
      {
      
          float halfWidth = Image.ExtentX / 2;
          float halfHeight = Image.ExtentZ / 2;
      
          // for upper left
          myModel.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.back);
          //for upper right
          myModel.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.back);
          //for lower left
          myModel.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.forward);
          //for lower right
          myModel.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.forward);
      
      }
      
    6. Use this as AugmentedImageVisualizer in your AugmentedImageExampleController

    If you want it to be stable and not change position over time, do the operation in fifth step in Start instead of Update or in another function and make sure it runs only once.

    I hope this helps Good Luck!