Skip to content

Instantly share code, notes, and snippets.

@andrew-raphael-lukasik
Last active December 11, 2023 05:12
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andrew-raphael-lukasik/60ce48c016cc6fb03f501ad68e9681ed to your computer and use it in GitHub Desktop.
point cloud mesh to billboards shader example
using UnityEngine;
using Random = Unity.Mathematics.Random;
[RequireComponent( typeof(MeshFilter) , typeof(MeshRenderer) )]
public class GenerateRandomPointCloudMesh : MonoBehaviour
{
Mesh _mesh;
[SerializeField] int _numVerties = 100;
void Awake ()
{
_mesh = new Mesh();
var rnd = new Random( (uint) System.DateTime.Now.GetHashCode() );
Vector3[] vertices = new Vector3[_numVerties];
for( int i=0 ; i<_numVerties ; i++ )
vertices[i] = rnd.NextFloat3( -Vector3.one , Vector3.one );
_mesh.vertices = vertices;
int[] indices = new int[ _mesh.vertices.Length ];
for( int i=0 ; i<indices.Length ; i++ )
indices[i] = i;
var mf = GetComponent<MeshFilter>();
_mesh.SetVertices( vertices );
_mesh.SetIndices( indices , MeshTopology.Points , 0 );
mf.sharedMesh = _mesh;
}
void OnDestroy ()
{
if( _mesh!=null ) Destroy(_mesh);
}
}
Shader "example/point cloud mesh to billboards" {
Properties
{
_Size ("Width", float) = 1.0
}
SubShader
{
LOD 200
// Tags { "RenderType"="Opaque" }
Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma geometry geom
// #pragma target 4.0
#include "UnityCG.cginc"
struct vertexIn {
float4 pos : POSITION;
float4 color : COLOR;
};
struct vertexOut {
float4 pos : SV_POSITION;
float4 color : COLOR0;
};
struct geomOut {
float4 pos : POSITION;
float4 color : COLOR0;
};
float _Size;
vertexOut vert ( vertexIn i )
{
vertexOut o;
o.pos = UnityObjectToClipPos(i.pos);
o.color = i.color;
return o;
}
[maxvertexcount(4)]
void geom ( point vertexOut IN[1] , inout TriangleStream<geomOut> triStream )
{
vertexOut vertex = IN[0];
const float2 points[4] = { float2(1,-1) , float2(1,1) , float2(-1,-1) , float2(-1,1) };
float2 pmul = float2( _Size*(_ScreenParams.y / _ScreenParams.x) , _Size ) * 0.5;
geomOut newVertex;
newVertex.color = vertex.color;
for( int i=0 ; i<4 ; i++ )
{
newVertex.pos = vertex.pos + float4( points[i]*pmul , 0 , 0 );
triStream.Append( newVertex );
}
}
float4 frag ( geomOut i ) : COLOR
{
return i.color;
}
ENDCG
}
}
FallBack Off
}
@j-polden
Copy link

Hi there. Just wondering how you implement the point cloud mesh to billboards.shader into the randomly generated pointcloud? I am trying to alter the size of the rendered points and having a bit of difficulty. Thanks :)

@andrew-raphael-lukasik
Copy link
Author

andrew-raphael-lukasik commented Feb 1, 2023

Hi @j-polden !

  1. Assign material to your Mesh Renderer component
  2. Hit Play to generate mesh
  3. Adjust Width property will change size of these quads

Screenshot 2023-02-01 233932

Side note: this shader works with most meshes

Screenshot 2023-02-01 233629

@j-polden
Copy link

j-polden commented Feb 2, 2023

@andrew-raphael-lukasik Thanks for that. It worked quite easily in the end. I am learning a lot about shaders at the moment. Tricky stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment