ArcTeleport.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. public class ArcTeleport : MonoBehaviour
  7. {
  8. private void Start()
  9. {
  10. this.m_nBackTrailCount = this.m_nTrailCount;
  11. this.m_trWarpCircle = base.transform.Find("WarpCircle");
  12. NDebug.Assert(this.m_trWarpCircle != null, "WarpCircleが見つかりません。");
  13. this.m_Circle_Size = this.m_trWarpCircle.localScale;
  14. this.m_trWarpCircle.gameObject.SetActive(false);
  15. this.m_Line_Obj = new GameObject();
  16. this.m_Line_Obj.name = "Teleport_Line";
  17. this.m_Line_Obj.AddComponent<SkinnedMeshRenderer>();
  18. SkinnedMeshRenderer component = this.m_Line_Obj.GetComponent<SkinnedMeshRenderer>();
  19. component.sharedMesh = new Mesh();
  20. component.sharedMesh.name = "Line_Mesh";
  21. this.Mesh_Setting(component);
  22. this.Bone_Setting(component);
  23. component.material = new Material(Resources.Load<Shader>(this.m_Line_Shader_Path));
  24. this.m_Line_Tex = Resources.Load<Texture>(this.m_Line_Texture_Path);
  25. this.m_Line_Tex_Gray = Resources.Load<Texture>(this.m_Gray_Texture_Path);
  26. component.material.SetTexture("_MainTex", this.m_Line_Tex);
  27. this.m_Line_Material = component.material;
  28. component.sharedMesh.RecalculateNormals();
  29. component.sharedMesh.RecalculateBounds();
  30. this.m_Line_Obj.transform.parent = base.transform;
  31. this.m_Line_Obj.SetActive(false);
  32. }
  33. private void Mesh_Setting(SkinnedMeshRenderer render)
  34. {
  35. Vector3[] array = new Vector3[this.m_nTrailCount * 4];
  36. Vector2[] array2 = new Vector2[this.m_nTrailCount * 4];
  37. List<int> list = new List<int>();
  38. int i;
  39. for (i = 0; i < this.m_nTrailCount; i++)
  40. {
  41. for (int j = 0; j < 4; j++)
  42. {
  43. array[i * 4 + j].x = Mathf.Cos(3.14159274f * (float)j) * this.m_Line_Width / 2f * (float)(1 - j / 2);
  44. array[i * 4 + j].y = Mathf.Cos(3.14159274f * (float)j) * this.m_Line_Width / 2f * (float)(j / 2);
  45. array[i * 4 + j].z = this.m_fLineLength * ((float)i / (float)(this.m_nTrailCount - 1)) - this.m_fLineLength / 2f;
  46. array2[i * 4 + j].x = (float)i / (float)(this.m_nTrailCount - 1);
  47. array2[i * 4 + j].y = (float)(j % 2);
  48. }
  49. if (i != 0)
  50. {
  51. int k;
  52. for (k = 0; k < 2; k++)
  53. {
  54. int[] source = new int[]
  55. {
  56. 0,
  57. 1,
  58. 5,
  59. 0,
  60. 5,
  61. 4
  62. };
  63. list.AddRange(from e in source
  64. select e + k * 2 + (i - 1) * 4);
  65. }
  66. }
  67. }
  68. render.sharedMesh.vertices = array;
  69. render.sharedMesh.uv = array2;
  70. render.sharedMesh.triangles = list.ToArray();
  71. }
  72. private void Bone_Setting(SkinnedMeshRenderer render)
  73. {
  74. this.m_Line_Bones = new Transform[this.m_nTrailCount];
  75. Matrix4x4[] array = new Matrix4x4[this.m_nTrailCount];
  76. BoneWeight[] array2 = new BoneWeight[this.m_nTrailCount * 4];
  77. for (int i = 0; i < this.m_nTrailCount; i++)
  78. {
  79. GameObject gameObject = new GameObject();
  80. this.m_Line_Bones[i] = gameObject.transform;
  81. this.m_Line_Bones[i].name = "Line_Bone";
  82. this.m_Line_Bones[i].parent = this.m_Line_Obj.transform;
  83. this.m_Line_Bones[i].position = Vector3.forward * (this.m_fLineLength * ((float)i / (float)(this.m_nBackTrailCount - 1)) - this.m_fLineLength / 2f);
  84. array[i] = this.m_Line_Bones[i].worldToLocalMatrix * this.m_Line_Obj.transform.localToWorldMatrix;
  85. for (int j = 0; j < 4; j++)
  86. {
  87. array2[i * 4 + j] = new BoneWeight
  88. {
  89. boneIndex0 = i,
  90. weight0 = 1f
  91. };
  92. }
  93. }
  94. render.bones = this.m_Line_Bones;
  95. render.sharedMesh.bindposes = array;
  96. render.sharedMesh.boneWeights = array2;
  97. }
  98. public void SetCallBackOnMove(ArcTeleport.dgOnMove f_dgMove)
  99. {
  100. this.m_dgOnMove = f_dgMove;
  101. }
  102. public void WarpSelecting()
  103. {
  104. this.m_bPush = true;
  105. }
  106. public bool IsWarpSelecting()
  107. {
  108. return this.m_bPush;
  109. }
  110. public void WarpDicide()
  111. {
  112. this.m_bPush = false;
  113. }
  114. public void WarpCancel()
  115. {
  116. this.m_bBackPush = (this.m_bPush = false);
  117. }
  118. private void OnEnable()
  119. {
  120. this.WarpCancel();
  121. }
  122. private void OnDisable()
  123. {
  124. this.WarpCancel();
  125. }
  126. private IEnumerator Circle_Animation()
  127. {
  128. this.m_trWarpCircle.localScale = Vector3.forward * this.m_Circle_Size.z;
  129. float timer = 0f;
  130. for (;;)
  131. {
  132. timer += Time.deltaTime;
  133. if (timer >= this.m_Anime_Time)
  134. {
  135. break;
  136. }
  137. this.m_trWarpCircle.localScale = Vector3.forward * this.m_Circle_Size.z + new Vector3(this.m_Circle_Size.x, this.m_Circle_Size.y, 0f) * timer / this.m_Anime_Time;
  138. yield return null;
  139. }
  140. yield break;
  141. yield break;
  142. }
  143. private Vector3 Circle_Angle()
  144. {
  145. Vector3 result = Vector3.zero;
  146. Vector3 toDirection = this.m_trWarpCircle.position - GameMain.Instance.MainCamera.GetPos();
  147. toDirection.y = 0f;
  148. toDirection.Normalize();
  149. result = Quaternion.FromToRotation(Vector3.forward, toDirection).eulerAngles;
  150. result.x = (result.z = 0f);
  151. return result;
  152. }
  153. private IEnumerator Line_Flash()
  154. {
  155. float timer = 0f;
  156. Color line_color = Color.white;
  157. string tex_name = this.m_Line_Tex.name;
  158. for (;;)
  159. {
  160. timer += Time.deltaTime;
  161. float time_rate = Mathf.Abs(Mathf.Cos(0.0174532924f * (180f * (timer / this.m_Flash_Time))));
  162. if (!this.m_trWarpCircle.gameObject.activeSelf)
  163. {
  164. if (tex_name != this.m_Line_Tex_Gray.name)
  165. {
  166. this.m_Line_Material.SetTexture("_MainTex", this.m_Line_Tex_Gray);
  167. tex_name = this.m_Line_Tex_Gray.name;
  168. }
  169. }
  170. else if (tex_name != this.m_Line_Tex.name)
  171. {
  172. this.m_Line_Material.SetTexture("_MainTex", this.m_Line_Tex);
  173. tex_name = this.m_Line_Tex.name;
  174. }
  175. line_color.a = Mathf.Lerp(1f, this.m_Min_Alpha, time_rate);
  176. this.m_Line_Material.SetColor("_Color", line_color);
  177. yield return null;
  178. }
  179. yield break;
  180. }
  181. private void Update()
  182. {
  183. bool flag = false;
  184. if (GameMain.Instance.MainCamera.IsFadeProc())
  185. {
  186. this.m_bBackHit = (this.m_bBackPush = (this.m_bPush = false));
  187. base.StopCoroutine("Line_Flash");
  188. this.m_Line_Obj.SetActive(false);
  189. return;
  190. }
  191. if (this.m_bPush)
  192. {
  193. if (this.m_nBackTrailCount != this.m_nTrailCount)
  194. {
  195. this.m_nBackTrailCount = this.m_nTrailCount;
  196. }
  197. if (!this.m_Line_Obj.activeSelf)
  198. {
  199. base.StartCoroutine("Line_Flash");
  200. }
  201. this.m_Line_Obj.SetActive(true);
  202. Vector3 normalized = this.m_trParent.forward.normalized;
  203. Debug.DrawLine(this.m_trParent.position, this.m_trParent.position + normalized * 1f, Color.green);
  204. Vector3 vector = new Vector3(normalized.x, 0f, normalized.z);
  205. Vector3 normalized2 = vector.normalized;
  206. Debug.DrawLine(this.m_trParent.position, this.m_trParent.position + normalized2 * 1f, Color.cyan);
  207. float num = Vector3.Angle(normalized, normalized2);
  208. if (normalized.y < normalized2.y)
  209. {
  210. num *= -1f;
  211. }
  212. Vector3 zero = Vector3.zero;
  213. float num2 = Vector3.Angle(Vector3.forward, normalized2);
  214. if (normalized2.x < 0f)
  215. {
  216. num2 *= -1f;
  217. }
  218. Quaternion rotation = Quaternion.Euler(0f, num2, 0f);
  219. Vector3 zero2 = Vector3.zero;
  220. float num3 = this.m_Defo_Display_Length;
  221. for (float num4 = this.m_fLineSplitLength; num4 < this.m_fLineLength; num4 += this.m_fLineSplitLength)
  222. {
  223. float num5 = this.m_fVelocity * Mathf.Cos(num * 0.0174532924f) * num4;
  224. float num6 = this.m_fVelocity * Mathf.Sin(num * 0.0174532924f) * num4 - 0.5f * this.m_fGravity * (num4 * num4);
  225. Vector3 vector2 = this.m_trParent.position + rotation * zero;
  226. Vector3 vector3 = this.m_trParent.position + rotation * new Vector3(0f, num6, num5);
  227. Debug.DrawLine(vector2, vector3);
  228. zero.Set(0f, num6, num5);
  229. if (!flag)
  230. {
  231. this.m_ray.origin = vector2;
  232. this.m_ray.direction = vector3 - vector2;
  233. if (Physics.Raycast(this.m_ray, out this.m_hit, Vector3.Distance(vector2, vector3), 512))
  234. {
  235. flag = true;
  236. this.m_vHitPos = this.m_hit.point;
  237. this.m_trWarpCircle.transform.rotation = Quaternion.Euler(this.m_Fixed_Angle);
  238. if (!this.m_trWarpCircle.gameObject.activeSelf)
  239. {
  240. base.StartCoroutine("Circle_Animation");
  241. }
  242. this.m_trWarpCircle.gameObject.SetActive(true);
  243. this.m_trWarpCircle.position = this.m_vHitPos;
  244. num3 = num4;
  245. }
  246. }
  247. }
  248. if (this.m_nTrailCount > 0)
  249. {
  250. float num7 = num3 / (float)this.m_nTrailCount;
  251. if (num7 <= 0f)
  252. {
  253. num7 = 1f;
  254. }
  255. zero = Vector3.zero;
  256. float num8 = num7;
  257. for (int i = 0; i < this.m_nTrailCount; i++)
  258. {
  259. num8 += num7;
  260. float num9 = this.m_fVelocity * Mathf.Cos(num * 0.0174532924f) * num8;
  261. float num10 = this.m_fVelocity * Mathf.Sin(num * 0.0174532924f) * num8 - 0.5f * this.m_fGravity * (num8 * num8);
  262. Vector3 vector4 = this.m_trParent.position + rotation * zero;
  263. Vector3 end = this.m_trParent.position + rotation * new Vector3(0f, num10, num9);
  264. Debug.DrawLine(vector4, end, Color.blue);
  265. zero.Set(0f, num10, num9);
  266. this.m_Line_Bones[i].position = vector4;
  267. }
  268. }
  269. }
  270. else
  271. {
  272. if (this.m_bBackPush && this.m_bBackHit && this.m_dgOnMove != null)
  273. {
  274. this.m_dgOnMove(this.m_vHitPos, this.Circle_Angle());
  275. }
  276. base.StopCoroutine("Line_Flash");
  277. this.m_Line_Obj.SetActive(false);
  278. }
  279. if (!flag)
  280. {
  281. this.m_trWarpCircle.localScale = this.m_Circle_Size;
  282. this.m_trWarpCircle.gameObject.SetActive(false);
  283. base.StopCoroutine("Circle_Animation");
  284. }
  285. this.m_bBackHit = flag;
  286. this.m_bBackPush = this.m_bPush;
  287. }
  288. public bool m_bPush;
  289. private bool m_bBackPush;
  290. public Transform m_trParent;
  291. public float m_fGravity = 0.98f;
  292. public float m_fVelocity = 100f;
  293. public float m_fLineLength = 10f;
  294. public float m_fLineSplitLength = 0.1f;
  295. public int m_nTrailCount = 10;
  296. private int m_nBackTrailCount;
  297. public ArcTeleport.dgOnMove m_dgOnMove;
  298. private Vector3 m_vHitPos;
  299. private Ray m_ray = default(Ray);
  300. private RaycastHit m_hit;
  301. private bool m_bBackHit;
  302. private Transform m_trWarpCircle;
  303. [SerializeField]
  304. [Header("ワープサークルの固定角度")]
  305. private Vector3 m_Fixed_Angle = Vector3.right * -90f;
  306. [SerializeField]
  307. [Header("サークルのアニメーション時間")]
  308. private float m_Anime_Time = 0.75f;
  309. private Vector3 m_Circle_Size;
  310. private string m_Line_Shader_Path = "Shaders/Telepot_Line";
  311. private GameObject m_Line_Obj;
  312. private Transform[] m_Line_Bones;
  313. private string m_Line_Texture_Path = "SteamVR/ArcTeleport";
  314. private string m_Gray_Texture_Path = "SteamVR/ArcTeleport_Gray";
  315. private Texture m_Line_Tex;
  316. private Texture m_Line_Tex_Gray;
  317. [SerializeField]
  318. [Header("ラインの横幅")]
  319. private float m_Line_Width = 0.1f;
  320. [SerializeField]
  321. [Header("表示するラインの長さの基準値")]
  322. private float m_Defo_Display_Length = 5.5f;
  323. [SerializeField]
  324. [Header("点滅時間")]
  325. private float m_Flash_Time = 1.5f;
  326. [SerializeField]
  327. [Range(0f, 1f)]
  328. private float m_Min_Alpha = 0.4f;
  329. private Material m_Line_Material;
  330. public delegate void dgOnMove(Vector3 f_vPosWorld, Vector3 f_vRotWorld);
  331. }