Reputation: 752
Today I started trying some things with Vuforia 7 and it's Ground Plane / Plane Finder capabilities. I did the simple example found on Vuforia website and now I am trying to create a simple app with it.
What I want to accomplish is that, when the Vuforia PlaneFinderBehaviour has detected a plane, I want to display a toolbox onto the screen where the user can select between a few primitive 3D GameObjects (Cube, Sphere, Cilinder). After selecting the desired 3D GameObject, I want it to appear onto the ground that was detected by Vuforia. Next, when walking around and Vuforia detects ground again, the same toolbox needs to appear where the user can select a different type of 3D GameObject to be placed onto the ground.
After messing around in Unity, I still haven't managed to create my own PlaneFinderBehaviour C# script with a method that needs to be triggered when Vuforia detects ground.
What I have done:
PlaneFinderBehaviour
and has a method called CustomPerformHitTest
which just prints something into the console.using System.Collections; using System.Collections.Generic; using UnityEngine; using Vuforia; public class CustomPlaneFinderBehaviour : PlaneFinderBehaviour { public void CustomPerformHitTest(Vector2 screenPosition) { print("CustomPlaneFinderBehaviour > CustomPerformHitTest has been triggered"); } }
PlaneFinderBehaviour
of the PlaneFinder GameObject with my Custom scriptCould someone send me in the right direction and explain how Vuforia's plane detection works in order for me to understand what I have done wrong.
Thanks!
Upvotes: 3
Views: 1476
Reputation: 346
You don't need a custom plane finder behaviour to achieve this. Just use the basic event OnAutomaticHitTest
of the standard PlaneFinderBehaviour
.
OnAutomaticHitTest
fires every frame a plane is detected. So you can use this event to toggle your toolbox. (Log somehow if the event was fired during the last frame. E.g. You could log the frame count if the event was fired and then compare it in a LateUpdate
function. If the frame count matches, you know a plane is currently detected, if not ... well then not.)
Then, if your toolbox is shown and your user interacts with it, just trigger the public method PerformHitTest
of PlaneFinderBehaviour
. This method shoots a ray from the given screen position and returns the corresponding position on the ground plane. If this method succeeds the other event of PlaneFinderBehaviour
called OnInteractiveHitTest
gets fired.
Listen to this event and then position/spawn content like this:
public class CustomContentPositioningBehaviour : MonoBehaviour
{
public GameObject yourContentPrefab;
private PositionalDeviceTracker deviceTracker;
private GameObject previousAnchor;
public void Awake()
{
VuforiaARController.Instance.RegisterVuforiaStartedCallback( OnVuforiaStarted );
}
public void OnDestroy()
{
VuforiaARController.Instance.UnregisterVuforiaStartedCallback( OnVuforiaStarted );
}
private void OnVuforiaStarted()
{
deviceTracker = TrackerManager.Instance.GetTracker<PositionalDeviceTracker>();
}
public void SpawnContent( HitTestResult result )
{
if( result == null || yourContentPrefab == null)
{
Debug.LogWarning( "Hit test is invalid or content is not set" );
return;
}
var anchor = deviceTracker.CreatePlaneAnchor( Guid.NewGuid().ToString(), result );
if( anchor != null )
{
anchor.transform.parent = this.gameObject.transform;
GameObject content = Instantiate( yourContentPrefab );
content.transform.parent = anchor.transform;
content.transform.localPosition = Vector3.zero;
content.transform.localRotation = Quaternion.identity;
content.SetActive( true );
}
if( previousAnchor != null )
{
Destroy( previousAnchor );
}
previousAnchor = anchor;
}
}
SpawnContent
gets called from the OnInteractiveHitTest
event handler. If you have different types of content you need of course to distinguish between them in this method.
Upvotes: 4