0% found this document useful (0 votes)
166 views4 pages

Point Cloud

This document provides code examples for rendering a point cloud in Unity using a MeshRenderer. It includes a script that generates a point cloud with randomized positions and colors, renders it to a mesh. It also includes a shader that displays the vertex colors. Additionally, it contains a script that enables OpenGL settings for smooth points with customizable sizes.

Uploaded by

abbas fattah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views4 pages

Point Cloud

This document provides code examples for rendering a point cloud in Unity using a MeshRenderer. It includes a script that generates a point cloud with randomized positions and colors, renders it to a mesh. It also includes a shader that displays the vertex colors. Additionally, it contains a script that enables OpenGL settings for smooth points with customizable sizes.

Uploaded by

abbas fattah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

1
2
3 using UnityEngine;
4 using System.Collections;
5
6
7 [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class PointCloud : MonoBehaviour {
8
9
private Mesh mesh;
10 int numPoints = 60000;
11
12 // Use this for initialization
13 void Start () {
14 mesh = new Mesh();
15
GetComponent<MeshFilter>().mesh = mesh;
16 CreateMesh();
17 }
18
19 void CreateMesh() {
20 Vector3[] points = new Vector3[numPoints];
21 int[] indecies = new int[numPoints];
Color[] colors = new Color[numPoints];
22 for(int i=0;i<points.Length;++i) {
23 points[i] = new Vector3(Random.Range(-10,10), Random.Range (-10,10), Ran
24 indecies[i] = i;
25 colors[i] = new Color(Random.Range(0.0f,1.0f),Random.Range (0.0f,1.0f),R
}
26
27 mesh.vertices = points;
28 mesh.colors = colors;
29 mesh.SetIndices(indecies, MeshTopology.Points,0);
30
31 }
32 }
33
34

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

1 Shader "Custom/VertexColor" {
SubShader {
2 Pass {
3 LOD 200
4
5 CGPROGRAM
6 #pragma vertex vert
#pragma fragment frag
7
8 struct VertexInput {
9 float4 v : POSITION;
10 float4 color: COLOR;
11 };
12
13 struct VertexOutput {
float4 pos : SV_POSITION;
14 float4 col : COLOR;
15 };
16
17 VertexOutput vert(VertexInput v) {
18
19 VertexOutput o;
o.pos = mul(UNITY_MATRIX_MVP, v.v);
20
o.col = v.color;
21
22 return o;
23 }
24
25 float4 frag(VertexOutput o) : COLOR {
26 return o.col;
}
27
28 ENDCG
29 }
30 }
31
32 }
33
34
35
36
37

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

1 #if UNITY_STANDALONE
2 #define IMPORT_GLENABLE
#endif
3
4 using UnityEngine;
5 using System;
6 using System.Collections;
7 using System.Runtime.InteropServices;
8
9 public class EnablePointSize : MonoBehaviour
10 {
const UInt32 GL_VERTEX_PROGRAM_POINT_SIZE = 0x8642;
11 const UInt32 GL_POINT_SMOOTH = 0x0B10;
12
13 const string LibGLPath =
14 #if UNITY_STANDALONE_WIN
15 "opengl32.dll";
#elif UNITY_STANDALONE_OSX
16 "/System/Library/Frameworks/OpenGL.framework/OpenGL";
17 #elif UNITY_STANDALONE_LINUX
18 "libGL"; // Untested on Linux, this may not be correct
19 #else
20 null; // OpenGL ES platforms don't require this feature
#endif
21
22 #if IMPORT_GLENABLE
23 [DllImport(LibGLPath)]
24 public static extern void glEnable(UInt32 cap);
25
26 private bool mIsOpenGL;
27
28 void Start()
{
29 mIsOpenGL = SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
30 }
31
32 void OnPreRender()
33 {
34 if (mIsOpenGL)
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
35 glEnable(GL_POINT_SMOOTH);
36 }
37 #endif
38 }
39
40
41
42
43
44
point cloud shaders unity | 4 Comments
Comments:

HashimY says:
2014-06-24 13:44:52Hi.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:05Great 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 *

You might also like