l ๐Ÿค– ์ฝ”๋“œ l

์œ ๋‹ˆํ‹ฐ AR ๋ผ์ธ๋“œ๋กœ์ž‰ / ์นด๋ฉ”๋ผ ์ฆ๊ฐ•ํ˜„์‹ค ๋ผ์ธ๋“œ๋กœ์ž‰ ํ•˜๋Š” ๋ฒ•

์ฝ”๋“œ๊ทธ๋ฆผ 2023. 11. 22.
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š”~

์ฝ”๋“œ๊ทธ๋ฆผ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ์œ ๋‹ˆํ‹ฐ๋กœ ๋ผ์ธ๋“œ๋กœ์ž‰ ํ•˜๋Š” ๋ฒ•์„ ์ •๋ฆฌํ•ด ๋ณผ๊ฒŒ์š”!! ใ…Žใ…Ž

 

์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด๋ง˜๋Œ€๋กœ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆด ์ˆ˜ ์žˆ๋Š” ์•ฑ์ด์—์š”๐Ÿ˜ƒ๐Ÿ‘

 

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

 

 

์šฐ์„  ์œ ๋‹ˆํ‹ฐ๋ถ€ํ„ฐ ์ผœ์ฃผ์„ธ์š”~!!

 

1. ํ”„๋กœ์ ํŠธ ์…‹ํŒ…

 

1-1. window -> Package Manager

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

1-2. AR foundation ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ € ๋‹ค์šด๋กœ๋“œ

 

์ƒ๋‹จ +๋ฒ„ํŠผ ์˜†์— packages: Unity Registry๋กœ ๋ฐ”๊ฟ”์ฃผ์‹œ๊ณ 

๊ฒ€์ƒ‰์œผ๋กœ com.unity.xr.arfoundation์„ ๊ฒ€์ƒ‰ํ•˜์‹œ๋ฉด ๊ธˆ๋ฐฉ ์ฐฉ์˜ค์—†์ด ์ฐพ์œผ์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด, ์Šคํฌ๋กคํ•ด์„œ AR foundation์„ ์ฐพ์œผ์‹  ๋’ค์— ๋‹ค์šด๋กœ๋“œ ํ•ด์ฃผ์„ธ์š”๐Ÿ˜€

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

2. ์œ ๋‹ˆํ‹ฐ์—์„œ ํ”„๋กœ์ ํŠธ ์—์…‹ ์„ค์ •ํ•ด์ฃผ๊ธฐ

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

 

์œ„์™€ ๊ฐ™์€ ๋ชจ์Šต์œผ๋กœ ํŒŒ์ผ๋“ค์„ ์…‹ํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ๊ฑด Script ํŒŒ์ผ์„ ๋งŒ๋“ค ๋•Œ ์ด๋ฆ„์„ ์ˆ˜์ •ํ•˜๋ฉด ๋ฐ˜๋“œ์‹œ class๋ช…๋„ ๋‹ค์‹œ ํ™•์ธํ•˜์…”์•ผ ํ•ด์š”~!

 

3. script ์ฝ”๋“œ์…‹ํŒ…

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

public class Drawing : MonoBehaviour
{

    public Camera cam;
    public Material defaultMaterial;

    private LineRenderer curline;
    private int positionCount = 2;
    private Vector3 PrevPos = Vector3.zero; //์›์  (0,0,0)์„ ์˜๋ฏธํ•จ

    
    // Start is called before the first frame update
    //์ฒ˜์Œ์— ํ•œ๋ฒˆ ์‹คํ–‰
    void Start()
    {
        
    }

