Leaf card billboards - Camera Facing Matrix

Hello,

I have tree model which has leaf cards as billboards. I’m trying write shader for it. I need billboarding for all axes so not only vertical billboarding. Is there some way how can I compute camera facing matrix in shader program?

Also I do not know how can I write shadows for it. With my code shadows are computed for my camera direction, not for light direction. Is there some way how can I render shadows faced with light?

Vertices are in center position of each leaf card. In colors member I have stored x and y offsets of corner for computing final vertex position (vertex center position + billboard rotation + corner offset).

There is my surface shader for vertical billboarding:

Shader "MyTests/LeafCard" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
	_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
	_BumpMap ("Normalmap", 2D) = "bump" {}
	_GlossMap ("Gloss (A)", 2D) = "black" {}
	_TranslucencyMap ("Translucency (A)", 2D) = "white" {}
	_ShadowOffset ("Shadow Offset (A)", 2D) = "black" {}
	
	// These are here only to provide default values
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.3
	_Scale ("Scale", Vector) = (1,1,1,1)
	_SquashAmount ("Squash", Float) = 1
}

SubShader { 
	Tags { "IgnoreProjector"="True" "RenderType"="TreeLeaf" }
	LOD 200
	Cull Off
		
CGPROGRAM
#pragma surface surf TreeLeaf alphatest:_Cutoff vertex:LeafCardBB addshadow nolightmap
#pragma exclude_renderers flash
#pragma glsl_no_auto_normalization
#include "Tree.cginc"
#include "MyUtility.cginc"

sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _GlossMap;
sampler2D _TranslucencyMap;
half _Shininess;

struct Input {
	float2 uv_MainTex;
	fixed4 color : COLOR; // color.a = AO
};

void LeafCardBB (inout appdata_full v){
	float3 center = v.vertex;
	float3 eyeVector = ObjSpaceViewDir(v.vertex);
	
	const float3 upVector = float3(0,1,0);
    float3 sideVector = normalize(cross(eyeVector,upVector));   

    float3 pos = center;
	float2 corner = DecodeVectorFromColor(v.color).xy;

    pos += corner.x * sideVector;
    pos += corner.y * upVector;

    v.vertex.xyz = pos;
}

void surf (Input IN, inout LeafSurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
	o.Albedo = c.rgb * _Color.rgb;
	o.Translucency = tex2D(_TranslucencyMap, IN.uv_MainTex).rgb;
	o.Gloss = UNITY_SAMPLE_1CHANNEL(_GlossMap, IN.uv_MainTex);
	o.Alpha = c.a;
	o.Specular = _Shininess;
	o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
}
ENDCG
}

Dependency "OptimizedShader" = "Hidden/Nature/Tree Creator Leaves Optimized"
FallBack "Diffuse"
}

Thank you
Jiri

Hi All,

it looks that I found solution how to do billboard faced with all axis. Here is the code.

void LeafCardBB (inout appdata_full v) {
	float3 center = v.vertex;
	float3 eyeVector = ObjSpaceViewDir(v.vertex);
	
	float3 upVector = mul(UNITY_MATRIX_T_MV, float4(1,1,0,1));
    float3 sideVector = normalize(cross(eyeVector,upVector));   

    float3 pos = center;
	float3 corner = float3(DecodeVectorFromColor(v.color).xy, 0.0f);

    pos += corner.x * sideVector;
    pos += corner.y * upVector;

    v.vertex.xyz = pos;
}

So now I need find solution how can I render shadows correctly. It means faced with light (standard global directional light) and not with player camera.
Could you help me somebody?

Thanks

Is this a lot faster than using Vector3.Lookat() in Update? It would work just the same as billboarding in Update?

I do not know that is it a lot of faster. I started with shaders 2 weeks ago. But I have to do it in shader program, because my tree model has leaves created as 1 mesh. This mesh contains all tree leaves billboards. So I have to setup billboarding for each vertex in mesh model. I cannot do it in Update function. It can be done if you have 1 billboard model for game object if I know.

Definitely. And, it’s better separation of concerns. If billboarding is only done for rendering, then it belongs in a shader. Only work outside of shaders when you need to manipulate data that matters for something other than display.

Wow, thanks a lot…! This along with figuring out a better way to do transparency ought to really help! :slight_smile:

How’s the progress with shadowing?