-
Notifications
You must be signed in to change notification settings - Fork 9
/
Plane.cs
126 lines (106 loc) · 3.77 KB
/
Plane.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using UnityEngine;
using System.Collections;
using com.youvisio;
using System.Threading;
public class Plane : MonoBehaviour {
public static string InfoStr;
private GameObject _camera;
private GameObject _sphere;
private BackgroundWorker _backgroundWorker;
// Use this for initialization
void Start () {
_camera = UnityEngine.GameObject.Find("Main Camera");
_sphere = UnityEngine.GameObject.Find("Sphere");
var text = UnityEngine.GameObject.Find("Text");
var bc = text.AddComponent<BoxCollider>();
bc.center = new Vector3(0, 0, 0);
bc.size = new Vector3(7, 1, 1);
bc.transform.parent = text.transform;
}
// Update is called once per frame
void Update () {
HandleTouch();
_sphere.transform.position =
RotateAbout(
_sphere.transform.position,
new Vector3(0, 1, 0), // rotate about this point
new Vector3(0, 1, 0), // on a plane with this normal
30 * Time.smoothDeltaTime);// 30 degrees per second
if (_backgroundWorker != null) _backgroundWorker.Update();
}
private void OnTouched()
{
if (_backgroundWorker != null) _backgroundWorker.CancelAsync();
_backgroundWorker = new BackgroundWorker();
if (string.IsNullOrEmpty(Thread.CurrentThread.Name)) Thread.CurrentThread.Name = "Main";
Debug.Log("START " + Thread.CurrentThread.Name);
Plane.InfoStr += "\nS("+Thread.CurrentThread.Name+");";
_backgroundWorker.DoWork += (o, a) =>
{
Debug.Log("INSIDE1 " + Thread.CurrentThread.Name);
Plane.InfoStr += "IN1("+Thread.CurrentThread.Name+");"+a.Argument+";";
for (var i = 0; i < 10000000; ++i)
{
if (a.IsCanceled) return;
var n = 67876 + i / 100f;
var x = Mathf.Pow(n, 3);
}
Debug.Log("INSIDE2 " + Thread.CurrentThread.Name);
Plane.InfoStr += "IN2("+Thread.CurrentThread.Name+");";
a.Result = a.Argument+"!";
};
_backgroundWorker.RunWorkerCompleted += (o, a) =>
{
Debug.Log("END " + Thread.CurrentThread.Name);
Plane.InfoStr += "E("+Thread.CurrentThread.Name+");"+a.Result+";";
};
_backgroundWorker.RunWorkerAsync("A1");
}
#region utils
private bool _touching;
private void HandleTouch()
{
var touches = Input.touches;
var len = touches.Length;
Vector2 pos2d = new Vector2(0, 0);
if (len == 0)
{
if (Input.GetMouseButton(0))
{
pos2d = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
len = 1;
}
}
else
{
pos2d = new Vector2(touches[0].position.x, Screen.height - touches[0].position.y);
}
if (len > 0)
{
if (!_touching)
{
var ray = _camera.camera.ViewportPointToRay(new Vector3(pos2d.x / Screen.width, 1f - pos2d.y / Screen.height, 1f));
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
var hitName = hit.transform.name;
if (hitName == "Text")
{
_touching = true;
OnTouched();
}
}
}
return;
}
if (_touching)
{
_touching = false;
}
}
public static Vector3 RotateAbout(Vector3 point, Vector3 pivot, Vector3 axis, float degrees)
{
return (Quaternion.AngleAxis(degrees, axis) * (point - pivot)) + pivot;
}
#endregion
}