Unity3d 使用颜色渲染一组点

Unity3d 使用颜色渲染一组点,unity3d,Unity3d,我想用统一的颜色渲染一组点,我已经按照这个示例随机生成点及其颜色 C#代码 有没有办法改变这些点的颜色?因为我将这些点的颜色设置为一个常量值,但它不起作用。可能是统一性有问题。我构建了很多次,然后重新启动unity,然后它就可以正常工作了。我建议您将包含点成员的结构/类定义为Vector3类型,将索引成员定义为int类型,将颜色成员定义为color类型,可以任意调用此结构/类,并创建一个长度为numPoints的结构/类数组,而不是创建三个长度为numPoints的数组:点、索引和颜色。这使代码

我想用统一的颜色渲染一组点,我已经按照这个示例随机生成点及其颜色

C#代码


有没有办法改变这些点的颜色?因为我将这些点的颜色设置为一个常量值,但它不起作用。

可能是统一性有问题。我构建了很多次,然后重新启动unity,然后它就可以正常工作了。

我建议您将包含点成员的结构/类定义为Vector3类型,将索引成员定义为int类型,将颜色成员定义为color类型,可以任意调用此结构/类,并创建一个长度为numPoints的结构/类数组,而不是创建三个长度为numPoints的数组:点、索引和颜色。这使代码更易于使用、阅读,并提高了性能。
public class PointCloud : MonoBehaviour 
{ 
  private Mesh mesh;
  int numPoints = 60000;

  // Use this for initialization
  void Start () {
    mesh = new Mesh();

    GetComponent<MeshFilter>().mesh = mesh;
    CreateMesh();
  }

  void CreateMesh() {
    Vector3[] points = new Vector3[numPoints];
    int[] indecies = new int[numPoints];
    Color[] colors = new Color[numPoints];
    for(int i=0;i<points.Length;++i) {
        points[i] = new Vector3(Random.Range(-10,10), Random.Range (-10,10), Random.Range (-10,10));
        indecies[i] = i;
        colors[i] = new Color(Random.Range(0.0f,1.0f),Random.Range (0.0f,1.0f),Random.Range(0.0f,1.0f),1.0f);
    }

    mesh.vertices = points;
    mesh.colors = colors;
    mesh.SetIndices(indecies, MeshTopology.Points,0);

  }
}
Shader "Custom/VertexColor" {
    SubShader
    {
        Pass
        {
            LOD 200

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            struct VertexInput 
            {
                float4 v : POSITION;
                float4 color: COLOR;
            };

            struct VertexOutput 
            {
                float4 pos : SV_POSITION;
                float4 col : COLOR;
            };

            VertexOutput vert(VertexInput v) 
            {
                VertexOutput o;
                o.pos = UnityObjectToClipPos(v.v);
                o.col = v.color;

                return o;
            }

            float4 frag(VertexOutput o) : COLOR
            {
                return o.col;
            }

            ENDCG
            }
        }
    }
}