Last active
December 11, 2023 05:12
Star
You must be signed in to star a gist
point cloud mesh to billboards shader example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Hi @j-polden !
- Assign material to your
Mesh Renderer
component - Hit Play to generate mesh
- Adjust
Width
property will change size of these quads
Side note: this shader works with most meshes
@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
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 :)