//Evan Arthur //CS453 //Spring 2021 using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.IO.Ports; public class cuber : MonoBehaviour { SerialPort sp; GameObject[,] cubes = new GameObject[40,25]; Material[,] materials = new Material[40,25]; Color[] myColors = new Color[5]; int[,] colorIndices = new int[40,25]; float[,] t = new float[40,25]; float[,] lerpTimes = new float[40,25]; int len; int cur_color = 0; //bool switch_color = false; bool mic_on = false; // Start is called before the first frame update void Start() { Debug.Log("HERE!!!!!"); //loops through, creating a 2d array of cubes that each has its own material for(int i = 0;i < 40;i++) { for(int j = 0; j < 25; j++) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(i,j,0); Material mat = new Material(Shader.Find("Sprites/Default")); cube.GetComponent().material = mat; cube.GetComponent().material.EnableKeyword("_EMISSION"); cube.GetComponent().material.color = new Color(0.05f,0.47f,0.85f); cube.GetComponent().material.SetColor("_EmissionColor",cube.GetComponent().material.color); materials[i,j] = mat; cubes[i,j] = cube; //calculate the lerp time based off of how far away the current cube is from the origin float dist = Vector2.Distance(new Vector2(0f,12f),new Vector2(i,j)); dist = scale(0f,48f,0.6f,0.8f,dist); lerpTimes[i,j] = dist; colorIndices[i,j] = 0; } } //the colors that the wall cycles through myColors[0] = new Color(0.05f,0.47f,0.85f); myColors[1] = new Color(0.08f,0.51f,0.85f); myColors[2] = new Color(0.31f,0.75f,0.85f); myColors[3] = new Color(0.37f,0.85f,0.73f); myColors[4] = new Color(0.45f,0.83f,0.89f); len = myColors.Length; string the_com=""; print(SerialPort.GetPortNames().Length); foreach (string mysps in SerialPort.GetPortNames()) { print(mysps); print("hola"); if (mysps != "ttyUSB0") { the_com = mysps; break; } } the_com = "ttyUSB0"; //sp = new SerialPort("\\\\.\\" + the_com, 9600); sp = new SerialPort("/dev/ttyUSB0", 9600); sp.ReadTimeout = 1; } // Update is called once per frame void Update() { if (!sp.IsOpen) { sp.Open(); print("opened sp"); } if (sp.IsOpen) { try { string val = sp.ReadLine(); print(val); if(val.Contains("1")) { print("got a 1"); cur_color = 1; } if(val.Contains("2")) { cur_color = 2; } if(val.Contains("3")) { cur_color = 3; } if(val.Contains("4")){ mic_on = false; } if(val.Contains("5")){ print("turning on mic"); mic_on = true; } } catch (TimeoutException) { } } //loops through the cubes, updating its color and emission for(int i = 0;i < 40; i++){ for(int j = 0;j < 25; j++){ cubes[i,j].GetComponent().material.color = Color.Lerp(cubes[i,j].GetComponent().material.color,myColors[colorIndices[i,j]],lerpTimes[i,j]*Time.deltaTime); cubes[i,j].GetComponent().material.SetColor("_EmissionColor",cubes[i,j].GetComponent().material.color); t[i,j] = Mathf.Lerp(t[i,j],1f,lerpTimes[i,j]*Time.deltaTime); //if it has reached the color, set the target to the next color in the list if(t[i,j] > 0.9f){ t[i,j] = 0f; colorIndices[i,j]++; colorIndices[i,j] = (colorIndices[i,j] >= len) ? 0 : colorIndices[i,j]; } //if space is held, make some of the blocks smaller, else make them go back to normal size if(Input.GetKey(KeyCode.Space) || mic_on){ if(UnityEngine.Random.value < 0.1f && cubes[i,j].transform.localScale[0] > 0.01f){ cubes[i,j].transform.localScale += new Vector3(-0.01f,-0.01f,-0.01f); } } else { if(cubes[i,j].transform.localScale != new Vector3(1f,1f,1f)){ cubes[i,j].transform.localScale += new Vector3(0.005f,0.005f,0.005f); } } } } if ((Input.GetKeyDown(KeyCode.UpArrow) || cur_color == 1)) { //the colors that the wall cycles through myColors[0] = new Color(0.65f,0.015f,0.129f); myColors[1] = new Color(0.949f,0.023f,0.188f); myColors[2] = new Color(0.705f,0.227f,0.313f); myColors[3] = new Color(0.498f,0.011f,0.098f); myColors[4] = new Color(1.0f,0.325f,0.447f); } else if (cur_color == 2){ //the colors that the wall cycles through myColors[0] = new Color(0.05f,0.47f,0.85f); myColors[1] = new Color(0.08f,0.51f,0.85f); myColors[2] = new Color(0.31f,0.75f,0.85f); myColors[3] = new Color(0.37f,0.85f,0.73f); myColors[4] = new Color(0.45f,0.83f,0.89f); } else if (cur_color == 3){ myColors[0] = new Color(0.356f,0.798f,0.537f); myColors[1] = new Color(0.137f,0.666f,0.56f); myColors[2] = new Color(0.247f,0.717f,0.552f); myColors[3] = new Color(0.466f,0.819f,0.513f); myColors[4] = new Color(0.709f,0.909f,0.466f); } } public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue){ //converts a value from one range to a new range float OldRange = (OldMax - OldMin); float NewRange = (NewMax - NewMin); float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin; return(NewValue); } //myColors[0] = new Color(0.86f,0.109f,0.074f); //myColors[1] = new Color(0.917f,0.298f,0.274f); //myColors[2] = new Color(0.941f,0.454f,0.439f); //myColors[3] = new Color(0.945f,0.568f,0.607f); //myColors[4] = new Color(0.964f,0.741f,0.752f); }