Guest User

Unity 2019 Billboard Shader

a guest
Aug 6th, 2019
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. Shader "Custom/Billboard"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex("Texture", 2D) = "white" {}
  6.     }
  7.         SubShader
  8.     {
  9.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True" }
  10.  
  11.         ZWrite Off
  12.         Blend SrcAlpha OneMinusSrcAlpha
  13.  
  14.         Pass
  15.         {
  16.             CGPROGRAM
  17.             #pragma vertex vert
  18.             #pragma fragment frag
  19.             // make fog work
  20.             #pragma multi_compile_fog
  21.  
  22.             #include "UnityCG.cginc"
  23.  
  24.             struct appdata
  25.             {
  26.                 float4 vertex : POSITION;
  27.                 float2 uv : TEXCOORD0;
  28.             };
  29.  
  30.             struct v2f
  31.             {
  32.                 float2 uv : TEXCOORD0;
  33.                 UNITY_FOG_COORDS(1)
  34.                 float4 pos : SV_POSITION;
  35.             };
  36.  
  37.             sampler2D _MainTex;
  38.             float4 _MainTex_ST;
  39.  
  40.             v2f vert(appdata v)
  41.             {
  42.                 v2f o;
  43.                 o.pos = UnityObjectToClipPos(v.vertex);
  44.                 o.uv = v.uv.xy;
  45.  
  46.                 // billboard mesh towards camera
  47.                 float3 vpos = mul((float3x3)unity_ObjectToWorld, v.vertex.xyz);
  48.                 float4 worldCoord = float4(unity_ObjectToWorld._m03, unity_ObjectToWorld._m13, unity_ObjectToWorld._m23, 1);
  49.                 float4 viewPos = mul(UNITY_MATRIX_V, worldCoord) + float4(vpos, 0);
  50.                 float4 outPos = mul(UNITY_MATRIX_P, viewPos);
  51.  
  52.                 o.pos = outPos;
  53.  
  54.                 UNITY_TRANSFER_FOG(o,o.vertex);
  55.                 return o;
  56.             }
  57.  
  58.             fixed4 frag(v2f i) : SV_Target
  59.             {
  60.                 // sample the texture
  61.                 fixed4 col = tex2D(_MainTex, i.uv);
  62.             // apply fog
  63.             UNITY_APPLY_FOG(i.fogCoord, col);
  64.             return col;
  65.         }
  66.         ENDCG
  67.     }
  68.     }
  69. }
Add Comment
Please, Sign In to add comment