Last active
December 11, 2018 17:43
-
-
Save samloeschen/1ae6156f3dfc324dc351456a79d93166 to your computer and use it in GitHub Desktop.
Example of billboarding in a shader
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 "Unlit/BillboardColor" { | |
Properties{ | |
_Color("_Color", Color) = (1,1,1,1) | |
} | |
SubShader{ | |
Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" } | |
Blend One OneMinusSrcAlpha | |
Pass{ | |
Name "BillboardColor" | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma fragmentoption ARB_precision_hint_fastest | |
#include "UnityCG.cginc" | |
uniform fixed4 _Color; | |
uniform float _Falloff; | |
struct vertex_input { | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct vertex_output { | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
vertex_output vert(vertex_input v) { | |
vertex_output output; | |
output.uv = v.uv; | |
// billboard | |
float3 vertex = v.vertex.xyz; | |
// get the translation of the object to world matrix | |
float4 world_trans = float4(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23, 1); | |
// multiply the view matrix by the world translation to get the view space position, and apply the scaled vertex's offset | |
float4 view_pos = mul(UNITY_MATRIX_V, world_trans) + float4(vertex, 0); | |
// multiply the view space position by the perspective matrix to get a perspective-correct position | |
float4 persp_pos = mul(UNITY_MATRIX_P, view_pos); | |
output.vertex = persp_pos; | |
return output; | |
} | |
fixed4 frag(vertex_output i) : COLOR{ | |
return _Color; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment