用颜色渲染一组点

时间:2017-08-25 06:02:24

标签: unity3d

我想用一致的颜色渲染一组点,我已经按照这个例子http://www.kamend.com/2014/05/rendering-a-point-cloud-inside-unity/来随机生成点和颜色。

C#代码

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
            }
        }
    }
}

有没有改变这些点的颜色?由于我使用常量值设置这些点的颜色,但它不起作用。

1 个答案:

答案 0 :(得分:0)

也许团结有问题。我建立了很多次并重新启动统一,然后它完美地工作。