03/13 : Plane Extraction + Grid Generation

 

바닥면을 인식해서 그리드가 될 사각형을 배치하는 프로그램을 작성하였다. 

plane extraction 예제를 참고했는데, 원래 이 예제는 센서를 이용해 모든 평면을 인식해 Virtual twin을 만들어주는 것이지만 바닥면만 인식하도록 변경해서 사용하였다. 


작성한 코드와 완성 사진은 다음과 같다. 




내일은 이 grid의 오브젝트 몇개를 선택하고 그 위치에 맞게 virtual twin을 피팅하는 것을 구현할 것이다. 


+. 사실 plane extraction이 현실에서도 잘 작동한다면 그냥 모델을 불러오기만 해도 될 것 같다. 


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

public class PlanesDemo : MonoBehaviour
{
    public Transform BBoxTransform;
    public Vector3 BBoxExtents;
    public GameObject PlaneGameObject;
    public GameObject Cube_;
    public GameObject Cube1;
    public GameObject Cube2;

    private MLPlanes.QueryParams _queryParams = new MLPlanes.QueryParams();
    public MLPlanes.QueryFlags QueryFlags;

    private float timeout = 5f;
    private float timeSinceLastRequest = 0f;
    private List<GameObject> _planeCache = new List<GameObject>();

    void Start()
    {
        MLPlanes.Start();
    }

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


    void Update()
    {
        timeSinceLastRequest += Time.deltaTime;
        if (timeSinceLastRequest > timeout)
        {
            timeSinceLastRequest = 0f;
            RequestPlanes();
        }
    }

    void RequestPlanes()
    {
        _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)
    {
        for (int i = _planeCache.Count - 1; i >= 0; --i)
        {
            Destroy(_planeCache[i]);
            _planeCache.Remove(_planeCache[i]);
        }

        GameObject newPlane;
        GameObject _obj;
        _obj = Instantiate(Cube2, new Vector3(1, 0, 0), Quaternion.identity);
        _obj = Instantiate(Cube1, new Vector3(-1, 0, 0), Quaternion.identity);
        for (int i = 0; i < planes.Length; ++i)
        {
            newPlane = Instantiate(PlaneGameObject);
            newPlane.transform.position = planes[i].Center;
            newPlane.transform.rotation = planes[i].Rotation;
            newPlane.transform.localScale = new Vector3(planes[i].Width, planes[i].Height, 1f); // Set plane scale

            Debug.Log("width : " + planes[i].Width + " Height : " + planes[i].Height);
            Debug.Log("Plane.x : " + planes[i].Center.x + " Plane.y : " + planes[i].Center.y + " Plane.z : " + planes[i].Center.z);
            Debug.Log("Rotation.x : " + planes[i].Rotation.x + " Rotation.y : " + planes[i].Rotation.y+ " Rotation.z : " + planes[i].Rotation.z);

            Drawgrid(planes[i].Center, planes[i].Width, planes[i].Height);
            _planeCache.Add(newPlane);
        }
    }

    private void Drawgrid(Vector3 center, float w, float h)
    {
        GameObject _obj; 
        for(float i= 0f; i<w/2; i=i+0.1f)
        {
            for(float j=0; j<h/2; j= j+0.1f)
            {
                _obj = Instantiate(Cube1, new Vector3(center.x + i, center.y, center.z + j), Quaternion.identity);
                _obj = Instantiate(Cube1, new Vector3(center.x + i, center.y, center.z - j), Quaternion.identity);
                _obj = Instantiate(Cube1, new Vector3(center.x - i, center.y, center.z + j), Quaternion.identity);
                _obj = Instantiate(Cube1, new Vector3(center.x - i, center.y, center.z - j), Quaternion.identity);
            }
        }
    }

}



댓글

이 블로그의 인기 게시물

0817 : Hand Tracking

0701_Using server in Unity