由于需要面向视角方向。因此需要重新构建向上向量。1.先构建出View视角向量 然后和Up(0,1,0)向量叉积得到向右向量。这个时候向右向量和View向量是两两垂直的。3.再由View向量和向右向量叉积,得到重新计算后的向上向量Up1。4.这个时候由Up1 View Right三个正交基向量构建出旋转矩阵。
Shader "QShader/Billboard_ByAll"{
Properties{
_MainTex("Main Texture",2D) = "white"{}
}
SubShader{
Tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" "DisableBatching" = "True"}
pass {
Tags{"LightMode" = "Always"}
Cull Off
CGPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
#include "Lighting.cginc"
#include "UnityCG.cginc"
sampler2D _MainTex;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f Vertex(appdata_base v) {
v2f o;
float3 center = float3(0,0,0);
float3 viewer = mul(unity_WorldToObject,float4(_WorldSpaceCameraPos,1)).xyz;
float3 viewDir = viewer - center;
viewDir = normalize(viewDir);
float3 upDir = float3(0,1,0);
float3 rightDir = normalize(cross(upDir,viewDir));
upDir = normalize(cross(viewDir,rightDir));
float3 centerOffset = v.vertex.xyz - center;
float3x3 _RotateMatrix = transpose(float3x3(rightDir,upDir,viewDir));
float3 pos = mul(_RotateMatrix,centerOffset) + center;
o.pos = UnityObjectToClipPos(float4(pos,1));
o.uv = v.texcoord;
return o;
}
fixed4 Fragment(v2f i) :SV_TARGET{
return tex2D(_MainTex,i.uv);
}
ENDCG
}
}
}