NextStepRoutePathRenderer.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using UnityEngine;
  2. namespace ARLocation.MapboxRoutes
  3. {
  4. public class NextStepRoutePathRenderer : AbstractRouteRenderer
  5. {
  6. [System.Serializable]
  7. public class SettingsData
  8. {
  9. [Tooltip("The material used to render the line.")]
  10. public Material LineMaterial;
  11. [Tooltip("A texture offset factor used to move the line's texture as to appear that a pattern is moving." +
  12. " If you don't need this just set it to '0'.")]
  13. public float TextureOffsetFactor = -4.0f;
  14. }
  15. [System.Serializable]
  16. private class State
  17. {
  18. public GameObject Go;
  19. public LineRenderer LineRenderer;
  20. }
  21. public SettingsData Settings = new SettingsData{};
  22. private State state = new State();
  23. public void OnDestroy()
  24. {
  25. if (state.Go != null)
  26. {
  27. GameObject.Destroy(state.Go);
  28. }
  29. }
  30. public void OnEnable()
  31. {
  32. state.Go = new GameObject("[NextStepRoutePathRenderer]");
  33. state.LineRenderer = state.Go.AddComponent<LineRenderer>();
  34. state.LineRenderer.startWidth = 0.25f;
  35. state.LineRenderer.useWorldSpace = true;
  36. state.LineRenderer.alignment = LineAlignment.View;
  37. state.LineRenderer.material = Settings.LineMaterial;
  38. state.LineRenderer.textureMode = LineTextureMode.Tile;
  39. state.LineRenderer.numCornerVertices = 2;
  40. }
  41. public void OnDisable()
  42. {
  43. GameObject.Destroy(state.Go);
  44. state.Go = null;
  45. }
  46. public override void Init(RoutePathRendererArgs args)
  47. {
  48. }
  49. public override void OnRouteUpdate(RoutePathRendererArgs args)
  50. {
  51. var groundHeight = args.Route.Settings.GroundHeight;
  52. var y = Camera.main.transform.position.y;
  53. var userPos = MathUtils.SetY(args.UserPos, y-groundHeight);
  54. var targetPos = MathUtils.SetY(args.TargetPos, y-groundHeight);
  55. state.LineRenderer.material.SetVector("_Origin", args.UserPos);
  56. state.LineRenderer.positionCount = 2;
  57. state.LineRenderer.SetPosition(0, userPos);
  58. state.LineRenderer.SetPosition(1, targetPos);
  59. state.LineRenderer.material.SetTextureOffset("_MainTex", Settings.TextureOffsetFactor * new Vector2(args.Distance, 0));
  60. }
  61. }
  62. }