MeshTest.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MeshTest : MonoBehaviour
  5. {
  6. MeshRenderer m_MeshRenderer;
  7. MeshFilter m_MeshFilter;
  8. Material m_Material;
  9. private Shader m_Shader;
  10. Mesh m_Mesh;
  11. Vector3[] pos = new Vector3[100];
  12. void Start()
  13. {
  14. Init();
  15. for (int i = 0; i < 100; i++)
  16. {
  17. pos[i] = new Vector3(i * 0.1f,0, i * 0.1f);
  18. }
  19. DrawCloud(pos);
  20. }
  21. void Init()
  22. {
  23. if (m_MeshRenderer == null)
  24. {
  25. m_MeshRenderer = gameObject.GetComponent<MeshRenderer>();
  26. if (m_MeshRenderer == null)
  27. {
  28. m_MeshRenderer = gameObject.AddComponent<MeshRenderer>();
  29. }
  30. }
  31. if (m_MeshFilter == null)
  32. {
  33. m_MeshFilter = gameObject.GetComponent<MeshFilter>();
  34. if (m_MeshFilter == null)
  35. {
  36. m_MeshFilter = gameObject.AddComponent<MeshFilter>();
  37. }
  38. }
  39. if (m_Mesh == null)
  40. {
  41. m_Mesh = new Mesh();
  42. }
  43. m_MeshFilter.mesh = m_Mesh;
  44. m_Material = m_MeshRenderer.material;
  45. m_MeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  46. m_MeshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  47. m_MeshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  48. }
  49. public void DrawCloud(Vector3[] vers)
  50. {
  51. Debug.Log("绘制点云");
  52. int numPoints = vers.Length;
  53. int[] indices = new int[numPoints];
  54. Vector3[] pts = new Vector3[numPoints];
  55. Color32[] cols = new Color32[numPoints];
  56. for (int i = 0; i < numPoints; i++)
  57. {
  58. indices[i] = i;
  59. pts[i] = vers[i];
  60. }
  61. for (int i = 0; i < cols.Length; i++)
  62. {
  63. cols[i] = Color.green;
  64. }
  65. m_Mesh.Clear();
  66. m_Mesh.vertices = pts;
  67. m_Mesh.colors32 = cols;
  68. m_Mesh.SetIndices(indices, MeshTopology.Points, 0);
  69. m_Mesh.bounds = new Bounds(transform.position, new Vector3(float.MaxValue, float.MinValue, float.MaxValue));
  70. //m_Mesh.bounds = new Bounds(transform.position, new Vector3(0.1f, 0.1f, 0.1f));
  71. m_MeshFilter.mesh = m_Mesh;
  72. }
  73. private void OnRenderObject()
  74. {
  75. if ( m_Material != null)
  76. {
  77. m_MeshRenderer.enabled = true;
  78. m_Material.SetFloat("_PerspectiveEnabled", 1f);
  79. m_Material.SetFloat("_PointSize", 100f);
  80. m_Material.SetColor("_PointColor", Color.yellow);
  81. }
  82. else
  83. {
  84. m_MeshRenderer.enabled = false;
  85. }
  86. }
  87. void Update()
  88. {
  89. //if (Input.GetKeyDown(KeyCode.Mouse0))
  90. //{
  91. // for (int i = 0; i < 100; i++)
  92. // {
  93. // pos[i] = -new Vector3(i, 0, 0);
  94. // }
  95. // DrawCloud(pos);
  96. //}
  97. }
  98. }