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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
using UnityEngine; using System.Collections; public class DrawRect : MonoBehaviour { public Color rectColor; public Color textColor; private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控 private float shapeSize = 10.0f; private float shapeBorder = 3.0f; 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; _transform = transform; } void OnRenderObject() { rectMat.SetPass(0); DrawRadarPoint(_transform, rectColor); } void DrawRadarPoint(Transform ufo, Color col) { Transform _spaceShip = GameManager.GetHero(); if (_spaceShip == null) { return; } Vector3 relative = _spaceShip.transform.InverseTransformPoint(ufo.position); float distance = Mathf.Sqrt( relative.magnitude ) / 3.0f; if (distance > 43.0f) { return; } float angle = -Mathf.Atan2(relative.x, relative.z) + 90.0f * Mathf.Deg2Rad; GameObject radarGui = GameObject.Find("SmallPlayerShip"); Vector3 cen = radarGui.transform.position; float localScale = 1.0f / 200.0f; Vector3 up = radarGui.transform.forward * localScale; Vector3 right = radarGui.transform.right * localScale; Vector3 D = cen + (right * (Mathf.Cos(angle) * distance)) + (up * (Mathf.Sin(angle) * distance)) + radarGui.transform.up*0.01f; Vector3 A = D + up; Vector3 B = D - up + right; Vector3 C = D - up - right; GL.Color(col); GL.Begin(GL.TRIANGLES); GL.Vertex3(A.x, A.y, A.z); GL.Vertex3(C.x, C.y, C.z); GL.Vertex3(B.x, B.y, B.z); GL.End(); } void OnGUI() { if(Camera.main) { Vector3 cen = Camera.main.WorldToScreenPoint(this.transform.position); if (cen.z>0) { //Debug.Log(a+" "+b); Draw(cen); } } } //渲染2D框 void Draw(Vector3 cen) { rectMat.SetPass(0); 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) { Transform _spaceShip = GameManager.GetHero(); if(_spaceShip==null) return; float tmp = Vector3.Distance(_transform.position,_spaceShip.position); int dis = (int)tmp; string str = dis+"mt"; PilotNameGenerator cmp = this.gameObject.GetComponent<PilotNameGenerator>(); if (cmp != null) { str=cmp.getPilotName()+"\n"+str; } GUI.skin.label.normal.textColor=col; GUI.skin.label.alignment = TextAnchor.MiddleCenter; 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
Revision | Author | Commited | Message |
---|---|---|---|
134 Diff | DRuega | Fri 07 Nov, 2014 18:48:12 +0000 | In GameManager ho aggiunto HandleWeaponHit per centralizzare e generalizzare la gestione degli impatti delle armi (SU_LaserShot e Missile). |
127 Diff | DRuega | Thu 06 Nov, 2014 16:52:49 +0000 | Aggiungo PilotNameGenerator.cs Genera nomi random alle navi... così sembra più figo :) :) |
126 Diff | DRuega | Thu 06 Nov, 2014 14:45:25 +0000 | Cleanup, warnings e altre minchiate :) |
122 Diff | DRuega | Thu 06 Nov, 2014 10:12:00 +0000 | Tocco Magico :P |
118 Diff | DRuega | Mon 03 Nov, 2014 21:08:37 +0000 | aggiunto radar 3d |
117 Diff | DRuega | Tue 28 Oct, 2014 13:39:51 +0000 | |
116 Diff | DRuega | Mon 20 Oct, 2014 20:14:25 +0000 | |
112 Diff | FMMortaroli | Thu 09 Oct, 2014 14:21:59 +0000 | |
107 | FMMortaroli | Thu 09 Oct, 2014 11:56:46 +0000 |