123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 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<MeshRenderer>();
- if (m_MeshRenderer == null)
- {
- m_MeshRenderer = gameObject.AddComponent<MeshRenderer>();
- }
- }
- if (m_MeshFilter == null)
- {
- m_MeshFilter = gameObject.GetComponent<MeshFilter>();
- if (m_MeshFilter == null)
- {
- m_MeshFilter = gameObject.AddComponent<MeshFilter>();
- }
- }
-
- 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);
- //}
- }
- }
|