Subversion Repository Public Repository

Nextrek

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using UnityEngine;
using System.Collections;

public class DrawRect : MonoBehaviour 
{
	public Transform start;
	public Transform end;
	public Color rectColor;
	public Color textColor;

	private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控

	private float shapeSize = 20.0f;
	private float shapeBorder = 3.0f;

	private Transform _spaceShip;
	private Transform _transform;

	void Start()
	{
		rectMat = new Material("Shader \"Lines/Colored Blended\" {" +
		                       "SubShader { Pass { " +
		                       "    Blend SrcAlpha OneMinusSrcAlpha " +
		                       "    ZWrite Off Cull Off Fog { Mode Off } " +
		                       "    BindChannels {" +
		                       "      Bind \"vertex\", vertex Bind \"color\", color }" +
		                       "} } }");//生成画线的材质
		rectMat.hideFlags = HideFlags.HideAndDontSave;
		rectMat.shader.hideFlags = HideFlags.HideAndDontSave;

		_spaceShip = GameManager.Instance.spaceShip;
		_transform = transform;
	}

	void OnGUI()
	{
		rectMat.SetPass(0);

		DrawRadarPoint(_transform, rectColor);

		if(start==null|end==null)
		{
			return;
		}

		if(Camera.main)
		{
			Vector3 cen = Camera.main.WorldToScreenPoint((start.position+end.position)/2);

			if (cen.z>0) {
				//Debug.Log(a+" "+b);
				Draw(cen);
			}
		}


	}

	void DrawRadarPoint(Transform ufo, Color col)
	{
		Vector3 relative = _spaceShip.transform.InverseTransformPoint(ufo.position);
		
		float distance = Mathf.Sqrt( relative.magnitude / 3.0f );
		
		if (distance > 45.0f) {
			return;
		}
		
		float angle = Mathf.Atan2(relative.x, relative.z) - 90.0f * Mathf.Deg2Rad;
		
		Vector2 D = new Vector2( Mathf.Cos(angle), Mathf.Sin(angle) );
		
		D *= distance;
		
		D.x += Screen.width-150;
		D.y += Screen.height-50;
		
		Vector2 dir = new Vector2(0.0f,1.5f);
		Vector2 right = new Vector2(1.5f,0.0f);
		Vector2 A = D + dir * 2.0f;
		Vector2 B = D + right;
		Vector2 C = D - right;

		GL.Color(col);

		GL.Begin(GL.TRIANGLES);
		GL.Vertex3(A.x, A.y, 0);	
		GL.Vertex3(C.x, C.y, 0);	
		GL.Vertex3(B.x, B.y, 0);	
		GL.End();
	}
	
	//渲染2D框
	void Draw(Vector3 cen)
	{	
		
		
		GL.PushMatrix();//保存摄像机变换矩阵
		try {
			GL.LoadPixelMatrix();//设置用屏幕坐标绘图

			// check if OutOfScreen
			if ((cen.x < 0) || (cen.x > Screen.width) || (cen.y < 0) || (cen.y > Screen.height)) {
				Vector2 screenCenter = new Vector2(Screen.width/2, Screen.height/2);
				Vector2 dir = new Vector2(cen.x-screenCenter.x, cen.y-screenCenter.y);
				dir.Normalize();
				cen = polarProject(screenCenter, dir, shapeSize);
				DrawArrow(cen, dir, shapeSize, rectColor, shapeBorder);
			} else {
				DrawFrame(cen, shapeSize, rectColor, shapeBorder);
			}
		} finally {
			GL.PopMatrix();//还原
		}

		if ((cen.y-shapeSize)<=0) {
			DrawDistance(cen.x, cen.y+shapeSize, textColor);
		} else {
			DrawDistance(cen.x, cen.y-shapeSize, textColor);
		}
	}

