12/29 : placing Gameobject on plane
Request : put GameObject on the middle of the pink foam in Real world
working time : 9 a.m. ~ 12a.m. / 2 p.m. ~ 4 p.m. / 8p.m. ~
To easily find where the GameObject is, I added a function to draw a line between the center of the object and controller. It took a long time as I learned how to run two C# scripts at once.
First script : PlanesDemo
This script use the MLPlane function to find the plane. We can get center point, rotation, width and height. Using those information, it is able to put our model in the center of the plane which was recognized.
*code*
using System.C
ollections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
public class PlanesDemo : MonoBehaviour
{
public Transform BBoxTransform;
public Vector3 BBoxExtents;
public GameObject PlaneGameObject;
public GameObject ObjectGameObject;
public static int t;
private MLPlanes.QueryParams _queryParams = new MLPlanes.QueryParams();
public MLPlanes.QueryFlags QueryFlags;
public GameObject newModel;
private float timeout = 5f;
private float timeSinceLastRequest = 0f;
private List<GameObject> _planeCache = new List<GameObject>();
private List<GameObject> _objectCache = new List<GameObject>();
void Start()
{
MLPlanes.Start();
t = 0;
}
private void OnDestroy()
{
MLPlanes.Stop();
}
void Update()
{
//Debug.Log("실행중 + 시간 : " + timeSinceLastRequest);
timeSinceLastRequest += Time.deltaTime;
if (timeSinceLastRequest > timeout)
{
Debug.Log("들어오긴 함");
timeSinceLastRequest = 0f;
RequestPlanes();
}
}
void RequestPlanes()
{
Debug.Log("Check 1");
_queryParams.Flags = QueryFlags;
_queryParams.MaxResults = 1;
_queryParams.BoundsCenter = BBoxTransform.position;
_queryParams.BoundsRotation = BBoxTransform.rotation;
_queryParams.BoundsExtents = BBoxExtents;
MLPlanes.GetPlanes(_queryParams, HandleOnReceivedPlanes);
}
private void HandleOnReceivedPlanes(MLResult result, MLPlanes.Plane[] planes, MLPlanes.Boundaries[] boundaries)
{
Debug.Log("Check 2");
for (int i = _objectCache.Count - 1; i >= 0; --i)
{
//Destroy(_planeCache[i]);
//_planeCache.Remove(_planeCache[i]);
Destroy(_objectCache[i]);
_objectCache.Remove(_objectCache[i]);
Debug.Log("Check 3");
}
Debug.Log("Check 3.5");
if(planes.Length >0 )
{
newModel = Instantiate(ObjectGameObject);
newModel.transform.position = planes[0].Center;
newModel.transform.rotation = planes[0].Rotation;
newModel.transform.localScale = new Vector3(planes[0].Width, planes[0].Height, 0.005f); // Set plane scale
t = 1;
_objectCache.Add(newModel);
}
}
}
Second script : Beam
This code is to control the line. Because of the structure in first code, it was unable to write both functions in one script. So I divided scripts in two and take the value of first script on second script.
*code*
댓글
댓글 쓰기