    void DrawMouse() 
    {
        //2์ฐจ์› ์ขŒํ‘œ๋ฅผ 3์ฐจ์› ์ขŒํ‘œ๋กœ ๋ฐ”๊พธ๊ฒŒ ๋„์™€์ฃผ๋Š” ํ•จ์ˆ˜.
        //ScreenToWorldPoint ํ•จ์ˆ˜๋Š” ์นด๋ฉ”๋ผ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
        Vector3 mousePos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.3f));


        //GetMouseButtonDown ํ•œ๋ฒˆ ๋ˆ„๋ฅด๋Š” ์ˆœ๊ฐ„ ํ˜ธ์ถœ
        if (Input.GetMouseButtonDown(0))
        {
            //createLIne
            createLIne(mousePos);
        }

        //GetMouseButton ๋ˆ„๋ฅด๊ณ  ์žˆ์„๋•Œ ๊ณ„์† ํ˜ธ์ค„
        else if (Input.GetMouseButton(0)) 
        {
            //connectLine
            connectLine(mousePos);
        }
    }

    void createLIne(Vector3 mousePos) 
    {
        positionCount = 2;
        GameObject line = new GameObject("Line");
        LineRenderer lineRend = line.AddComponent<LineRenderer>();

        line.transform.parent = cam.transform;
        line.transform.position = mousePos;

        lineRend.startWidth = 0.01f; //์„ ์˜ ์‹œ์ž‘๋‘๊ป˜
        lineRend.endWidth = 0.01f; //์„ ์˜ ๋ ๋‘๊ป˜
        lineRend.numCornerVertices = 5;
        lineRend. numCapVertices = 5;
        lineRend.material = defaultMaterial;
        lineRend.SetPosition(0, mousePos);
        lineRend.SetPosition(1, mousePos);

        curline = lineRend;
    }

    void connectLine(Vector3 mousePos) 
    {
        if (PrevPos != null && Mathf.Abs(Vector3.Distance(PrevPos, mousePos)) >= 0.001f)
        {
            PrevPos = mousePos;
            positionCount++;
            curline.positionCount = positionCount;
            curline.SetPosition(positionCount-1, mousePos);

        }
    
    }

  
    // Update is called once per frame
    //๋งค๋ฒˆ ์—…๋ฐ์ดํŠธ ์‹คํ–‰
    void Update()
    {
        DrawMouse();
    }
}

 

 

์ค‘์š” ๋ถ€๋ถ„๋“ค์€ ์ฃผ์„์ฒ˜๋ฆฌํ•˜์—ฌ ์„ค๋ช…๋„ ๊ฐ™์ด ๋„ฃ์–ด๋‘์—ˆ์–ด์š”!

์ดํ•ด์— ๋„์›€์ด ๋˜์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค๐Ÿ˜ƒ

 

4. ์œ ๋‹ˆํ‹ฐ์—์„œ ์ƒ์„ธ์„ค์ •

 

4-1. ์œ ๋‹ˆํ‹ฐ hierarchy์—์„œ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ -> Create Empty

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

๋ผ์ธ๋“œ๋กœ์ž‰์— ๊ด€๋ จ๋œ ํ•จ์ˆ˜์™€ ์นด๋ฉ”๋ผ๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค„๊ฑฐ์˜ˆ์š”!

 

4-2. ์นด๋ฉ”๋ผ, sript ๋งตํ•‘

 

Asset์— ์žˆ๋Š” Drawing script ํŒŒ์ผ์„ hierarchy์— ๋“œ๋ž˜๊ทธ ์•ค ๋“œ๋กญ ํ•ด์ฃผ์„ธ์š”.

๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฐฝ์ด ํ™œ์„ฑํ™” ๋ฉ๋‹ˆ๋‹ค.

 

Camera์—๋Š” XR Origin ์•ˆ์— ์žˆ๋Š” Main Camera๋ฅผ cam์— ์—ฐ๋™ํ•ด์ฃผ์„ธ์š”.

material์€ project -> Assets ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ํด๋ฆญ

material -> default material์„ ํด๋ฆญํ•ด์„œ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”.

camera์— ์—ฐ๋™ํ–ˆ๋˜๊ฑฐ๋ž‘ ๋˜‘๊ฐ™์ด ๋“œ๋ž˜๊ทธ์•ค๋“œ๋กญ์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

 

5. ํ”Œ๋ ˆ์ด

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

์œ ๋‹ˆํ‹ฐ ์ƒ๋‹จ์˜ ํ”Œ๋ ˆ์ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ 

๊ฒŒ์ž„ ํŒจ๋„์—์„œ ๋“œ๋ž˜๊ทธ๋กœ ๋“œ๋กœ์ž‰ ํ•ด๋ณด๋ฉด ๊ฐ์ฒด๊ฐ€ ์˜ฌ๋ผ๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.

 

๋นŒ๋“œ๋ฅผ ํ†ตํ•ด ํ•ธ๋“œํฐ์œผ๋กœ ์ผœ๋ณด๋ฉด ํ™”๋ฉด์—์„œ ๋“œ๋กœ์ž‰์ด ๋˜๋Š” ๋ชจ์Šต๋„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

โ“’์ฝ”๋“œ๊ทธ๋ฆผ ์œ ๋‹ˆํ‹ฐ AR๊ฐœ๋ฐœ

 

 

๊ทธ๋Ÿผ ์˜ค๋Š˜๋„ ๋ชจ๋‘๋ชจ๋‘ ์ฆ๊ฒ๊ณ  ํ–‰๋ณตํ•œ ์ฝ”๋”ฉํ•˜์‹ญ์…”~

์ด์ƒ ์ฝ”๋“œ๊ทธ๋ฆผ์ด์—ˆ์Šต๋‹ˆ๋‹ค~~

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

๐Ÿ’ฒ ์ถ”์ฒœ ๊ธ€