์ ๊ฐ ์ด์ฉ๋ค ์ ๋ํฐ๋ฅผ ํ๊ฒ ๋์์๊น์..........
๐ท AR Foundation์ด๋?
์ ๋ํฐ๋ ํฌ๋ก์ค ํ๋ซํผ์ ์ง์ํ๋ AR ํ๋ซํผ์ ๋ง๋ค๊ธฐ ์ํด AR Foundation ํ๋ ์์ํฌ๋ฅผ ์ ๊ณตํ๋ค.
AR Foundation์ ARKit, ARCore, Magic Leap, GoloLens์ ๊ณตํต๋ ๊ธฐ๋ฅ๋ค์ ์ถ์ํํด์ ํ๋์ ํจํค์ง๋ก ๋ง๋ ๊ฒ์ผ๋ก, ๋ด๋ถ์ ์ผ๋ก ARKit์ ARCore๊ฐ ๋ชจ๋ ํตํฉ๋์ด์ ๋ค์ด๊ฐ ์๊ธฐ ๋๋ฌธ์ android, iOS ๋๊ฐ์ง ํํ์ AR ์ฑ์ ์ ์ ํ ์ ์๋ ๊ฒ์ด๋ค.
์ฆ, AR Foundation์ผ๋ก ์ฑ์ ๋น๋ํ๋ฉด ์ด๋ค ๊ธฐ๊ธฐ๋ ์๊ด์์ด ๊ฐ ํ๋ซํผ์์ ์ง์ํ๋ ๊ธฐ๋ฅ๋ค์ ํ์ฉํ ์ ์๋ค!
๐ท AR Foundation ์๋ ๊ณผ์
AR์ ์๋ ๊ณผ์ ์ ํตํด ์๋ํ๋ค.
- AR Session์ ์์ฒญํ๋ค.
- ์นด๋ฉ๋ผ ํ๋์จ์ด๋ฅผ ์ผ ๋ค.
- IMU ํ๋์จ์ด๋ฅผ ์ผ ๋ค.
- ์นด๋ฉ๋ผ์ Motion Sensor๋ AR Frame๋ง๋ค ๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.
- AR Session์ด ์ด๋ฆฐ๋ค. => Session์ด ์ด๋ฆฐ ์๊ฐ์ ์นด๋ฉ๋ผ ์์น๋ฅผ ์์ ์ผ๋ก ๊ฐ์ ์๋ ์ขํ๊ณ๊ฐ ํ์ฑ๋๋ค.
- ๊ทธ ์ขํ๊ณ์์ VIO๋ฅผ ํตํด ํ์ฌ ์นด๋ฉ๋ผ์ ์์น์ ํ์ ๊ฐ์ ์ ๋ฐ์ดํธํ๋ค.
- ์นด๋ฉ๋ผ ์ด๋ฏธ์ง๋ฅผ ๋ถ์ํด์, ๋ฌผ์ฒด๋ฅผ ๊ฐ์งํ๋ค.
- (๋ฑ๋ก๋ ์ฌ๋ฌผ์ด ์๋๋ฉด) ์๋ก์ด ์ต์ปค๋ฅผ ๋ง๋ค์ด์ ์ฌ๋ฌผ์ ์์น์ํจ๋ค.
- (์ด๋ฏธ ๋ฑ๋ก๋ ์ฌ๋ฌผ์ด๋ฉด) ์ ๋ฐ์ดํธ ๋๋ ๋ณํฉํ๋ค.
- 7~9 ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
[์ถ์ฒ] https://zerosin13.tistory.com/30
๐ท AR Foundation ์ธํ
+ Unity Student Plan์ ์ ์ฒญํ๋ฉด ๋ฌด๋ฃ๋ก Unity Pro ๋ฒ์ ์ ์ด์ฉํ ์ ์์ต๋๋ค. ์ด ๋ ์ธ์ฆ ์๋ฅ๋ฅผ ์ ๋ก๋ ํ๋ผ๊ณ ํ๋๋ฐ,, ๋ ๋ด๊ธฐ ์ซ์ด์ ์ ๋ชฐ๋ ํ๊ณ ํ๊ธ๋ก ๋ ๋ฑ๋ก๊ธ๋ฉ์ ํ์ธ์ pdf๋ฅผ ์ฌ๋ ธ๋๋, ์์ด๊ฐ ์๋๋ผ 2-3์ผ ์ ๋ ์์๋๊ธด ํ์ง๋ง ๋ฌด์ฌํ accept ๋์๋ต๋๋ค~! ํ๊ธ ํ์ผ๋ ๋๋ค๋ ๊ฟํ!
ํน๊ฐ์งฑํฉ์ ์๋๊ป์ ์ฌ๋ ค์ฃผ์ ์์์ ์ฐธ๊ณ ํ๋ฉด ์์ฃผ ์ฝ๊ฒ ์งํํ ์ ์๋ค!
1. AR Foundation ์ค์น
2. ๋น๋ ์ค์
3. AR ์ด๊ธฐ ํ๊ฒฝ ์ธํ
AR Session๊ณผ AR Session Origin์ ์ค์ ํด์ผ ํ๋ค.
AR Session์ ๊ธฐ๊ธฐ์ ์นด๋ฉ๋ผ, ์ผ์, AR ํ๋ซํผ(ex. ARCore)๊ณผ ์ฐ๊ฒฐ๋์ด ์ค์ ํ๊ฒฝ์ ์ถ์ ํ๊ณ ์ํธ์์ฉ์ ๊ฐ๋ฅํ๊ฒ ํ๋ AR ์ธ์ ์ ๊ด๋ฆฌํ๋ ์ญํ ์ ํ๋ค. AR์ ์ ์ฒด์ ์ธ ๋ผ์ดํ์ฌ์ดํด์ ๊ด๋ฆฌํ๊ณ , AR ์ง์ ๊ฐ๋ฅ ์ฌ๋ถ, Session ์คํ ์ฌ๋ถ ๋ฑ์ ๊ด๋ฆฌํ๋ ๊ฒ์ด ์ฃผ์ ์ญํ ์ด๋ค. AR ์ฑ์ ๋ง๋ค๊ธฐ ์ํด์ ๋ฐ๋์ ์ถ๊ฐํด์ผ ํ๋ ์์์ด๋ค!
AR Session Origin์ AR ์์๋ค์ Unity ๊ณต๊ฐ์ transformation ํ๊ธฐ ์ํด ํ์ํ ์ปดํฌ๋ํธ๋ก, ๋ชจ๋ ์์๋ค์ scale์ ์กฐ์ ํ ๋ ๋ง์ด ์ฌ์ฉํ๋ค(scene์ ๋ชจ๋ ์์๋ค์ ์ด๋์ํค๋ ๋์ , ์นด๋ฉ๋ผ๋ฅผ ์์ง์ด๋ ๋๋). ํ์์ AR Camera๊ฐ ํฌํจ๋์ด ์๋๋ฐ, ์ฌ์ฉ์ ์ ์ฅ์์๋ AR Session Origin==์ค์ ์นด๋ฉ๋ผ์ ์์น ๋ผ๊ณ ์ฌ๊ฒจ์ง๋ค. ๋ฐ๋ผ์ ์ด๋ฅผ ์ถ๊ฐํ๋ฉด ๊ธฐ์กด์ ์๋ Main Camera๋ ์ญ์ ํด์ผ ํ๋ค.
4. ๋น๋
์์์ ๊ทธ๋๋ก ๋ฐ๋ผํ๊ณ ๋น๋๋ฅผ ํ๋๋ฐ, ์ฑ ์ค์น ์๋ฃ ํ ์นด๋ฉ๋ผ ๊ถํ์ ํ์ฉํ๋ฉด ์ฑ์ด ๋ฐ๋ก ์ข ๋ฃ๋๋ ํ์์ด ๋ฐ์ํ๋ค.
์ด๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์๋์ ๊ฐ๋ค.
Project - Player - other settings ์์
- Scripting Backend๋ฅผ IL2CPP๋ก ๋ณ๊ฒฝํ๋ค.
- Api Compatibility Level์ .Net Standard 2.1๋ก ๋ณ๊ฒฝํ๋ค.
- Target Architectures์ ARM64๋ฅผ ์ฒดํฌํ๋ค. (ARM v7์ ์๋ ์ฒดํฌ๋์ด ์๊ธธ๋ ๋๋๋๋ฐ, ๋ค๋ฅธ ๋ธ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด ์ฒดํฌ ํด์ ํ๋ผ๋ ๋ง๋ ์๊ณ .. ์์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค)
๐ท ํ๋ฉด ์ธ์
AR Session์ด ๋ชจ๋ ๊ฒ์ ๊ธฐ์ค์ ์ด ๋๋ฏ๋ก, ์ด ๋ด๋ถ์ ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋ค.
1. Hierarchy - Scene์ ํ์์ XR - AR Default Point Cloud, AR Default Plane ์ถ๊ฐ
- AR Default Point Cloud
- ์์์์ ์ป์ด์ง๋ ํน์ง์ ๋ค์ ๋ ธ๋์ ํฌ์ธํธ๋ก ํ์ํ๋ ์ปดํฌ๋ํธ
- AR Default Plane
- AR Default Point Cloud์์ ์ฐพ์ ํน์ง์ ๋ค์ ๊ธฐ๋ฐ์ผ๋ก ํด์ ํ๋ฉด์ ์ฐพ๋ ์ปดํฌ๋ํธ
2. AR Session์ ์ฐ๊ฒฐ
- Add Component - AR Point Cloud Manager ์ถ๊ฐ
- Scene์ ์ถ๊ฐํด๋์ AR Default Point Cloud๋ฅผ Prefeb์ ์ฐ๊ฒฐ
- ⇒ ์์์ ์ธ์๋ point cloud๊ฐ ์นด๋ฉ๋ผ๋ฅผ ์ค์ ์ผ๋ก ํด์ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ค
- Add Component - AR Plane Manager ์ถ๊ฐ
- Scene์ ์ถ๊ฐํด๋์ AR Default Plane๋ฅผ Prefeb์ ์ฐ๊ฒฐ
๐ท ํ๋ฉด์ 3D Object ๋ฐฐ์น
AR Raycast Manager์ ์ด์ฉํด์ ray๋ฅผ ์๊ณ , ray๊ฐ ๋ฟ๋ ๊ทธ ๊ณต๊ฐ์ 3D object๋ฅผ ๋ฐฐ์นํด๋ณด์!
1. AR Session - Add Component - AR Raycast Manager ์ถ๊ฐ
- Raycast๋,
- 3์ฐจ์ ๊ณต๊ฐ์์ ์ด๋ ํ ์ ์์ ray๋ฅผ ์ ํด์ง ๋ฐฉํฅ์ผ๋ก ์ด์ ray์ ์ถฉ๋๋๋ ๊ฐ์ฒด๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ
- Unity Physics์ ์ด๋ฏธ Raycast ์ธํฐํ์ด์ค๊ฐ ์ ์๋์ด ์์
Unity Physics์ ์ด๋ฏธ ์ ์๋ Raycast ์ธํฐํ์ด์ค - ๊ทธ๋ฌ๋ AR์ ๊ฐ์ง ๊ฐ๋ฅํ object๊ฐ ๋ฐ๋์ ๋ฌผ๋ฆฌ์ ๊ณต๊ฐ์๋ง ์กด์ฌํ๋ ๊ฒ ์๋๊ธฐ ๋๋ฌธ์, AR Foundation์์๋ Raycast์ ๋น์ทํ ๋ณ๋์ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๊ณ ์์
- ARRaycastManager.cs์ Raycast ํจ์ ์ฌ์ฉ
- screenPoint, hitResults, TrackableType์ ์ด์ฉํด์ ๊ณต๊ฐ ์์ ์๋ ์ด๋ค ์ค๋ธ์ ํธ๋ค์ ray๋ฅผ ์ด์ ํ์ธํ๊ณ , ๊ทธ ์ค๋ธ์ ํธ๋ค์ ๋ฐํ ๋ฐ๋ ์ผ์ ํ๋ ํจ์
- screenPoint, hitResults, TrackableType์ ์ด์ฉํด์ ๊ณต๊ฐ ์์ ์๋ ์ด๋ค ์ค๋ธ์ ํธ๋ค์ ray๋ฅผ ์ด์ ํ์ธํ๊ณ , ๊ทธ ์ค๋ธ์ ํธ๋ค์ ๋ฐํ ๋ฐ๋ ์ผ์ ํ๋ ํจ์
2. Import New Asset - .obj / .fbx ํ์ผ import ํ Scene์ ๋ฐฐ์น
3. Hierarchy - Scene์ ํ์์ Empty object ์ถ๊ฐ
- Add Component - ARPlaceOnPlane script ์ถ๊ฐ
- ์ด๋ ๊ณต๊ฐ์ ray๋ฅผ ์ ๊ฑด์ง, ๋ฐํ๋ ๊ฐ์ฒด๊ฐ ์๋ค๋ฉด ๊ทธ ๊ฐ์ฒด๋ฅผ ์ด๋ค ์์ผ๋ก ์ฌ์ฉํ ๊ฑด์ง ํธ๋ค๋ง ํ๋ ์ฝ๋ ์์ฑ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARPlaceOnPlane : MonoBehaviour
{
public ARRaycastManager arRaycaster;
public GameObject placeObject;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdateCenterObject();
}
private void UpdateCenterObject()
{
Vector3 ScreenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
List<ARRaycastHit> hits = new List<ARRaycastHit>();
arRaycaster.Raycast(ScreenCenter, hits, TrackableType.Planes);
if(hits.Count > 0)
{
Pose placementPose = hits[0].pose;
placeObject.SetActive(true);
placeObject.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
}
}
}
- ํ๋
- ARRaycastManager์ public์ผ๋ก ์ ์ธ
- unity editor์์ AR Session Origin์ ์ฐ๊ฒฐ
- ์ด๋ค object๋ฅผ ๋ฐฐ์นํ ๊ฑด์ง ์ ์ ํ๋ placeObject ๊ฐ์ฒด๋ฅผ public์ผ๋ก ์ ์ธ
- unity editor์์ Scene์ ๋ฐฐ์นํ 3D object๋ฅผ ์ฐ๊ฒฐ
- ARRaycastManager์ public์ผ๋ก ์ ์ธ
- UpdateCenterObject
- ํ๋ฉด์ ์ผํฐ ๋ถ๋ถ์ object๋ฅผ ๋งค ํ๋ ์๋ง๋ค ์์น์ํค๋ ํจ์
- Vector3 ScreenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
- screen์ center point ๋ฐ์์ด
- ViewportToScreenPoint : point๋ฅผ viewport ์ขํ์์ screen ์ขํ๋ก ๋ณํ
- arRaycaster.Raycast(ScreenCenter, hits, TrackableType.Planes);
- Raycast(์ด๋ ๋ฐฉํฅ์ผ๋ก ray๋ฅผ ์ ๊ฑด์ง, ray๋ฅผ ์ด์ ์ถฉ๋๋๋ ๊ฐ์ฒด๋ค์ list ํํ๋ก ๋ฐํ(, trackable object type ์ง์ ))
- ScreenCenter ๋ฐฉํฅ์ผ๋ก ray๋ฅผ ์๊ณ , Planes์ด๋ผ๋ ๊ฐ์ฒด์ ray๊ฐ ๋ฟ์ผ๋ฉด hits์ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋ฐํ ๋ฐ์
- if(hits.Count > 0)
- ๋ญ๊ฐ ๋ถ๋ชํ ๊ฐ์ฒด๊ฐ ์์ผ๋ฉด ๊ฑฐ๊ธฐ์ ๋ฐฐ์นํ์
- Pose placementPose = hits[0].pose;
- ray์ ๋ถ๋ชํ ๊ฐ์ฒด ์ค ๊ฐ์ฅ ๋จผ์ ๋ถ๋ชํ ๊ฐ์ฒด์ ์์น๋ฅผ placementPose์ ์ ์ฅ
- placeObject.SetActive(true);
- ์ ๋ณด์ด๋ 3D object๋ฅผ ๋์ ๋ณด์ด๊ฒ ๋ฐ๊ฟ
- placeObject.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
- 3D object์ ์์น(position๊ณผ rotation) ์ค์