RenderParcours.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RenderParcours : MonoBehaviour
  5. {
  6. public Transform MyParent;
  7. public Material LineMaterial;
  8. public ConfigReader Config;
  9. void Start()
  10. {
  11. GetParcoursData();
  12. }
  13. void GetParcoursData()
  14. {
  15. /** "LineWidth" : 0.1,
  16. "Kind" : "PieceWise",
  17. "LinePieces" : [
  18. { "x" : 1.1, "y" : 12.2},
  19. { "x" : 1.43, "y" : 4.5},
  20. { "x" : 1.8, "y" : 3.5},
  21. { "x" : 1.1, "y" : 2.5}
  22. ]**/
  23. if (Config.configuration.Parcours.Kind == "PieceWise")
  24. {
  25. ConfigLinePieces[] lps = Config.configuration.Parcours.LinePieces;
  26. int AmntLinePieces = lps.Length / 2; // Length should have been checked by ConfigReader.ValidParcours() already
  27. for (int i = 0; i < AmntLinePieces; i++)
  28. {
  29. GameObject Renderer = new GameObject();
  30. Renderer.transform.SetParent(MyParent);
  31. LineRenderer lr = Renderer.AddComponent<LineRenderer>();
  32. PrepareLR(lr, Renderer);
  33. lr.widthCurve = AnimationCurve.Constant(0.0f, 1f, Config.configuration.Parcours.LineWidth);
  34. Vector3[] posvector = new Vector3[2];
  35. posvector[0] = new Vector3(lps[i*2].x, lps[i*2].y, 0f);
  36. posvector[1] = new Vector3(lps[i * 2 + 1].x, lps[i * 2 + 1].y, 0f);
  37. lr.positionCount = posvector.Length;
  38. lr.SetPositions(posvector);
  39. }
  40. }
  41. else if (Config.configuration.Parcours.Kind == "Trail")
  42. {
  43. GameObject Renderer = new GameObject();
  44. Renderer.transform.SetParent(MyParent);
  45. LineRenderer lr = Renderer.AddComponent<LineRenderer>();
  46. PrepareLR(lr, Renderer);
  47. lr.widthCurve = AnimationCurve.Constant(0.0f, 1f, Config.configuration.Parcours.LineWidth);
  48. ConfigLinePieces[] lps = Config.configuration.Parcours.LinePieces;
  49. Vector3[] posvector = new Vector3[lps.Length];
  50. for (int i = 0; i < lps.Length; i++)
  51. {
  52. posvector[i] = new Vector3(lps[i].x, lps[i].y, 0f);
  53. }
  54. lr.positionCount = posvector.Length;
  55. lr.SetPositions(posvector);
  56. }
  57. }
  58. void PrepareLR(LineRenderer lr, GameObject Renderer)
  59. {
  60. lr.alignment = LineAlignment.TransformZ;
  61. lr.material = LineMaterial;
  62. lr.textureMode = LineTextureMode.Stretch;
  63. lr.useWorldSpace = false;
  64. lr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  65. Renderer.transform.localPosition = new Vector3(0, 0, -0.0001f);
  66. Renderer.transform.localRotation = Quaternion.identity;
  67. lr.generateLightingData = true;
  68. lr.numCornerVertices = 3;
  69. lr.numCapVertices = 3;
  70. }
  71. }