Billboard effect means that a normal direction of the two-dimensional plane is always the same line of sight (viewing direction of the camera). Widely used in rendering smoke, clouds, flash and so on.
Its essence is to construct a rotation matrix, then we can choose to build three basis vectors of this matrix.
Direction (X-axis) is directed →
Direction (Y-axis) is directed ↑
Plane normal direction (Z-axis)
It is generally calculated:
1. Determine the normal direction (the nature of the billboard effect) according to the current direction of the camera, and normalized
normal=normalize(viewDir);
2. Calculate the rightward direction and the normal direction according to the initial upward direction (in the local space is the float3 (0,1,0)) obtained and normalized
right = normalize (cross (up, normal)); (calculated using the vector cross product)
3. After calculating the right direction and the normal direction recalibrate the upward direction, the exact value of
up'=normalize(cross(right,normal));
Offset in the direction of the respective original vertices After calculating the rotation matrix
Script is as follows:
1 // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' 2 3 Shader "MyUnlit/Billboarding" 4 { 5 Properties 6 { 7 _MainTex ("Texture", 2D) = "white" {} 8 _Color("Color Tint",color)=(1,1,1,1) 9 } 10 SubShader 11 { 12 // vertices must be disabled engagement transform P 13 is Tags { " Queue " = " Transparent " " RenderType " = " Transparent " " IgnoreProjector " = " to true " " DisableBatching " = " True " } 14 15 Pass 16 { . 17 / / alpha blending 18 is Tags { " LightMode " = "forwardbase" } 19 ZWrite off 20 Blend SrcAlpha OneMinusSrcAlpha 21 Cull off 22 23 CGPROGRAM 24 #pragma vertex vert 25 #pragma fragment frag 26 #pragma multi_compile_fog 27 28 #include "UnityCG.cginc" 29 30 struct appdata 31 { 32 float4 vertex : POSITION; 33 float2 uv : TEXCOORD0; 34 is }; 35 36 struct V2f 37 [ { 38 is float2 UV: TEXCOORD0; 39 UNITY_FOG_COORDS ( . 1 ) 40 float4 Vertex: SV_POSITION; 41 is }; 42 is 43 is sampler2D _MainTex; 44 is float4 _MainTex_ST; 45 fixed4 _Color; 46 is 47 V2f Vert (AppData V) 48 { 49 V2f O; 50 // gaze direction calculating model space 51 MUL = objViewDir float3 (unity_WorldToObject, float4 (_WorldSpaceCameraPos, . 1 )); 52 is 53 is // respective base vector calculating rotation matrix 54 is float3 normalDir = the normalize (objViewDir); 55 float3 upDir float3 = ( 0 , . 1 , 0 ); 56 is float3 = rightDir the normalize (Cross (normalDir, upDir)); 57 is upDir = the normalize (Cross (normalDir, rightDir)); 58 59 // rotation matrix of offset vertices 60 * = rightDir v.vertex.x localPos float3 + + normalDir v.vertex.y upDir * * v.vertex.z; 61 is 62 is // value as a new offset after transfer calculations vertex 63 is o.vertex UnityObjectToClipPos = ( float4 (localPos, . 1 )); 64 o.uv = TRANSFORM_TEX (v.uv, _MainTex); 65 UNITY_TRANSFER_FOG (O, o.vertex); 66 return O; 67 } 68 69 fixed4 the frag (V2f I): SV_Target 70 { 71 is fixed4 COL = tex2D (_MainTex, i.uv); 72 col.rgb *= _Color.rgb; 73 UNITY_APPLY_FOG(i.fogCoord, col); 74 return col; 75 } 76 ENDCG 77 } 78 } 79 fallback "Transparent/VertexLit" 80 }