using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshTest : MonoBehaviour { MeshRenderer m_MeshRenderer; MeshFilter m_MeshFilter; Material m_Material; private Shader m_Shader; Mesh m_Mesh; Vector3[] pos = new Vector3[100]; void Start() { Init(); for (int i = 0; i < 100; i++) { pos[i] = new Vector3(i * 0.1f,0, i * 0.1f); } DrawCloud(pos); } void Init() { if (m_MeshRenderer == null) { m_MeshRenderer = gameObject.GetComponent(); if (m_MeshRenderer == null) { m_MeshRenderer = gameObject.AddComponent(); } } if (m_MeshFilter == null) { m_MeshFilter = gameObject.GetComponent(); if (m_MeshFilter == null) { m_MeshFilter = gameObject.AddComponent(); } } if (m_Mesh == null) { m_Mesh = new Mesh(); } m_MeshFilter.mesh = m_Mesh; m_Material = m_MeshRenderer.material; m_MeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; m_MeshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; m_MeshRenderer.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off; } public void DrawCloud(Vector3[] vers) { Debug.Log("绘制点云"); int numPoints = vers.Length; int[] indices = new int[numPoints]; Vector3[] pts = new Vector3[numPoints]; Color32[] cols = new Color32[numPoints]; for (int i = 0; i < numPoints; i++) { indices[i] = i; pts[i] = vers[i]; } for (int i = 0; i < cols.Length; i++) { cols[i] = Color.green; } m_Mesh.Clear(); m_Mesh.vertices = pts; m_Mesh.colors32 = cols; m_Mesh.SetIndices(indices, MeshTopology.Points, 0); m_Mesh.bounds = new Bounds(transform.position, new Vector3(float.MaxValue, float.MinValue, float.MaxValue)); //m_Mesh.bounds = new Bounds(transform.position, new Vector3(0.1f, 0.1f, 0.1f)); m_MeshFilter.mesh = m_Mesh; } private void OnRenderObject() { if ( m_Material != null) { m_MeshRenderer.enabled = true; m_Material.SetFloat("_PerspectiveEnabled", 1f); m_Material.SetFloat("_PointSize", 100f); m_Material.SetColor("_PointColor", Color.yellow); } else { m_MeshRenderer.enabled = false; } } void Update() { //if (Input.GetKeyDown(KeyCode.Mouse0)) //{ // for (int i = 0; i < 100; i++) // { // pos[i] = -new Vector3(i, 0, 0); // } // DrawCloud(pos); //} } }