	Vector2 polarProject(Vector2 screenCenter, Vector2 dir, float size)
	{
		float radius = (Screen.height+Screen.width)*0.5f;

		Vector2 A = screenCenter + dir*(radius*0.5f);
		if (A.x<0)
		{	
			A.x = 0;
		} 
		else 
		{
			if (A.x>Screen.width)
			{
				A.x = Screen.width;
			}
		}
		if (A.y<0)
		{	
			A.y = 0;
		} 
		else 
		{
			if (A.y>Screen.height)
			{
				A.y = Screen.height;
			}
		}
		
		Vector2 D = A - dir*size;
		return D;
	}

	void DrawArrow(Vector2 D, Vector2 dir, float size, Color col, float border)
	{
		/* 
		 *        + A
 		 *      /  \
 		 *    /     \
 		 *  +----+---+
 		 * C     D    B
 		 */
		
		Vector2 right = new Vector2(-dir.y, dir.x);
		right *= size*0.5f;

		Vector2 A = D + dir * size;
		Vector2 B = D + right;
		Vector2 C = D - right;

		GL.Color(col);
		
		GL.Begin(GL.TRIANGLE_STRIP);
			GL.Vertex3(A.x, A.y, 0);	
			GL.Vertex3(C.x, C.y, 0);	
			GL.Vertex3(B.x, B.y, 0);	
		GL.End();
	}

	void DrawFrame(Vector3 cen, float size, Color col, float border)
	{
		/*
		 * +-----------------+
		 * | \             / |
		 * |  +-----------+  |
		 * |  |           |  |
		 * |  |           |  |
		 * |  |           |  |
		 * |  +-----------+  |
		 * | /             \ |
		 * +-----------------+
		 */
		GL.Color(col);

		GL.Begin(GL.TRIANGLE_STRIP);
			GL.Vertex3(cen.x-size+border, cen.y-size+border, 0);
			GL.Vertex3(cen.x-size, cen.y-size, 0);

			GL.Vertex3(cen.x-size+border, cen.y+size-border, 0);
			GL.Vertex3(cen.x-size, cen.y+size, 0);

			GL.Vertex3(cen.x+size-border, cen.y+size-border, 0);
			GL.Vertex3(cen.x+size, cen.y+size, 0);

			GL.Vertex3(cen.x+size-border, cen.y-size+border, 0);
			GL.Vertex3(cen.x+size, cen.y-size, 0);

			GL.Vertex3(cen.x-size+border, cen.y-size+border, 0);
			GL.Vertex3(cen.x-size, cen.y-size, 0);
		GL.End ();
	}
	
	void DrawDistance(float x, float y,Color col)
	{
		if(_spaceShip==null) return;

		float tmp = Vector3.Distance(_transform.position,_spaceShip.position);
		int dis = (int)tmp;
		string str = dis+"km";
		GUI.skin.label.normal.textColor=col;
		Vector2 sz = GUI.skin.label.CalcSize(new GUIContent(str));

		GUI.Label(new Rect(x-sz.x/2,Screen.height-y,sz.x,sz.y),str);
	}
	/*
	void DrawDistance(Vector3 start,Vector3 end,Color col)
	{
		if(spaceShip==null)return;
		start.y=Screen.height-start.y;
		end.y=Screen.height-end.y;
		float tmp = Vector3.Distance(_transform.position,spaceShip.position);
		int dis = (int)tmp;
		string str = dis+"km";
		GUI.skin.label.normal.textColor=col;
		Vector2 sz = GUI.skin.label.CalcSize(new GUIContent(str));
		float x = (start.x+end.x)/2-sz.x/2;
		float y = end.y-sz.y;
		if(y<=0)y=start.y+sz.y;
		GUI.Label(new Rect(x,y,sz.x,sz.y),str);
	}*/
}

Commits for Nextrek/3DSpace/Assets/Custum/Scripts/DrawRect.cs

Diff revisions: vs.
Revision Author Commited Message
117 Diff Diff DRuega picture DRuega Tue 28 Oct, 2014 13:39:51 +0000
116 Diff Diff DRuega picture DRuega Mon 20 Oct, 2014 20:14:25 +0000
112 Diff Diff FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 14:21:59 +0000
107 FMMortaroli picture FMMortaroli Thu 09 Oct, 2014 11:56:46 +0000