Rendering a Point Cloud inside Unity

May 27th, 2014

Here is a short example of how to render a point cloud using MeshRenderer inside Unity, have in mind that you have a limit of 65k points per mesh, so if you want to render more points, you need to split them.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class PointCloud : MonoBehaviour {

	private Mesh mesh;
	int numPoints = 60000;

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

		GetComponent<MeshFilter>().mesh = mesh;

	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);


And here is the code for the material’s shader, that will use the vertex color from the mesh:

Shader "Custom/VertexColor" {
	SubShader {
	Pass {
		LOD 200
		#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 = mul(UNITY_MATRIX_MVP, v.v);
			o.col = v.color;
			return o;
		float4 frag(VertexOutput o) : COLOR {
			return o.col;



And this is how you enable GL_VERTEX_PROGRAM_POINT_SIZE and GL_POINT_SMOOTH, so you could set the point size in your shader and have smooth points. Uou must attach this script to your main Camera. The code was found on this thread


using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;

public class EnablePointSize : MonoBehaviour 
	const UInt32 GL_VERTEX_PROGRAM_POINT_SIZE = 0x8642;
	const UInt32 GL_POINT_SMOOTH = 0x0B10;
	const string LibGLPath =
	"libGL";  // Untested on Linux, this may not be correct
	null;   // OpenGL ES platforms don't require this feature
	public static extern void glEnable(UInt32 cap);
	private bool mIsOpenGL;
	void Start()
		mIsOpenGL = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
	void OnPreRender()
		if (mIsOpenGL)


HashimY says:
2014-06-24 13:44:52
Hi. Cheers for that. I've been scratching my head on how to display point clouds properly for days. The shader worked great. I'm having a little trouble with the "EnablePointSize" script. In the editor I'm getting "the associated script cannot be loaded....." message. I cannot seem to find what is causing the error. I tried reducing the code by simply saying const string LibGLPath ="opengl32.dll"; Not quite sure what I'm missing Cheers Hashim
Nelson says:
2015-09-15 21:59:05
Great info. Thank you for posting. I can't seem to get the color of the points to change using your shader. Is there something special we need to do to the material?

Leave a Reply

Your email address will not be published. Required fields are marked *