Skip to content

Instantly share code, notes, and snippets.

@tanyuan
Created January 19, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanyuan/fb2262e9ff3875a18a1710d2b489c7b4 to your computer and use it in GitHub Desktop.
Save tanyuan/fb2262e9ff3875a18a1710d2b489c7b4 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class RecordROI : MonoBehaviour {
public GameObject highlight;
public string path;
private Vector3 mousePos;
private Vector3 spherePos;
// Use this for initialization
void Start () {
// Hide the highlight
highlight.transform.position = Vector3.back;
}
// Update is called once per frame
void Update () {
// 左鍵
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
// Move the highlight to the cursor position
highlight.transform.position = hit.point;
// Get time (seconds) from game start
float time = Time.time;
// Get longitude
spherePos.x = Remap (hit.point.x, 10, -10, 180, -180);
// Get latitude
spherePos.y = Remap (hit.point.y, -5, 5, -90, 90);
spherePos.z = 0;
Debug.Log ("Set ROI: "+ spherePos.x + ", " + spherePos.y);
// Write to file
System.IO.File.AppendAllText(path, System.String.Format("{0} {1} {2}\n", time, spherePos.x, spherePos.y));
}
}
// 右鍵
if (Input.GetMouseButtonDown (1)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
// Move the highlight to the cursor position
highlight.transform.position = hit.point;
// Get time (seconds) from game start
float time = Time.time;
// Get longitude
spherePos.x = Remap (hit.point.x, 10, -10, 180, -180);
// Get latitude
spherePos.y = Remap (hit.point.y, -5, 5, -90, 90);
spherePos.z = 0;
Debug.Log ("Set ROI: "+ spherePos.x + ", " + spherePos.y);
// Write to file
System.IO.File.AppendAllText(path, System.String.Format("{0} {1} {2}\n", -1.0f*time, spherePos.x, spherePos.y));
}
}
}
float Remap (float value, float from1, float from2, float to1, float to2) {
return (value - from1) / (from2 - from1) * (to2 - to1) + to1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment