MemoryPredictionLine.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MemoryPredictionLine : MonoBehaviour
  5. {
  6. [SerializeField] float p_y = 11f;
  7. [SerializeField] int vertexCount = 100;
  8. [SerializeField] float startWidthMultiplier = 0.5f;
  9. [SerializeField] float width = 2f;
  10. LineRenderer lineRenderer;
  11. public void Init()
  12. {
  13. lineRenderer = GetComponent<LineRenderer>();
  14. this.gameObject.SetActive(false);
  15. }
  16. public void SetMemoryPredictionLine(MemoryTab currentMemoryTab, MemoryTab nextMemoryTab)
  17. {
  18. if(lineRenderer == null || currentMemoryTab == null || nextMemoryTab == null)
  19. {
  20. return;
  21. }
  22. if(currentMemoryTab == nextMemoryTab)
  23. {
  24. this.gameObject.SetActive(false);
  25. return;
  26. }
  27. if(vertexCount <= 0)
  28. {
  29. return;
  30. }
  31. if(p_y == 0)
  32. {
  33. return;
  34. }
  35. this.gameObject.SetActive(true);
  36. lineRenderer.positionCount = vertexCount;
  37. Vector3 memoryTabPos(MemoryTab memoryTab)
  38. {
  39. Vector3 position = new Vector3();
  40. int index = GManager.instance.memoryObject.memoryTabs.IndexOf(memoryTab) - 10;
  41. position = new Vector3(9f * index, 0, 3);
  42. return position;
  43. }
  44. float alpha = Mathf.Abs(memoryTabPos(nextMemoryTab).x - memoryTabPos(currentMemoryTab).x) / 2f;
  45. float p_x = (memoryTabPos(nextMemoryTab).x + memoryTabPos(currentMemoryTab).x) / 2f;
  46. float a = (p_y - memoryTabPos(nextMemoryTab).z) / Mathf.Pow(alpha, 2);
  47. Vector3[] positions = new Vector3[vertexCount];
  48. for(int i=0;i<vertexCount;i++)
  49. {
  50. float x = 2 * alpha / vertexCount * i + -alpha + p_x;
  51. float z = function_z(x);
  52. positions[i] = new Vector3(x, 0, z);
  53. float function_z(float x)
  54. {
  55. float z = 0;
  56. z = -a * Mathf.Pow(x - p_x, 2) + p_y;
  57. return z;
  58. }
  59. }
  60. lineRenderer.SetPositions(positions);
  61. AnimationCurve animationCurve = new AnimationCurve();
  62. float a_anim = -4f * startWidthMultiplier + 4;
  63. for (int i = 0; i < vertexCount; i++)
  64. {
  65. float x = 1f / vertexCount * i;
  66. float y = function_anim_y(x);
  67. animationCurve.AddKey(x,y);
  68. float function_anim_y(float x)
  69. {
  70. float y = 0;
  71. y = -a_anim * Mathf.Pow(x - 0.5f, 2) + 1;
  72. return y;
  73. }
  74. }
  75. lineRenderer.widthCurve = animationCurve;
  76. lineRenderer.widthMultiplier = width;
  77. }
  78. }