Create objects with ground plane detection only once with Vuforia & Unity
Asked Answered
P

6

5

I am trying to create an AR app using Unity & Vuforia. I have a 3D model that needs to be spawned when ground plane is detected.But this needs to happen only once. The way Vuforia work is, it keeps on spawning objects when new plane is detected. So what i need to do is either detect plane only once or spawn the object only once. As i am new to Unity, i need help doing this. Great if someone could tell me what i need to do to achieve this.

Proteinase answered 6/2, 2018 at 5:20 Comment(0)
N
3

In your app you should have a Plane Finder object somewhere with the following properties set by default
enter image description here

The Plane Finder object has a Behaviour component attached that calls a Position Content method if a plane was found. That method belongs to the Content Positioning Behaviour and it makes an instance (Clone) of your Ground Plane Stage. In order to avoid more than one instance you should import the vuforia Deploy Stage Once script located here: https://library.vuforia.com/articles/Solution/ground-plane-guide.html and you should change the Plane Finder Behaviour as the following:enter image description here

Nutritive answered 6/2, 2018 at 9:17 Comment(12)
That did not fix my issue. When i click on a new plane, a new model pops up. Now i have another issue too...the model is shown before the camera even before ground detection. and disappears and appears again normally when the ground plane is detected.Proteinase
@AnupGPrasad Quote: "The way Vuforia work is, it keeps on spawning objects when new plane is detected." you did not say anything about clicking. Nevertheless I know what you are talking about because I already made a workaround that issue for myself. Hint: go to this page library.vuforia.com/articles/Solution/ground-plane-guide.html and look at Number 15Nutritive
Okay. I have an interactive button on a model with action. Will that work without conflicting this if i replace the script?Proteinase
@AnupGPrasad I didn't understand the question.Nutritive
I have a 3d model and i have added a UI button to it. There is an associated action to it too. My question is the step 15 you mentioned...will it interfere with the button click? Because what the script does is first click: spawn an object; subsequent clicks: Change the position of object. But now i have done the step 15. But now i am seeing only the marker and clicking on it doesnot spawn the object.Proteinase
About the interfering part, it depends on what the button's action does. What marker are you talking about? are you testing this on a mobile device or editor mode?Nutritive
I am using a mobile device. As a temporary fix, this is what i have done: Place the 3d object outside the Ground Plane Stage. This will spawn the object only once. I don't think this is the correct method.But it does fix the issue.Proteinase
@AnupGPrasad Hey, glad it worked, could you please mark my answer as the solution. Regarding your UI problem, please either update your question or make a new question and I can help you. Thanks!Nutritive
All issues are fixed. Its just that sometimes after ground detection, the object is not spawned. Sometimes it works..sometimes not. Any idea?Proteinase
@AnupGPrasad I pretty much have the same for me, I think its from Vuforia.Nutritive
Vuforia docs lack the crucial step of updating the function of PlaneFinder for onInteractiveHitTest. Thanks for making it explicit using the screenshot.Giff
@Giff Glad you found it useful.Nutritive
M
9

Vuforia has updated.Now There is no DeploymentStageOnce script.Inorder to stop duplicating while we touch, we have to turn off Duplicate Stage in Content Positioning Behaviour (Script)Check the Inspector when we click Plane Finder. enter image description here

Moonstruck answered 4/6, 2018 at 9:51 Comment(1)
Done that. Still deploying the object multiple times.Flinch
N
3

In your app you should have a Plane Finder object somewhere with the following properties set by default
enter image description here

The Plane Finder object has a Behaviour component attached that calls a Position Content method if a plane was found. That method belongs to the Content Positioning Behaviour and it makes an instance (Clone) of your Ground Plane Stage. In order to avoid more than one instance you should import the vuforia Deploy Stage Once script located here: https://library.vuforia.com/articles/Solution/ground-plane-guide.html and you should change the Plane Finder Behaviour as the following:enter image description here

