Created
June 2, 2022 00:11
-
-
Save camnewnham/290fd777d7b04775f7ad98bbd3ced40f to your computer and use it in GitHub Desktop.
Point cloud shaders
This file contains hidden or 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 "Points/Billboard" | |
{ | |
Properties | |
{ | |
_PointSize("PointSize", Range(0, 0.1)) = 0.01 | |
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Source Blend", Float) = 1 // "One" | |
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Destination Blend", Float) = 0 // "Zero" | |
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Depth Test", Float) = 4 // "LessEqual" | |
[Enum(DepthWrite)] _ZWrite("Depth Write", Float) = 1 // "On" | |
} | |
SubShader | |
{ | |
Pass | |
{ | |
Blend[_SrcBlend][_DstBlend] | |
ZTest[_ZTest] | |
ZWrite[_ZWrite] | |
CGPROGRAM | |
#pragma target 5.0 | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma geometry geom | |
#pragma multi_compile_instancing | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float4 color: COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2g | |
{ | |
float4 pos : POSITION; | |
float4 col : COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct g2f | |
{ | |
float4 pos : SV_POSITION; | |
float4 col : COLOR; | |
UNITY_VERTEX_OUTPUT_STEREO | |
}; | |
fixed _PointSize; | |
v2g vert(appdata v) | |
{ | |
v2g o; | |
UNITY_SETUP_INSTANCE_ID(v); | |
UNITY_TRANSFER_INSTANCE_ID(v, o); | |
o.pos = mul(unity_ObjectToWorld, v.vertex); | |
o.col = v.color; | |
return o; | |
} | |
[maxvertexcount(4)] | |
void geom(point v2g p[1], inout TriangleStream<g2f> triStream) | |
{ | |
g2f pIn; | |
UNITY_SETUP_INSTANCE_ID(p[0]); | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(pIn); | |
float3 up = float3(0, 1, 0); | |
float3 look = _WorldSpaceCameraPos - p[0].pos; | |
float dist = length(look); | |
look.y = 0; | |
look = normalize(look); | |
float3 right = cross(up, look); | |
float halfS = 0.5f * _PointSize * dist; | |
float4 v[4]; | |
v[0] = float4(p[0].pos + halfS * right - halfS * up, 1.0f); | |
v[1] = float4(p[0].pos + halfS * right + halfS * up, 1.0f); | |
v[3] = float4(p[0].pos - halfS * right + halfS * up, 1.0f); | |
v[2] = float4(p[0].pos - halfS * right - halfS * up, 1.0f); | |
pIn.pos = UnityObjectToClipPos(v[0]); | |
pIn.col = p[0].col; | |
triStream.Append(pIn); | |
pIn.pos = UnityObjectToClipPos(v[1]); | |
pIn.col = p[0].col; | |
triStream.Append(pIn); | |
pIn.pos = UnityObjectToClipPos(v[2]); | |
pIn.col = p[0].col; | |
triStream.Append(pIn); | |
pIn.pos = UnityObjectToClipPos(v[3]); | |
pIn.col = p[0].col; | |
triStream.Append(pIn); | |
} | |
float4 frag(g2f input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
} |
This file contains hidden or 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 "Points/Vertex" | |
{ | |
Properties | |
{ | |
_PointSize("PointSize", Range(0, 0.1)) = 0.01 | |
[Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Source Blend", Float) = 1 // "One" | |
[Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Destination Blend", Float) = 0 // "Zero" | |
[Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Depth Test", Float) = 4 // "LessEqual" | |
[Enum(DepthWrite)] _ZWrite("Depth Write", Float) = 1 // "On" | |
} | |
SubShader | |
{ | |
Pass | |
{ | |
Blend[_SrcBlend][_DstBlend] | |
ZTest[_ZTest] | |
ZWrite[_ZWrite] | |
CGPROGRAM | |
#pragma target 5.0 | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma multi_compile_instancing | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float4 color: COLOR; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2f | |
{ | |
float4 pos : POSITION; | |
float4 col : COLOR; | |
UNITY_VERTEX_OUTPUT_STEREO | |
}; | |
v2f vert(appdata v) | |
{ | |
v2f o; | |
UNITY_SETUP_INSTANCE_ID(v); | |
UNITY_INITIALIZE_OUTPUT(v2f, o); | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); | |
o.pos = UnityObjectToClipPos(v.vertex); | |
o.col = v.color; | |
return o; | |
} | |
float4 frag(v2f input) : COLOR | |
{ | |
return input.col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment