I am working on a project for college in Unity that involves displaying a very big pointcloud. I have implemented the point cloud as a mesh with 1 vertex per point and no triangles. I want to implement a shader in Unity that creates a triangle for each vertex and colors it of the color of the vertex.
I have no experience with shaders and I have developed the code trying to follow multiple resources online.
At the current state the code simply makes the pointcloud disappear and nothing is displayed.
Thank you!
Shader "Custom/SpherePoint" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Radius ("Sphere Radius", float) = 0.01
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#pragma target 4.0 // Use shader model 3.0 target, to get nicer looking lighting
#include "UnityCG.cginc"
struct vertexIn {
float4 pos : POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
};
struct vertexOut {
float4 pos : POSITION;
float4 color : COLOR0;
float3 normal : NORMAL;
};
struct geomOut {
float4 pos : POSITION;
float4 color : COLO0R;
float3 normal : NORMAL;
};
//Vertex shader: computes normal wrt camera
vertexOut vert (vertexIn i) {
vertexOut o;
o.pos = UnityObjectToClipPos(i.pos);
//o.pos = mul(unity_ObjectToWorld, i.pos);
o.color = i.color;
//o.normal = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, o.pos).xyz); //normal is towards the camera
o.normal = ObjSpaceViewDir(o.pos);
return o;
}
float _Radius;
//Geometry shaders: Creates an equilateral triangle with the original vertex in the orthocenter
[maxvertexcount(3)]
void geom(point vertexOut i[1], inout TriangleStream<geomOut> OutputStream)
{
geomOut o, p;
float3 perp = float3(i[0].normal.y - i[0].normal.x, i[0].normal.x, i[0].normal.x) * _Radius;
o.color = i[0].color;
o.normal = i[0].normal;
o.pos = i[0].pos;
o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
//o.pos = float4(normalize(perp + i[0].pos) * _Radius , 1); //Generic perpendicular to the normal
OutputStream.Append(o);
p.color = i[0].color;
p.normal = i[0].normal;
p.pos = float4(i[0].pos.x, i[0].pos.y * _Radius, i[0].pos.z, 1);
//p.pos = float4( (normalize(cross(i[0].pos.xyz, perp.xyz)) - perp.xyz / 2 + i[0].pos) * _Radius,1);
OutputStream.Append(p);
o.color = i[0].color;
o.normal = i[0].normal;
o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
//p.pos = float4(-1 * (normalize(cross(i[0].pos.xyz, perp.xyz)) * _Radius) /* - perp.xyz / 2 + i[0].pos*/, 1);
OutputStream.Append(p);
OutputStream.RestartStrip();
}
float4 frag(geomOut i) : COLOR
{
return i.color;
}
ENDCG
}
}
FallBack "Diffuse"
}
,I am working on a project for college in Unity that involves displaying a very big pointcloud. I have implemented the point cloud as a mesh with 1 vertex per point and no triangles. I want to implement a shader in Unity that creates a triangle for each vertex and colors it of the color of the vertex.
I have no experience with shaders and I have developed the code trying to follow multiple resources online.
At the current state the code simply makes the pointcloud disappear and nothing is displayed.
Thank you!
Shader "Custom/SpherePoint" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Radius ("Sphere Radius", float) = 0.01
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
#pragma target 4.0 // Use shader model 3.0 target, to get nicer looking lighting
#include "UnityCG.cginc"
struct vertexIn {
float4 pos : POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
};
struct vertexOut {
float4 pos : POSITION;
float4 color : COLOR0;
float3 normal : NORMAL;
};
struct geomOut {
float4 pos : POSITION;
float4 color : COLO0R;
float3 normal : NORMAL;
};
//Vertex shader: computes normal wrt camera
vertexOut vert (vertexIn i) {
vertexOut o;
o.pos = UnityObjectToClipPos(i.pos);
//o.pos = mul(unity_ObjectToWorld, i.pos);
o.color = i.color;
//o.normal = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, o.pos).xyz); //normal is towards the camera
o.normal = ObjSpaceViewDir(o.pos);
return o;
}
float _Radius;
//Geometry shaders: Creates an equilateral triangle with the original vertex in the orthocenter
[maxvertexcount(3)]
void geom(point vertexOut i[1], inout TriangleStream<geomOut> OutputStream)
{
geomOut o, p;
float3 perp = float3(i[0].normal.y - i[0].normal.x, i[0].normal.x, i[0].normal.x) * _Radius;
o.color = i[0].color;
o.normal = i[0].normal;
o.pos = i[0].pos;
o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
//o.pos = float4(normalize(perp + i[0].pos) * _Radius , 1); //Generic perpendicular to the normal
OutputStream.Append(o);
p.color = i[0].color;
p.normal = i[0].normal;
p.pos = float4(i[0].pos.x, i[0].pos.y * _Radius, i[0].pos.z, 1);
//p.pos = float4( (normalize(cross(i[0].pos.xyz, perp.xyz)) - perp.xyz / 2 + i[0].pos) * _Radius,1);
OutputStream.Append(p);
o.color = i[0].color;
o.normal = i[0].normal;
o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
//p.pos = float4(-1 * (normalize(cross(i[0].pos.xyz, perp.xyz)) * _Radius) /* - perp.xyz / 2 + i[0].pos*/, 1);
OutputStream.Append(p);
OutputStream.RestartStrip();
}
float4 frag(geomOut i) : COLOR
{
return i.color;
}
ENDCG
}
}
FallBack "Diffuse"
}
Hi, I'm also working on a real-time kinect point-cloud visualization for an academic project. I've tried both of the provided answers, but they don't render any point at all. Did you found a working point cloud shader? Would be very helpful! thanks!
– Culpable