CreateSinShapedLineStrip.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using System.Collections;
  3. using VolumetricLines;
  4. /// <summary>
  5. /// Create a sin shaped line strip using a volumetric line strip
  6. /// </summary>
  7. public class CreateSinShapedLineStrip : MonoBehaviour
  8. {
  9. public int m_numVertices = 50;
  10. public Material m_volumetricLineStripMaterial;
  11. public Color m_color;
  12. public float m_start = 0f;
  13. public float m_end = Mathf.PI;
  14. // Use this for initialization
  15. void Start ()
  16. {
  17. // Create an empty game object
  18. GameObject go = new GameObject();
  19. go.transform.parent = transform;
  20. // Add the MeshFilter component, VolumetricLineStripBehavior requires it
  21. go.AddComponent<MeshFilter>();
  22. // Add a MeshRenderer, VolumetricLineStripBehavior requires it
  23. go.AddComponent<MeshRenderer>();
  24. // Add the VolumetricLineStripBehavior and set parameters, like color and all the vertices of the line
  25. var volLineStrip = go.AddComponent<VolumetricLineStripBehavior>();
  26. volLineStrip.DoNotOverwriteTemplateMaterialProperties = false;
  27. volLineStrip.TemplateMaterial = m_volumetricLineStripMaterial;
  28. volLineStrip.LineColor = m_color;
  29. volLineStrip.LineWidth = 55.0f;
  30. volLineStrip.LightSaberFactor = 0.83f;
  31. var lineVertices = new Vector3[m_numVertices];
  32. for (int i=0; i < m_numVertices; ++i)
  33. {
  34. float x = Mathf.Lerp(m_start, m_end, (float)i / (float)(m_numVertices-1));
  35. float y = Mathf.Sin(x);
  36. lineVertices[i] = gameObject.transform.TransformPoint(new Vector3(x, y, 0f));
  37. }
  38. volLineStrip.UpdateLineVertices(lineVertices);
  39. }
  40. void OnDrawGizmos()
  41. {
  42. Gizmos.color = Color.green;
  43. for (int i=0; i < m_numVertices; ++i)
  44. {
  45. float x = Mathf.Lerp(m_start, m_end, (float)i / (float)(m_numVertices-1));
  46. float y = Mathf.Sin(x);
  47. Gizmos.DrawSphere(gameObject.transform.TransformPoint(new Vector3(x, y, 0f)), 5f);
  48. }
  49. }
  50. }