Unityshader example 08: Billboard (Billboard) material

Source: Internet
Author: User
Tags mul

Billboard (Billboard) material


Billboard Overview

Billboard technology plays an important role in the game engine, generally used in the particle effect or halo effect, so that the particle patch Z axis toward the camera. Unity is no exception, in the unity particle system of the render panel in the render mode in the four-billboard mode is optional, these modes are facing the camera, but the axial constraints are different In addition, Unity's terrain system also has the use of Billboard technology, but if we use our own model or preset to make it face the camera, we have to write the script or write the corresponding shader implementation, Here will take shadowgun inside a well-written shader (madfinger/transparent/blinking godrays billboarded) as an example to achieve billboard functions (such as:), In the original example, there were some effects unrelated to billboard (such as flashing, fading according to distance, etc.) and I took it off, only the implementation of Billboard.



The preparation of the model in 3d software

In this shader, in order to obtain the center of the patch, some data exist in the vertex color and UV2 inside, in the vertex color R channel and the G-Channel inside a unit patch (1 width of the patch) coordinates, that is, four vertices of the vertex color (0,0,0), (255,0,0), (255, 255,0), (0,255,0), while in UV2 the length and width of the patch (6,8), it is necessary to ensure that the unit is meters, easy to match unity units; As shown in this example, the vertex color of the four vertices is as shown, the length width is 6 and 8, and four vertices are UV2 as indicated, Record the length and width of units 6 and 8.



Shader Implementation

This shader is a billboard effect that can constrain the y-axis, so in the properties we need to define a parameter to control the switch of the axial constraint:


_verticalbillboarding ("Vertical Restraints", Range (0,1)) = 1


Next, the main work is to modify the vertex of the patch model in the Vert function, to calculate the vertex position, first we get the vector of the center of the patch to the camera position, and this time we have the model vertex color and uv2 data can work, the following calculations are calculated under object space.

Float3centeroffs  = FLOAT3 (float (0.5). xx-v.color.rg,0) * v.texcoord1.xyy;//use pre-existing model vertex color and UV2 data to get the offset of each vertex relative to the center// Float3centeroffs  = FLOAT3 (float (0.5). xx-v.color.rg,0) * v.color.bbb*255;float3centerlocal = v.vertex.xyz + centeroffs.xyz;//offsets the vertex coordinates to the central position float3viewerlocal = Mul (_WORLD2OBJECT,FLOAT4 (_worldspacecamerapos,1));// Obtain the coordinates of the observer point under object space Float3localdir    = viewerlocal-centerlocal;//obtains the direction vector of the patch center to the Observer point Localdir.y =LOCALDIR.Y * _ verticalbillboarding;//if the _verticalbillboarding is 0, the patch rotates around the y-axis

then the resulting direction vector is regularization, and the direction vector is used as the z-axis, and the right-hand direction vector rightlocal and the upward-direction vector are reconstructed from the center facing angle of the film uplocal;

float3rightlocal;float3uplocal; Calcorthonormalbasis (Normalize (Localdir), rightlocal,uplocal), the regularization of//localdir is very important!

The Calcorthonormalbasis (float3 dir,out float3 right,out float3 up) is used here to reconstruct the right-hand and up-up directions according to Dir, which is defined by itself and is prototyped as follows:

void Calcorthonormalbasis (FLOAT3 dir,out float3 right,out float3 up) {Up    = ABS (DIR.Y) > 0.999f? FLOAT3 (0,0,1): Flo AT3 (0,1,0), right = normalize (cross (Up,dir)), Up    = Cross (dir,right);}

From the above prototype it can be seen that dir,right,up is a vector perpendicular to each other, thus constructing a coordinate system, where our input is normalized to the localdir vector, so we get a localdir as the z axis under object space,rightlocal do the x-axis, uplocal do a new coordinate system for the y-axis, and localdir the line of sight, so the plane of rightlocal and uplocal is perpendicular to the line of sight. In the end, we will offset the vertex coordinates of the center of the patch by the two unit vectors and centerlocal the offset centeroffs back to form a new plane perpendicular to the line of sight;

Float3bblocalpos = centerlocal-(rightlocal * centeroffs.x + uplocal * centeroffs.y);

Of course, this new plane is under the object space, but also need to switch to the screen projection space;

O.pos   = Mul (UNITY_MATRIX_MVP, FLOAT4 (bblocalpos,1)),//w component set to 1 indicates that FLOAT4 (bblocalpos,1) is a vertex


VF Version Code 01

Shader "PENGLU/BILLBOARD/UNLITADDVF" {Properties {_maintex ("Base texture", 2D) = "White" {}_verticalbillboarding (" Vertical Restraints ", Range (0,1)) = 1}subshader {Tags {" Queue "=" Transparent "" ignoreprojector "=" True "" Rendertype "=" Transparent "}blend one onecull off Lighting off Zwrite off Fog {Color (0,0,0,0)}lod 100cginclude#include" Unitycg.cginc "Sampler2d _maintex;float _verticalbillboarding;struct v2f {float4pos:sv_position;float2uv:texcoord0;}; void Calcorthonormalbasis (FLOAT3 dir,out float3 right,out float3 up) {up = ABS (DIR.Y) > 0.999f? FLOAT3 (0,0,1): Floa T3 (0,1,0), right = normalize (cross (Up,dir)), up = Cross (dir,right);} v2f Vert (Appdata_full v) {v2f o;float3centeroffs = FLOAT3 (float (0.5). xx-v.color.rg,0) * V.texcoord1.xyy;//float3center Offs = FLOAT3 (float (0.5). xx-v.color.rg,0) * v.color.bbb*256;float3centerlocal = v.vertex.xyz + Centeroffs.xyz;float3vi ewerlocal = Mul (_WORLD2OBJECT,FLOAT4 (_worldspacecamerapos,1)); float3localdir = viewerlocal-centerlocal;Localdir.y =localdir.y * _verticalbillboarding;float3rightlocal;float3uplocal; Calcorthonormalbasis (Normalize (Localdir), rightlocal,uplocal); float3bbnormal = rightlocal * v.normal.x + upLocal * v.no    Rmal.y;float3bblocalpos = centerlocal-(rightlocal * centeroffs.x + uplocal * centeroffs.y); o.uv = V.texcoord.xy;o.pos = Mul (UNITY_MATRIX_MVP, FLOAT4 (bblocalpos,1)); return o;} Endcgpass {cgprogram#pragma vertex vert#pragma fragment Frag#pragma fragmentoption arb_precision_hint_fastestfixed4 Frag (v2f i): Color{return tex2d (_maintex, i.uv.xy);} ENDCG}}}

VF Version Code 01 effect:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Unityshader example 08: Billboard (Billboard) material

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.