public void initializeBuffer() { buffer = gameObject.AddComponent<UnityBuffer>(); buffer.initializeBuffer(); if(buffer!=null && buffer.bufferIsConnected){ buffer.NewEventsAdded += new BufferChangeEventHandler(eventsAdded);//Attach the buffer's event hadnler to the eventsAdded function isBufferOn = true; } }
// Buffer // TODO: This is a horrible combination of service management and buffer/event tracking... should separate into different pieces. private void eventsAdded(UnityBuffer _buffer, EventArgs e) { BufferEvent latestEvent = _buffer.getLatestEvent(); if (latestEvent.getType().toString() == Config.feedbackEventType) { IList alphaLatObjects = latestEvent.getValue().array as IList; currentAlphaLat = alphaLatObjects.Cast <double> ().ToArray(); } }
// Buffer // TODO: This is a horrilble combination of service management and buffer/event tracking... should separate into different pieces. private void eventsAdded(UnityBuffer _buffer, EventArgs e){ BufferEvent latestEvent = _buffer.getLatestEvent(); if(bufferIsOn && latestEvent.getType().toString()==Config.feedbackEventType) { clientIsConnected = true; IList alphaLatObjects = latestEvent.getValue().array as IList; currentAlphaLat = alphaLatObjects.Cast<double>().ToArray(); //Debug.Log (alphaLatObjects); } }
public void initializeBuffer() { buffer = gameObject.AddComponent <UnityBuffer>(); buffer.initializeBuffer(); if (buffer != null && buffer.bufferIsConnected) { buffer.NewEventsAdded += new BufferChangeEventHandler(eventsAdded); //Attach the buffer's event hadnler to the eventsAdded function isBufferOn = true; } }
public void initializeBuffer(){ if( buffer == null ) buffer = gameObject.AddComponent<UnityBuffer>(); buffer.initializeBuffer(); if(buffer!=null && buffer.bufferIsConnected){ //Attach the buffer's event handler to the eventsAdded function buffer.NewEventsAdded += new BufferChangeEventHandler(eventsAdded); bufferIsOn = true; }else{ Debug.Log ("Failed to initialize Unity Buffer Client"); } }
public void initializeBuffer() { if( buffer == null ) buffer = gameObject.AddComponent<UnityBuffer>(); buffer.initializeBuffer(); if(buffer!=null && buffer.bufferIsConnected){ //Attach the buffer's event handler to the eventsAdded function buffer.NewEventsAdded += new BufferChangeEventHandler(eventsAdded); bufferIsOn = true; }else{ Debug.Log ("Failed to initialize Unity Buffer Client"); } }
void PutData(int port, float[,] dataToPut) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected: " + port.ToString()); unityBuffer.putData(dataToPut); } else { print("connection failed..."); } }
float GetBufferDataStatus(int port) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected: " + port.ToString()); unityBuffer.header = unityBuffer.getHeader(); float[,] data = unityBuffer.getFloatData(unityBuffer.header.nSamples - 1, unityBuffer.header.nSamples - 1); return(data[0, 0]); } else { print("connection failed..."); return(0); } }
// Start is called before the first frame update void Start() { cursorMaterial = Resources.Load("unlitColor") as Material; float[,] GetBufferData(int port) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected"); unityBuffer.header = unityBuffer.getHeader(); float[,] data = unityBuffer.getFloatData(0, unityBuffer.header.nSamples - 1); print(data.GetLength(0)); print(data.GetLength(1)); return(data); } else { print("connection failed..."); return(new float[0, 0]); } } foreach (var port in ports) { print(port); cursors.Add(new Cursor(gameObject: GameObject.CreatePrimitive(PrimitiveType.Plane), trajectory: GetBufferData(port))); print(cursors[cursors.Count - 1].trajectory.GetLength(0)); print(cursors[cursors.Count - 1].trajectory.GetLength(1)); cursors[cursors.Count - 1].gameObject.transform.localScale = new Vector3(.1f, .1f, .1f) * 15; cursors[cursors.Count - 1].gameObject.GetComponent <Renderer>().material = cursorMaterial; } }
public bool initializeBuffer() { if (buffer == null) { buffer = gameObject.AddComponent <UnityBuffer>(); } if (!systemIsReady()) { buffer.initializeBuffer(); } if (systemIsReady()) { //Attach the buffer's event handler to the eventsAdded function buffer.NewEventsAdded += new BufferChangeEventHandler(eventsAdded); return(true); } else { Debug.Log("Failed to initialize Unity Buffer Client"); return(false); } }
void Start() { FPS_text = GameObject.Find("Canvas/FPS").GetComponent <TextMeshProUGUI>(); Stopwatch sw = new Stopwatch(); sw.Start(); cursorMaterial = Resources.Load("UnlitColor") as Material; float[,] GetBufferData(int port) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected"); unityBuffer.header = unityBuffer.getHeader(); float[,] data = unityBuffer.getFloatData(0, unityBuffer.header.nSamples - 1); //print(data.GetLength(0)); //print(data.GetLength(1)); return(data); } else { print("connection failed..."); return(new float[0, 0]); } } void printArrayDimensions(float[,] array) { print(array.GetLength(0)); print(array.GetLength(1)); } // get trajectories List <float[, ]> trajectory = new List <float[, ]>(); foreach (var port in ports) { print(port); trajectory.Add(GetBufferData(port)); printArrayDimensions(trajectory[trajectory.Count - 1]); } // get colors float[,] target_positions = GetBufferData(1002); printArrayDimensions(target_positions); // build cursors for (int TRIAL = 0; TRIAL < Number.trials; TRIAL++) { // reshape float[,] trajectory_to_use = new float[Number.frames, Number.axes]; for (int FRAME = 0; FRAME < Number.frames; FRAME++) { for (int AXIS = 0; AXIS < Number.axes; AXIS++) { //print(TRIAL.ToString() + ":" + FRAME.ToString() + ":" + AXIS.ToString()); trajectory_to_use[FRAME, AXIS] = trajectory[AXIS][TRIAL, FRAME]; } } Color colorToUse = colors[(int)target_positions[TRIAL, 0] - 1]; float randomValue = Random.Range(.25f, 1); colorToUse.r = colorToUse.r * randomValue; colorToUse.g = colorToUse.g * randomValue; colorToUse.b = colorToUse.b * randomValue; PlaneObject cursorPlane = PlaneObject.Create(width: Sizes.cursor, length: Sizes.cursor, widthSegments: 1, lengthSegments: 1); cursorPlane.gameObject.GetComponent <Renderer>().material = cursorMaterial; cursorPlane.gameObject.GetComponent <Renderer>().material.color = colorToUse; cursors.Add(new Cursor(gameObject: cursorPlane.gameObject, trajectory: trajectory_to_use)); //cursors[cursors.Count - 1].gameObject.transform.localScale = new Vector3(.1f, .1f, .1f) * 15; //cursors[cursors.Count - 1].gameObject.GetComponent<Renderer>().material = cursorMaterial; //cursors[cursors.Count - 1].gameObject.GetComponent<Renderer>().material.color = colorToUse; } // add target positions for (int TARGET = 0; TARGET < Number.targetPositions; TARGET++) { GameObject target = Create.Polygon(position: new Vector3(targetPositions[TARGET].x, 0, targetPositions[TARGET].y), rotation: new Vector3(-90, 0, 0), radius: Sizes.target, edgeThickness: .1f, nSegments: Defaults.nSegementsCircle, color: Color.white, //colors[TARGET], sortingOrder: 2); //GameObject target = GameObject.CreatePrimitive(PrimitiveType.Plane); //target.transform.localScale = new Vector3(.1f, .1f, .1f) * 15; //target.transform.position = new Vector3(targetPositions[TARGET].x, 0, targetPositions[TARGET].y); //target.GetComponent<Renderer>().material = cursorMaterial; //target.GetComponent<Renderer>().material.color = colors[TARGET]; target.name = TARGET.ToString(); } print(sw.Elapsed.Seconds); }
void Start() { Stopwatch sw = new Stopwatch(); sw.Start(); // timeVector setup for (int FRAME = 0; FRAME < Number.frames; FRAME++) { timeVector[FRAME] = (float)FRAME / 144; } // scene setup FPS_text = GameObject.Find("Canvas/FPS_text").GetComponent <TextMeshProUGUI>(); cursorMaterial = Resources.Load("UnlitColor") as Material; // add target positions void DrawTargets(float xMod, string parentName) { GameObject targets = new GameObject(name: parentName); for (int TARGET = 0; TARGET < Number.targetPositions; TARGET++) { GameObject target = Create.Polygon(position: new Vector3(targetPositions[TARGET].x + xMod, 0, targetPositions[TARGET].y), rotation: new Vector3(-90, 0, 0), radius: Sizes.target, edgeThickness: .1f, nSegments: Defaults.nSegementsCircle, color: Color.white, //colors[TARGET], sortingOrder: 2); target.name = "target." + TARGET.ToString(); target.transform.parent = targets.transform; } } for (int CONTROL = 0; CONTROL < Number.control; CONTROL++) { DrawTargets(xMod[CONTROL], "targets." + Labels.control[CONTROL]); } // session setup float[,] GetBufferData(int port) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected"); unityBuffer.header = unityBuffer.getHeader(); float[,] data = unityBuffer.getFloatData(0, unityBuffer.header.nSamples - 1); return(data); } else { print("connection failed..."); return(new float[0, 0]); } } void printArrayDimensions(float[,] array) { print(array.GetLength(0)); print(array.GetLength(1)); } // reshape method List <Vector2> reshapeTrajectory(List <float[, ]> trajectoryData, int TRIAL, float xMod) { List <Vector2> trajectoryToUse = new List <Vector2>(); for (int FRAME = 0; FRAME < Number.frames; FRAME++) { Vector2 position = new Vector2(); for (int AXIS = 0; AXIS < Number.axes; AXIS++) { //print(TRIAL.ToString() + ":" + FRAME.ToString() + ":" + AXIS.ToString()); if (AXIS == 0) { position[AXIS] = trajectoryData[AXIS][TRIAL, FRAME] + xMod; } else { position[AXIS] = trajectoryData[AXIS][TRIAL, FRAME]; } } trajectoryToUse.Add(position); } return(trajectoryToUse); } void AddCursors(int[] ports, string parentName, CursorType cursorType, float xMod, float[,] target_positions) { GameObject parent = new GameObject(name: parentName); // get trajectories List <float[, ]> trajectory = new List <float[, ]>(); foreach (var port in ports) { print(port); trajectory.Add(GetBufferData(port)); printArrayDimensions(trajectory[trajectory.Count - 1]); } // build cursors for (int TRIAL = 0; TRIAL < Number.trials; TRIAL++) { List <Vector2> cursorTrajectory = reshapeTrajectory(trajectory, TRIAL, xMod); Color colorToUse = colors[(int)target_positions[TRIAL, 0] - 1]; float randomValue = UnityEngine.Random.Range(.25f, 1); colorToUse.r = colorToUse.r * randomValue; colorToUse.g = colorToUse.g * randomValue; colorToUse.b = colorToUse.b * randomValue; GameObject SpawnCursorObject() { switch (cursorType) { case CursorType.cursor: GameObject cursorObject1 = PlaneObject.Create(width: Sizes.cursor, length: Sizes.cursor, widthSegments: 1, lengthSegments: 1).gameObject; cursorObject1.GetComponent <Renderer>().material = cursorMaterial; cursorObject1.GetComponent <Renderer>().material.color = colorToUse; return(cursorObject1); case CursorType.gaze: GameObject cursorObject2 = Create.Polygon(position: Vector3.zero, rotation: new Vector3(-90, 0, 0), radius: Sizes.gaze, edgeThickness: .1f, nSegments: Defaults.nSegementsCircle, color: colorToUse, sortingOrder: 1); return(cursorObject2); default: return(new GameObject()); } } GameObject cursorObject = SpawnCursorObject(); cursorObject.name = "cursor." + TRIAL.ToString(); cursorObject.transform.parent = parent.transform; cursors.Add(new Cursor(gameObject: cursorObject, trajectory: cursorTrajectory)); } } float[,] target_positions_solo = GetBufferData(Ports.target_positions_solo); AddCursors(Ports.P1, "P1", CursorType.cursor, xMod[0], target_positions_solo); AddCursors(Ports.P2, "P2", CursorType.cursor, xMod[0], target_positions_solo); AddCursors(Ports.P1_gaze_solo, "P1_gaze_solo", CursorType.gaze, xMod[0], target_positions_solo); AddCursors(Ports.P2_gaze_solo, "P2_gaze_solo", CursorType.gaze, xMod[0], target_positions_solo); float[,] target_positions_joint = GetBufferData(Ports.target_positions_joint); AddCursors(Ports.joint, "joint", CursorType.cursor, xMod[1], target_positions_joint); AddCursors(Ports.P1_gaze_joint, "P1_gaze_joint", CursorType.gaze, xMod[1], target_positions_joint); AddCursors(Ports.P2_gaze_joint, "P2_gaze_joint", CursorType.gaze, xMod[1], target_positions_joint); print(sw.Elapsed.Seconds); }
private void eventsAdded(UnityBuffer _buffer, EventArgs e) { BufferEvent latestEvent = _buffer.getLatestEvent(); Debug.Log(latestEvent.getType().toString() + ": " + latestEvent.getValue().toString()); }
private void eventsAdded(UnityBuffer _buffer, EventArgs e) { BufferEvent latestEvent = _buffer.getLatestEvent(); Debug.Log (latestEvent.getType().toString()+": "+latestEvent.getValue().toString()); }
void Start() { Stopwatch sw = new Stopwatch(); sw.Start(); // timeVector setup for (int FRAME = 0; FRAME < Number.frames; FRAME++) { timeVector[FRAME] = (float)FRAME / 144; } // scene setup FPS_text = GameObject.Find("Canvas/FPS").GetComponent <TextMeshProUGUI>(); cursorMaterial = Resources.Load("UnlitColor") as Material; timeText = GameObject.Find("Canvas/Time").GetComponent <TextMeshProUGUI>(); sessionText = GameObject.Find("Canvas/Session").GetComponent <TextMeshProUGUI>(); // session setup float[,] GetBufferData(int port) { UnityBuffer unityBuffer = new UnityBuffer(); if (unityBuffer.connect(host: "127.0.0.1", port: port)) { print("connected: " + port.ToString()); unityBuffer.header = unityBuffer.getHeader(); float[,] data = unityBuffer.getFloatData(0, unityBuffer.header.nSamples - 1); return(data); } else { print("connection failed..."); return(new float[0, 0]); } } void printArrayDimensions(float[,] array) { print(array.GetLength(0)); print(array.GetLength(1)); } // reshape method List <Vector2> reshapeTrajectory(List <float[, ]> trajectoryData, int TRIAL, float xMod, float yMod) { List <Vector2> trajectoryToUse = new List <Vector2>(); for (int FRAME = 0; FRAME < Number.frames; FRAME++) { Vector2 position = new Vector2(); for (int AXIS = 0; AXIS < Number.axes; AXIS++) { //print(TRIAL.ToString() + ":" + FRAME.ToString() + ":" + AXIS.ToString()); if (AXIS == 0) { position[AXIS] = trajectoryData[AXIS][TRIAL, FRAME] + xMod; } else { position[AXIS] = trajectoryData[AXIS][TRIAL, FRAME] + yMod; } } trajectoryToUse.Add(position); } return(trajectoryToUse); } void AddCursors(int[] ports, string parentName, CursorType cursorType, float xMod, float yMod, float[,] target_positions, GameObject grandParent) { GameObject parent = new GameObject(name: parentName); parent.transform.parent = grandParent.transform; // get trajectories List <float[, ]> trajectory = new List <float[, ]>(); foreach (var port in ports) { //print(port); trajectory.Add(GetBufferData(port)); //printArrayDimensions(trajectory[trajectory.Count - 1]); } // build cursors for (int TRIAL = 0; TRIAL < Number.trials; TRIAL++) { List <Vector2> cursorTrajectory = reshapeTrajectory(trajectory, TRIAL, xMod, yMod); Color colorToUse = colors[(int)target_positions[TRIAL, 0] - 1]; float randomValue = UnityEngine.Random.Range(.25f, 1); colorToUse.r = colorToUse.r * randomValue; colorToUse.g = colorToUse.g * randomValue; colorToUse.b = colorToUse.b * randomValue; GameObject SpawnCursorObject() { switch (cursorType) { case CursorType.cursor: GameObject cursorObject1 = PlaneObject.Create(width: Sizes.cursor, length: Sizes.cursor, widthSegments: 1, lengthSegments: 1).gameObject; cursorObject1.GetComponent <Renderer>().material = cursorMaterial; cursorObject1.GetComponent <Renderer>().material.color = colorToUse; return(cursorObject1); case CursorType.gaze: GameObject cursorObject2 = Create.Polygon(position: Vector3.zero, rotation: new Vector3(-90, 0, 0), radius: Sizes.gaze, edgeThickness: .1f, nSegments: Defaults.nSegementsCircle, color: colorToUse, sortingOrder: 1); return(cursorObject2); default: return(new GameObject()); } } GameObject cursorObject = SpawnCursorObject(); cursorObject.name = "cursor." + TRIAL.ToString(); cursorObject.transform.parent = parent.transform; cursors.Add(new Cursor(gameObject: cursorObject, trajectory: cursorTrajectory)); } } // add target positions void DrawTargets(float xMod, float yMod, string parentName, GameObject grandParent) { GameObject targets = new GameObject(name: parentName); targets.transform.parent = grandParent.transform; for (int TARGET = 0; TARGET < Number.targetPositions; TARGET++) { GameObject target = Create.Polygon(position: new Vector3(targetPositions[TARGET].x + xMod, 0, targetPositions[TARGET].y + yMod), rotation: new Vector3(-90, 0, 0), radius: Sizes.target, edgeThickness: .1f, nSegments: Defaults.nSegementsCircle, color: Color.white, //colors[TARGET], sortingOrder: 2); target.name = "target." + TARGET.ToString(); target.transform.parent = targets.transform; } } //int[] sessionsToUse = new int[] { 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; int[] sessionsToUse = new int[] { 12, 13, 14, 15, 16, 17 }; int sessionCount = 0; void prepareSession(int SESSION) { print("***************************************************************"); print("SESSION: " + SESSION.ToString()); sessionText.text = "S" + Labels.session[SESSION]; GameObject grandParentObject = new GameObject(name: Labels.session[SESSION]); float yModToUse = 0; for (int CONTROL = 0; CONTROL < Number.control; CONTROL++) { DrawTargets(xMod[CONTROL], yModToUse, "targets." + Labels.control[CONTROL], grandParentObject); } float[,] target_positions_solo = GetBufferData(Ports.target_positions_solo); AddCursors(Ports.P1, "P1", CursorType.cursor, xMod[0], yModToUse, target_positions_solo, grandParentObject); AddCursors(Ports.P2, "P2", CursorType.cursor, xMod[0], yModToUse, target_positions_solo, grandParentObject); AddCursors(Ports.P1_gaze_solo, "P1_gaze_solo", CursorType.gaze, xMod[0], yModToUse, target_positions_solo, grandParentObject); AddCursors(Ports.P2_gaze_solo, "P2_gaze_solo", CursorType.gaze, xMod[0], yModToUse, target_positions_solo, grandParentObject); float[,] target_positions_joint = GetBufferData(Ports.target_positions_joint); AddCursors(Ports.joint, "joint", CursorType.cursor, xMod[1], yModToUse, target_positions_joint, grandParentObject); AddCursors(Ports.P1_gaze_joint, "P1_gaze_joint", CursorType.gaze, xMod[1], yModToUse, target_positions_joint, grandParentObject); AddCursors(Ports.P2_gaze_joint, "P2_gaze_joint", CursorType.gaze, xMod[1], yModToUse, target_positions_joint, grandParentObject); } prepareSession(1); void StartOBSrecording() { var proc = new System.Diagnostics.Process { StartInfo = new System.Diagnostics.ProcessStartInfo // get process by window name - super dodge - also cannot run Pathly from unity { FileName = @"C:\Program Files (x86)\obs-studio\OBSCommand_v1.5.4\OBSCommand\OBSCommand.exe", Arguments = "/startrecording", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.Start(); } StartOBSrecording(); print(sw.Elapsed.Seconds); }
// Buffer // TODO: This is a horrible combination of service management and buffer/event tracking... should separate into different pieces. private void eventsAdded(UnityBuffer _buffer, EventArgs e) { BufferEvent latestEvent = _buffer.getLatestEvent(); if(bufferIsOn && latestEvent.getType().toString()==Config.feedbackEventType) { IList alphaLatObjects = latestEvent.getValue().array as IList; currentAlphaLat = alphaLatObjects.Cast<double>().ToArray(); } }