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*

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;




public class Beam : MonoBehaviour
{
    private LineRenderer lineRenderer;
    public float lineDrawSpeed = 6f;
    public GameObject Line;
    public Vector3 ContTransform;
    
    private MLInput.Controller _controller;
    private List<GameObject> _line = new List<GameObject>();
    private int k;


    // Start is called before the first frame update
    void Start()
    {
        k = 0;
        MLInput.Start();
        _controller = MLInput.GetController(MLInput.Hand.Left);
    }

    private void OnDestroy()
    {
        MLInput.Stop();
    }

    // Update is called once per frame
    void Update()
    {
        ContTransform = _controller.Position;
        Debug.Log("t=" + PlanesDemo.t);
        if (PlanesDemo.t>0)
        {
            PlanesDemo planesdemo = GameObject.Find("Planes").GetComponent<PlanesDemo>();
            if(planesdemo!=null)
            {
                Debug.Log("성공");
                if(k>0)
                {
                    Destroy(_line[0]);
                    _line.Remove(_line[0]);
                    k = 0;
                }
                if(planesdemo.newModel!=null)
                {
                    drawline(ContTransform, planesdemo.newModel.transform.position);
                }
                
            }
            else
            {
                Debug.Log("실패");
            }
        }
    }


    void drawline(Vector3 start, Vector3 end)
    {
        GameObject liner = Instantiate(Line, new Vector3((start.x + end.x) / 2, (start.y + end.y) / 2, (start.z + end.z) / 2), Quaternion.identity);
        lineRenderer = liner.GetComponent<LineRenderer>();
        lineRenderer.SetPosition(0, start);
        lineRenderer.SetWidth(.002f, .002f);
        lineRenderer.SetPosition(1, end);
        _line.Add(liner);
        k = 1;
        return;
    }
}



댓글

이 블로그의 인기 게시물

0817 : Hand Tracking

0701_Using server in Unity