01/04 : SNN VIsualization(4)
Working hour : 9-13
Works to do
1. convert to magic leap from unity
2. make powerpoint slides introducing Neural networks and our model
*Completed Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
public class _210103 : MonoBehaviour
{
//1: using line renderer
private LineRenderer lineRenderer;
public float lineDrawSpeed = 6f;
public GameObject Line;
List<List<GameObject>> lines_0 = new List<List<GameObject>>();
List<List<GameObject>> lines_1 = new List<List<GameObject>>();
List<List<GameObject>> lines_2 = new List<List<GameObject>>();
List<List<GameObject>> lines_3 = new List<List<GameObject>>();
//2: node & blocks & answerblocks
public int numb;
public List<GameObject> ansblock = new List<GameObject>();
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>();
//3: using controller
private MLInput.Controller _controller;
//4: others
private Dictionary<int, List<int>> numData = new Dictionary<int, List<int>>();
List<List<int>> nodeinfo_12 = new List<List<int>>();
List<List<int>> nodeinfo_23 = new List<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, 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 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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 });
// Initial blocks & nodes construction
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 10; col++)
{
GameObject _obj_3 = Instantiate(BBlock, new Vector3(-1.7f, 0.3f * row - 1.5f, 0.3f * col + 5f), Quaternion.identity);
BlockList.Add(_obj_3);
}
}
float ran_x, ran_y, ran_z;
for (int i = 0; i < numb; i++)
{
ran_x = Random.Range(-1.4f, -0.6f);
ran_y = Random.Range(-1.0f, 1.0f);
ran_z = Random.Range(5.5f, 7.5f);
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.4f, 0.4f);
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.6f, 1.4f);
GameObject _obj_3 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);
Node_3.Add(_obj_3);
}
}
// 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)
{
StartCoroutine("timecont");
}
}
IEnumerator timecont()
{
for(float q=20f; q>=0; q-=1f)
{
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_0.Count;
for (int k = len - 1; k >= 0; k--)
{
for (int a = lines_0[k].Count - 1; a >= 0; a--)
Destroy(lines_0[k][a]);
lines_0[k].Clear();
}
len = lines_1.Count;
for (int k = len - 1; k >= 0; k--)
{
for (int a = lines_1[k].Count - 1; a >= 0; a--)
Destroy(lines_1[k][a]);
lines_1[k].Clear();
}
len = lines_2.Count;
for (int k = len - 1; k >= 0; k--)
{
for (int a = lines_2[k].Count - 1; a >= 0; a--)
Destroy(lines_2[k][a]);
lines_2[k].Clear();
}
len = lines_3.Count;
for (int k = len - 1; k >= 0; k--)
{
for (int a = lines_3[k].Count - 1; a >= 0; a--)
Destroy(lines_3[k][a]);
lines_3[k].Clear();
}
Renderer Blockcolor = ansblock[targetnumb].GetComponent<Renderer>();
Blockcolor.material.color = Color.white;
Node_1.Clear();
Node_2.Clear();
Node_3.Clear();
BlockList.Clear();
lines_0.Clear();
lines_1.Clear();
lines_2.Clear();
lines_3.Clear();
gamecontrol();
Debug.Log("gamecontrol 실행 횟수 : " + q+ "t = " + Time.time);
yield return new WaitForSeconds(1.5f);
}
}
void gamecontrol()
{
targetnumb = Random.Range(0, 10);
// create new numb plane
drawplane();
// create new nodes
float ran_x, ran_y, ran_z;
for (int i = 0; i < numb; i++)
{
ran_x = Random.Range(-1.4f, -0.6f);
ran_y = Random.Range(-1.0f, 1.0f);
ran_z = Random.Range(5.5f, 7.5f);
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.4f, 0.4f);
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.6f, 1.4f);
GameObject _obj_3 = Instantiate(Node, new Vector3(ran_x, ran_y, ran_z), Quaternion.identity);
Node_3.Add(_obj_3);
}
select_node();
StartCoroutine("timecontrol");
//change_node();
}
//시냅스 연결과정
void select_node()
{
GameObject temp;
//Number plane ~ 1st node
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < numb; j++)
{
lines_0.Add(new List<GameObject>());
if (numData[targetnumb][i] == 1)
{
if ((Vector3.Distance(new Vector3(BlockList[i].transform.position.x, BlockList[i].transform.position.y, BlockList[i].transform.position.z), new Vector3(BlockList[i].transform.position.x, Node_1[j].transform.position.y, Node_1[j].transform.position.z)) < .5f) && (BlockList[i].transform.position.x - Node_1[j].transform.position.x) < .8f)
{
changeOBcolor(Node_1[j], 0);
temp = drawline(BlockList[i].transform.position, Node_1[j].transform.position, 1);
lines_0[j].Add(temp);
}
}
}
}
//1st node ~ 2nd node
int nodenumrand, nodenewrand;
for (int i = 0; i < numb; i++)
{
nodeinfo_12.Add(new List<int>());
lines_1.Add(new List<GameObject>());
nodenumrand = Random.Range(0, 4);
for (int j = 0; j < nodenumrand; j++)
{
nodenewrand = Random.Range(0, numb);
nodeinfo_12[i].Add(nodenewrand);
temp = drawline(Node_1[i].transform.position, Node_2[nodenewrand].transform.position, 0);
lines_1[i].Add(temp);
}
}
//2nd node ~ 3rd node
for (int i = 0; i < numb; i++)
{
nodeinfo_23.Add(new List<int>());
lines_2.Add(new List<GameObject>());
nodenumrand = Random.Range(0, 4);
for (int j = 0; j < nodenumrand; j++)
{
nodenewrand = Random.Range(0, numb);
nodeinfo_23[i].Add(nodenewrand);
temp = drawline(Node_2[i].transform.position, Node_3[nodenewrand].transform.position, 0);
lines_2[i].Add(temp);
}
}
}
IEnumerator timecontrol()
{
Debug.Log("timecontrol 들어옴(1) t = " + Time.time);
Renderer Nodecolor;
GameObject temp;
int sec_1, sec_2;
sec_1 = Random.Range(0, 3);
sec_2 = Random.Range(0, 3);
yield return new WaitForSeconds(0.03f);
Debug.Log("timecontrol 들어옴(2) t = " + Time.time);
for (int i = 0; i < numb; i++)
{
Nodecolor = Node_1[i].GetComponent<Renderer>();
if (Nodecolor.material.color == Color.white)
{
for (int k = 0; k < lines_0[i].Count; k++)
{
if(possibility_calc(30) == 1)
{
LineRenderer linecolor = lines_0[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(1, 1, 1, 100 / 255f);
}
else
{
LineRenderer linecolor = lines_0[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(50 / 255f, 63 / 255f, 70 / 255f, 255 / 255f);
}
}
}
}
yield return new WaitForSeconds(0.03f);
Debug.Log("timecontrol 들어옴(2) t = " + Time.time);
for (int i = 0; i < numb; i++)
{
Nodecolor = Node_1[i].GetComponent<Renderer>();
if (Nodecolor.material.color == Color.white)
{
for (int k = 0; k < lines_0[i].Count; k++)
{
if (possibility_calc(30) == 1)
{
LineRenderer linecolor = lines_0[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(1, 1, 1, 255 / 255f);
}
else
{
LineRenderer linecolor = lines_0[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(50 / 255f, 63 / 255f, 70 / 255f, 255 / 255f);
}
}
}
}
yield return new WaitForSeconds(0.01f);
Debug.Log("timecontrol 들어옴(2) t = " + Time.time);
for (int i = 0; i < numb; i++)
{
Nodecolor = Node_1[i].GetComponent<Renderer>();
if (Nodecolor.material.color == Color.white)
{
for(int k=0; k<lines_1[i].Count; k++)
{
if (possibility_calc(5) == 1)
{
LineRenderer linecolor = lines_1[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(1, 1, 1, 100 / 255f);
}
else
{
LineRenderer linecolor = lines_1[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(50 / 255f, 63 / 255f, 70 / 255f, 255 / 255f);
}
}
for (int k = 0; k < nodeinfo_12[i].Count; k++)
{
if (((3.8 + 2.7 * sec_1 < Node_2[nodeinfo_12[i][k]].transform.position.z) && (3.8 + 2.7 * (sec_1 + 1) > Node_2[nodeinfo_12[i][k]].transform.position.z)) && (((-1.5 + 1.5 * sec_2 < Node_2[nodeinfo_12[i][k]].transform.position.y) && (-1.5 + 1.5 * (sec_2 + 1) > Node_2[nodeinfo_12[i][k]].transform.position.y))))
{
changeOBcolor(Node_2[nodeinfo_12[i][k]], 0);
for (int a = lines_1[i].Count - 1; a >= 0; a--)
Destroy(lines_1[i][a]);
temp = drawline(Node_1[i].transform.position, Node_2[nodeinfo_12[i][k]].transform.position, 1);
lines_1[i].Add(temp);
}
}
}
}
yield return new WaitForSeconds(0.03f);
Debug.Log("timecontrol 들어옴(3) t = " + Time.time);
sec_1 = Random.Range(0, 3);
sec_2 = Random.Range(0, 3);
for (int i = 0; i < numb; i++)
{
Nodecolor = Node_2[i].GetComponent<Renderer>();
for (int k = 0; k < lines_2[i].Count; k++)
{
if (possibility_calc(5) == 1)
{
LineRenderer linecolor = lines_2[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(1, 1, 1, 100 / 255f);
}
else
{
LineRenderer linecolor = lines_2[i][k].GetComponent<LineRenderer>();
linecolor.material.color = new Color(50 / 255f, 63 / 255f, 70 / 255f, 255 / 255f);
}
}
if (Nodecolor.material.color == Color.white && possibility_calc(60) == 1)
{
for (int k = 0; k < nodeinfo_23[i].Count; k++)
{
if (((3.8 + 1.8 * sec_1 < Node_3[nodeinfo_23[i][k]].transform.position.z) && (3.8 + 1.8 * (sec_1 + 1) > Node_3[nodeinfo_23[i][k]].transform.position.z)) && (((-1.5 + 1.0 * sec_2 < Node_3[nodeinfo_23[i][k]].transform.position.y) && (-1.5 + 1.0 * (sec_2 + 1) > Node_3[nodeinfo_23[i][k]].transform.position.y))))
{
changeOBcolor(Node_3[nodeinfo_23[i][k]], 0);
for (int a = lines_2[i].Count - 1; a >= 0; a--)
Destroy(lines_2[i][a]);
temp = drawline(Node_2[i].transform.position, Node_3[nodeinfo_23[i][k]].transform.position, 1);
lines_2[i].Add(temp);
}
}
}
}
yield return new WaitForSeconds(0.03f);
Debug.Log("timecontrol 들어옴(4) t = " + Time.time);
for (int i=0; i<numb; i++)
{
lines_3.Add(new List<GameObject>());
Nodecolor = Node_3[i].GetComponent<Renderer>();
if(Nodecolor.material.color == Color.white && possibility_calc(40)==1)
{
temp = drawline(Node_3[i].transform.position, ansblock[targetnumb].transform.position, 1);
lines_3[i].Add(temp);
}
}
Renderer Blockcolor = ansblock[targetnumb].GetComponent<Renderer>();
Blockcolor.material.color = Color.cyan;
}
// 연결된 시냅스에서 신호의 전파
void change_node()
{
Renderer Nodecolor;
for (int i = 0; i < numb; i++)
{
Nodecolor = Node_1[i].GetComponent<Renderer>();
if (Nodecolor.material.color == Color.white)
{
for (int j = 0; j < numb; j++)
{
for (int k = 0; k < nodeinfo_12[j].Count; k++)
{
if (Vector3.Distance(new Vector3(Node_1[i].transform.position.x, Node_1[i].transform.position.y, Node_1[i].transform.position.z), new Vector3(Node_1[i].transform.position.x + .2f, Node_2[nodeinfo_12[j][k]].transform.position.y, Node_2[nodeinfo_12[j][k]].transform.position.z)) < 1f)
{
changeOBcolor(Node_2[nodeinfo_12[j][k]], 0);
for (int a = 0; a < lines_1[i].Count; a++)
{
changeLINEcolor(lines_1[i][a], 0);
}
}
}
}
}
}
}
void changeOBcolor(GameObject target, int colorcode)
{
Renderer Nodecolor = target.GetComponent<Renderer>();
//White
if (colorcode == 0)
{
Nodecolor.material.color = Color.white;
}
}
void changeLINEcolor(GameObject target, int colorcode)
{
Debug.Log("changeLINEcolor 실행");
LineRenderer linecolor = target.GetComponent<LineRenderer>();
//White
if (colorcode == 1)
{
linecolor.startColor = Color.cyan;
linecolor.endColor = Color.cyan;
}
}
void NodetoWhite(GameObject target)
{
Renderer Nodecolor = target.GetComponent<Renderer>();
Nodecolor.material.color = Color.white;
}
GameObject drawline(Vector3 start, Vector3 end, int colorcode)
{
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.material = new Material(Shader.Find("Sprites/Default"));
if (colorcode == 0)
{
lineRenderer.material.color = new Color(50 / 255f, 63 / 255f, 70 / 255f, 255 / 255f);
/*
lineRenderer.startColor = Color.black;
lineRenderer.endColor = Color.black ;
*/
}
else if (colorcode == 1)
{
lineRenderer.material.color = Color.white;
/*
lineRenderer.startColor = Color.cyan;
lineRenderer.endColor = Color.cyan;
*/
}
lineRenderer.SetPosition(0, start);
lineRenderer.SetWidth(.001f, .001f);
lineRenderer.SetPosition(1, end);
return liner;
}
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(-1.7f, 0.3f * row - 1.5f, 0.3f * col + 5f), Quaternion.identity);
}
else
{
_obj_3 = Instantiate(WBlock, new Vector3(-1.7f, 0.3f * row - 1.5f, 0.3f * col + 5f), Quaternion.identity);
}
BlockList.Add(_obj_3);
}
}
}
int possibility_calc(int input)
{
int i;
i = Random.Range(0, 100);
if (i < input)
return 1;
else
return 0;
}
}
댓글
댓글 쓰기