Nutritive answered 6/2, 2018 at 9:17 Comment(12)
That did not fix my issue. When i click on a new plane, a new model pops up. Now i have another issue too...the model is shown before the camera even before ground detection. and disappears and appears again normally when the ground plane is detected.Proteinase
@AnupGPrasad Quote: "The way Vuforia work is, it keeps on spawning objects when new plane is detected." you did not say anything about clicking. Nevertheless I know what you are talking about because I already made a workaround that issue for myself. Hint: go to this page library.vuforia.com/articles/Solution/ground-plane-guide.html and look at Number 15Nutritive
Okay. I have an interactive button on a model with action. Will that work without conflicting this if i replace the script?Proteinase
@AnupGPrasad I didn't understand the question.Nutritive
I have a 3d model and i have added a UI button to it. There is an associated action to it too. My question is the step 15 you mentioned...will it interfere with the button click? Because what the script does is first click: spawn an object; subsequent clicks: Change the position of object. But now i have done the step 15. But now i am seeing only the marker and clicking on it doesnot spawn the object.Proteinase
About the interfering part, it depends on what the button's action does. What marker are you talking about? are you testing this on a mobile device or editor mode?Nutritive
I am using a mobile device. As a temporary fix, this is what i have done: Place the 3d object outside the Ground Plane Stage. This will spawn the object only once. I don't think this is the correct method.But it does fix the issue.Proteinase
@AnupGPrasad Hey, glad it worked, could you please mark my answer as the solution. Regarding your UI problem, please either update your question or make a new question and I can help you. Thanks!Nutritive
All issues are fixed. Its just that sometimes after ground detection, the object is not spawned. Sometimes it works..sometimes not. Any idea?Proteinase
@AnupGPrasad I pretty much have the same for me, I think its from Vuforia.Nutritive
Vuforia docs lack the crucial step of updating the function of PlaneFinder for onInteractiveHitTest. Thanks for making it explicit using the screenshot.Giff
@Giff Glad you found it useful.Nutritive
H
2

I struggled a long with it, in short we must disable AnchorInputListenerBehaviour after hit.

  1. I attached a new script on PlaneFinder with this code below:

    <!-- language-all: c# -->
    public void OnInteractiveHitTest(HitTestResult result)
    {
        var listenerBehaviour = GetComponent<AnchorInputListenerBehaviour>();
        if (listenerBehaviour != null)
        {
            listenerBehaviour.enabled = false;
        }
     }
    
  2. I added event on Plane Finder Behavior

enter image description here

That's all, I hope it will be useful.

Hoashis answered 2/11, 2018 at 23:44 Comment(1)
I tried using the script but couldn't get it to work. Visual was giving me a bunch of errors.Trixie
B
1

For Updated Versions:

enter image description here

go to "Advanced" setting and "On Interactive Hit Test" script -> Select "Off" option for the script.

Backfill answered 23/6, 2018 at 10:56 Comment(0)
D
1

Most of the answers are correct but kind of obsolete, the correct way to do that is by code.

Create for example a gameObject called GameManager and pass the GroundPlaneStage and a prefab of the object you want to spawn to a script attached to that GameManager for example call it GameManagerScript.cs, and create a small function called spawnObjects which does the following:

public class SceneManagerScript : MonoBehaviour {

    public GameObject objPrefab;
    public GameObject ground;
    private int count = 0;

    public void spawnObject() {
        Instantiate(objPrefab, new Vector3(count, 0, 0), Quaternion.identity, ground.transform);
    count += 2; 
    }
}

then after that go to the PlaneFinder specifically to the PlaneFinderBehaviour.cs component you will have callbacks for OnInteractiveHitTest and OnAutomaticHitTest, in your case you need the OnAutomativeHitTest, click + and add a new callback (the function spawnObject in code above like in the image below)

enter image description here

  1. also when you instantiate the object of your preference via the prefab don't forget to write the proper position updates to prevent the objects from getting added in the same position
  2. also don't forget to make the GroundPlaneStage the parent of the object and realize that the position you are adding in the Instantiate() function is relative to that parent (GroundPlaneStage which is represented in the code above with the variable ground)
  3. Finally don't forget to uncheck Duplicate Stage from the "Content Positioning Behaviour" component in the Plane Finder as shown in the picture below:

enter image description here

I hope that helps

Dollie answered 18/10, 2019 at 2:30 Comment(0)
P
0

please try the vuforia website for this problem

Introduction to Ground Plane in Unity

Pelasgian answered 29/4, 2019 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.