10/30 : Perceptron Multilayer Visualization 프로젝트 마무리

 완성한 건 꽤 되었는데 이제야 정리한다. 


걸과물은 다음과 같다.







* 작성한 코드 *

   

using System.Collections;

using System.Collections.Generic;


using UnityEngine;

using UnityEngine.XR.MagicLeap;


public class test_2_0920 : MonoBehaviour

{


    private LineRenderer lineRenderer;

    public float lineDrawSpeed = 6f;

    public GameObject Line;

    public List<GameObject> ansblock = new List<GameObject>();

    public int numb;

    private int count=0;

    public GameObject Node;

    public GameObject BBlock;

    public GameObject WBlock;

    


    List<GameObject> Node_1 = new List<GameObject>();

    List<GameObject> Node_2 = new List<GameObject>();

    List<GameObject> Node_3 = new List<GameObject>();

    List<GameObject> BlockList = new List<GameObject>();

    List<GameObject> lines_1 = new List<GameObject>();

    List<GameObject> lines_2 = new List<GameObject>();

    List<GameObject> lines_3 = new List<GameObject>();

    List<GameObject> lines_4 = new List<GameObject>();


    private MLInput.Controller _controller;

    private Dictionary<int, List<int>> numData = new Dictionary<int, List<int>>();

    int targetnumb;

    


    // Start is called before the first frame update

    void Start()

    {

        


        MLInput.Start();

        MLInput.OnControllerButtonDown += OnButtonDown;

       

        _controller = MLInput.GetController(MLInput.Hand.Right);


        numData.Add(0, new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

        numData.Add(1, new List<int> { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0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1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

        numData.Add(8, new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

        numData.Add(9, new List<int> { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });


        float ran_x, ran_y, ran_z;


        for(int i=0; i<numb; i++)

        {

            ran_x = Random.Range(-1.1f, -0.9f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_1=Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_1.Add(_obj_1);


            ran_x = Random.Range(-0.1f, 0.1f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_2 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_2.Add(_obj_2);


            ran_x = Random.Range(0.9f, 1.1f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_3 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_3.Add(_obj_3);

        }


        for (int row = 0; row < 10; row++)

        {

            for (int col = 0; col < 10; col++)

            {

                GameObject _obj_3 = Instantiate(WBlock, new Vector3(-2f, 0.4f * row - 2.0f, 0.4f * col + 4.5f), Quaternion.identity);

                BlockList.Add(_obj_3);

            }

        }

        

        Debug.Log("Done");


    }


    // Update is called once per frame

    void Update()

    {

       

            

      

    }


    void OnDestroy()

    {

        MLInput.OnControllerButtonDown -= OnButtonDown;

        MLInput.Stop();

    }


    void OnButtonDown(byte controllereld, MLInput.Controller.Button button)

    {

        if(button == MLInput.Controller.Button.HomeTap)

        {

            for (int i = numb - 1; i >= 0; i--)

            {

                Destroy(Node_1[i]);

                Destroy(Node_2[i]);

                Destroy(Node_3[i]);

            }

            for (int j = 99; j >= 0; j--)

            {

                Destroy(BlockList[j]);

            }

            int len = lines_1.Count;

            for (int k = len - 1; k >= 0; k--)

            {

                Destroy(lines_1[k]);

            }

            len = lines_2.Count;

            for (int k = len - 1; k >= 0; k--)

            {

                Destroy(lines_2[k]);

            }

            len = lines_3.Count;

            for (int k = len - 1; k >= 0; k--)

            {

                Destroy(lines_3[k]);

            }

            len = lines_4.Count;

            for (int k = len - 1; k >= 0; k--)

            {

                Destroy(lines_4[k]);

            }

            Renderer Blockcolor = ansblock[targetnumb].GetComponent<Renderer>();

            Blockcolor.material.color = Color.white;

            Node_1.Clear();

            Node_2.Clear();

            Node_3.Clear();

            BlockList.Clear();

            lines_1.Clear();

            lines_2.Clear();

            lines_3.Clear();

            lines_4.Clear();

            gamecontrol();


            count++;

        }

        

    }


    void drawplane()

    {

        targetnumb = Random.Range(0, 10);

        GameObject _obj_3;



        for (int row = 0; row < 10; row++)

        {

            for (int col = 0; col < 10; col++)

            {

                

                if(numData[targetnumb][99-(10*row+col)]==0)

                {

                    _obj_3 = Instantiate(BBlock, new Vector3(-2f, 0.4f * row - 2.0f, 0.4f * col + 4.5f), Quaternion.identity);

                }

                else

                {

                     _obj_3 = Instantiate(WBlock, new Vector3(-2f, 0.4f * row - 2.0f, 0.4f * col + 4.5f), Quaternion.identity);

                }

                BlockList.Add(_obj_3);

            }

        }


    }


    void gamecontrol()

    {

        float ran_x, ran_y, ran_z;

        GameObject temp;

        int p;


        drawplane();


        for (int i = 0; i < numb; i++)

        {

            ran_x = Random.Range(-1.1f, -0.9f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_1 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_1.Add(_obj_1);


            ran_x = Random.Range(-0.1f, 0.1f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_2 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_2.Add(_obj_2);


            ran_x = Random.Range(0.9f, 1.1f);

            ran_y = Random.Range(-1.5f, 1.5f);

            ran_z = Random.Range(3.8f, 9.2f);


            GameObject _obj_3 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);

            Node_3.Add(_obj_3);

        }


        for(int j=0; j<numb*0.9f;j++)

        {

            p = Random.Range(0, 99);


            temp = drawline(new Vector3(BlockList[p].transform.position.x+0.1f, BlockList[p].transform.position.y, BlockList[p].transform.position.z), Node_1[j].transform.position);

            NodetoWhite(Node_1[j]);

            lines_1.Add(temp);

        }

        for (int j = 0; j < numb * 0.7f; j++)

        {

            temp=drawline(Node_1[j].transform.position, Node_2[j].transform.position);

            NodetoWhite(Node_2[j]);

            lines_2.Add(temp);

        }

        for (int j = 0; j < numb * 0.5f; j++)

        {

            temp = drawline(Node_2[j].transform.position, Node_3[j].transform.position);

            NodetoWhite(Node_3[j]);

            lines_3.Add(temp);

        }

        for (int j = 0; j < 5; j++)

        {

            temp=drawline(Node_3[j].transform.position, ansblock[targetnumb].transform.position);

            lines_4.Add(temp);

        }

        Renderer Blockcolor = ansblock[targetnumb].GetComponent<Renderer>();

        Blockcolor.material.color = Color.cyan;

    }


    GameObject 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);


        return liner;


    }


    void NodetoWhite(GameObject target)

    {

        Renderer Nodecolor = target.GetComponent<Renderer>();

        Nodecolor.material.color = Color.white;

    }



}




* 특이사항 *

1. 컨트롤러의 버튼을 누르면 숫자가 바뀌게 제작했다.

2. 숫자 파일은 파워포인트에서 10*10 표를 색칠해 만든 뒤 일일히 0,1로 바꿔서 만들었다. 너무 비효율적인 방법.

3. 원점 세팅을 어떻게 하는지 아직 잘 모르겠다. Unity 상에 MagicLeap 카메라가 있는데, 그 카메라 각에 맞추어 제작을 했음에도 기기를 착용하고 보면 어디있는지 둘러봐야 찾을 수 있다. 기준점을 어떻게 설정하는지 찾아볼 필요가 있다. 

 

댓글

이 블로그의 인기 게시물

0817 : Hand Tracking

0701_Using server in Unity