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