본문 바로가기
Unity

[유니티] AR Tracking

by Skull Crusher 2021. 6. 8.
728x90

1. AR foundation

Window -> Package Manager -> unity registry -> AR Foundation, ARCore XR Plugin 설치

Edit -> Project Settings

Switch Platform -> Build

 

2. AR 카메라 추가

XR -> AR session 추가 -> AR Session Origin 추가

 

3. AR 포인트 클라우드

XR -> AR Default Point Cloud 추가 -> Prefabs로 만들기

AR Session Origin -> AR Point Cloud 추가 -> AR Default point cloud 프리팹 추가

 

4. 바닥인식

XR -> AR Default Plane 추가 -> Prefabs 추가

AR Session Origin -> AR Plane Manager 추가

 

5. 큐브 추가

Create Empty -> 3D Cube 추가

Red Material 추가

Prefabs로 만들기

 

C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//AR Foundation 사용
using UnityEngine.XR.ARFoundation;
//AR Subsystems 사용
using UnityEngine.XR.ARSubsystems;

//AR Plane에 지정한 프리팹을 생성 및 배치하는 컴포넌트
public class PlacementOnPlane : MonoBehaviour
{
    //AR 공간에 배치할 프리팹
    [SerializeField] private GameObject placedPrefab;

    [SerializeField] private ARRaycastManager arRaycastManager;

    //터치를 통한 레이캐스트 정보 리스트(동적배열)
    static List<ARRaycastHit> hits = new List<ARRaycastHit>();

    
    // Update is called once per frame
    void Update()
    {
        Vector2 touchPosition = default; //Vector2 touchPosition = Vector2.zero;

        // 화면 터치가 발생했다면
        if (Input.touchCount > 0)
        {
            //첫번째 터치 포인트 위치
            touchPosition = Input.GetTouch(0).position;

            // 화면 터치 위에서 Plane을 향한 레이캐스트를 수행함
            if (arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon))
            {
                //Plane과 충돌한 Plane 위치 정보를 파악
                Pose hitPose = hits[0].pose;

                Instantiate(placedPrefab, hitPose.position, hitPose.rotation);
            }
        }
    }
}

Build and Run 수행

 

6. 큐브 배치 이동

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//AR Foundation 사용
using UnityEngine.XR.ARFoundation;
//AR Subsystems 사용
using UnityEngine.XR.ARSubsystems;

//AR Plane에 지정한 프리팹을 생성 및 배치하는 컴포넌트
public class PlacementOnPlane : MonoBehaviour
{
    //AR 공간에 배치할 프리팹
    [SerializeField] private GameObject placedPrefab;

    [SerializeField] private ARRaycastManager arRaycastManager;

    //터치를 통한 레이캐스트 정보 리스트(동적배열)
    static List<ARRaycastHit> hits = new List<ARRaycastHit>();

    private GameObject spawnObject;

    
    // Update is called once per frame
    void Update()
    {
        Vector2 touchPosition = default; //Vector2 touchPosition = Vector2.zero;

        // 화면 터치가 발생했다면
        if (Input.touchCount > 0)
        {
            //첫번째 터치 포인트 위치
            touchPosition = Input.GetTouch(0).position;

            // 화면 터치 위에서 Plane을 향한 레이캐스트를 수행함
            if (arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon))
            {
                //Plane과 충돌한 Plane 위치 정보를 파악
                Pose hitPose = hits[0].pose;

                if (spawnObject == null)
                {
                    spawnObject = Instantiate(placedPrefab, hitPose.position, hitPose.rotation);
                }
                else
                {
                    spawnObject.transform.position = hitPose.position;
                }
                
            }
        }
    }
}

 

7. 자동차 추가하기

 

 

'Unity' 카테고리의 다른 글

[유니티] ARFoundation  (0) 2021.06.11
[유니티] 포톤게임 만들기 3  (0) 2021.06.08
[유니티] 포톤 게임 만들기  (0) 2021.06.04
[유니티] Photon 배우기  (0) 2021.06.01
[유니티] RPG 게임 만들기2  (0) 2021.05.28

댓글