- Home /
how to add shadows to billboard shader help....
hi guys...
i have came across a billboard shader and i added a alpha cutout in it to make it work as a tree billboard shader but now i want to add some shadows in it as i want my trees to cast shadows so i tried some tags but no use so guys plz help me in adding shadows in it
code given blow
Shader "MyShader/billboards" {
Properties {
_MainTex ("Texture Image", 2D) = "white" {}
_CutOff("Cut off", float) = 0.1
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// User-specified uniforms
uniform sampler2D _MainTex;
uniform float _CutOff;
struct vertexInput {
float4 vertex : POSITION;
float4 tex : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_P,
mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
- float4(input.vertex.x, input.vertex.z, 0.0, 0.0));
output.tex = -input.tex;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
float4 color = tex2D(_MainTex, float2(input.tex.xy));
if(color.a < _CutOff) discard;
return color;
}
ENDCG
}
}
}
plz help i am realy confused with shadows i am able to cast some shadows by adding
FallBack "Transparent/Cutout/Diffuse" but it dosse not cast a cutout shadow for example i am using a planer for my trees so the shader is casting the shadow of a planer instead of cutout
Plz need help Thanks AR
Answer by AR_Rizvi · Aug 27, 2013 at 05:02 AM
i added ur suggetions to the shader its working fine but with the slight positioning problem as i comented above take a look at the shAder may b i ave mistaken something and i want it to stop facing camera when camera gose up like on y axis plz take a look Thanks AR
//Modified by AR_RIZVI
Shader "MyShader/billboards123" {
Properties {
_MainTex ("Texture Image", 2D) = "white" {}
_CutOff ("cutout size",Range(0,1))=0.1
}
SubShader {
AlphaTest Greater [_CutOff]
Pass
{
Tags { "LightMode" = "ShadowCaster" }
SetTexture [_MainTex]
}
Pass {
Tags { "Queue" = "AlphaTest" "RenderType"="Transparentcutout" }
cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// User-specified uniforms
uniform sampler2D _MainTex;
uniform float _CutOff;
struct vertexInput {
float4 vertex : POSITION;
float4 tex : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_P,
mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
- float4(input.vertex.x, input.vertex.z, 0.0, 0.0));
output.tex = -input.tex;
return output;
}
float4 frag(vertexOutput input) : COLOR
{
float4 color = tex2D(_MainTex, float2(input.tex.xy));
clip(color.a - _CutOff) ;
return color;
}
ENDCG
}
// UsePass "VertexLit/SHADOWCOLLECTOR"
// UsePass "VertexLit/SHADOWCASTER"
}
Fallback "Transparent/Cutout/Diffuse"
}
This shouldn't be necessary and might cause you some extra problems clip(color.a - _CutOff) ;
Answer by Jona-Marklund · Aug 26, 2013 at 09:03 PM
Hello, this might be what you're looking for, hope it helps!
//Please note that this shader is made to work with the Unity3D Cube object
//Shadows will be cast depending upon the Transform of the cube object
//Best Regards Jona Marklund
Shader "JM/BillboardShadowCast"
{
Properties
{
_Cutoff ("Cutoff", Range(0.0,1.0)) = 0.5
_MainTex ("Particle Texture", 2D) = "white"
}
SubShader
{
AlphaTest Greater [_Cutoff]
Pass
{
Tags { "LightMode" = "ShadowCaster" }
SetTexture [_MainTex]
}
Pass
{
Tags { "Queue" = "AlphaTest" "RenderType"="Transparentcutout" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 position : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
uniform sampler2D _MainTex;
v2f vert(appdata IN)
{
v2f OUT;
//Simple Version (Normal Billboard)
//Based upon : http://en.wikibooks.org/wiki/Cg_Programming/Unity/Billboards
// /*
OUT.position = mul(UNITY_MATRIX_P,
mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
- float4(IN.position.x, IN.position.z, 0.0, 0.0));
OUT.texcoord = IN.texcoord;
// */
//Complex Version (Rotates based upon Transform)
//Based upon Lulucifer's code
/*
float4 modelView = mul(UNITY_MATRIX_MV,float4(0,0,0,1));
float4 INposition = IN.position;
float2 r1 = float2(_Object2World[0][0],_Object2World[0][2]);
float2 r2 = float2(_Object2World[2][0],_Object2World[2][2]);
float2 INposition0 = INposition.x*r1;
INposition0 += INposition.z*r2;
INposition.xy = INposition0;
INposition.z = 0.0;
INposition.xyz += modelView.xyz;
OUT.position = mul(UNITY_MATRIX_P, INposition);
OUT.texcoord = IN.texcoord;
*/
return OUT;
}
fixed4 frag(v2f IN) : COLOR
{
float4 color = tex2D(_MainTex, float2(IN.texcoord.xy));
return color;
}
ENDCG
}
}
}
Thanks so much for your help Jona $$anonymous$$arklund its working fine but a slight problem it that the shadow is not at its proper place like the texture is leaving the planer's position but the shadow is rendering from the planes position
pardon me for my english here is a screen shot of what i am sayng hope its understandable thanks for looking in to the matter
Hello AR, you could try this, it allows for the mesh you have the shader attached to, to be scaled and rotated and change to look of the sprite that way.
Shader "J$$anonymous$$/BillboardShadowCast"
{
//Please note that this shader is made to work with the Unity3D Cube object
//Shadows will be cast depending upon the Transform of the cube object
//By Jona $$anonymous$$arklund 27th of August 2013
Properties
{
_Cutoff ("Cutoff", Range(0.0,1.0)) = 0.5
_$$anonymous$$ainTex ("Particle Texture", 2D) = "white"
}
SubShader
{
AlphaTest Greater [_Cutoff]
Pass
{
Tags { "Light$$anonymous$$ode" = "ShadowCaster" }
SetTexture [_$$anonymous$$ainTex]
}
Pass
{
Tags { "Queue" = "AlphaTest" "RenderType"="Transparentcutout" }
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 position : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
uniform sampler2D _$$anonymous$$ainTex;
v2f vert(appdata IN)
{
v2f OUT;
//Simple Version (Normal Billboard)
//Based upon : http://en.wikibooks.org/wiki/Cg_Program$$anonymous$$g/Unity/Billboards
/*
OUT.position = mul(UNITY_$$anonymous$$ATRIX_P,
mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$V, float4(0.0, 0.0, 0.0, 1.0))
- float4(IN.position.x, IN.position.z, 0.0, 0.0));
OUT.texcoord = IN.texcoord;
*/
//Complex Version (Rotates based upon Transform)
//Based upon Lulucifer's code
///*
float4 modelView = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$V,float4(0,0,0,1));
float4 INposition = IN.position;
float2 r1 = float2(_Object2World[0][0],_Object2World[0][2]);
float2 r2 = float2(_Object2World[2][0],_Object2World[2][2]);
float2 INposition0 = INposition.x*r1;
INposition0 += INposition.z*r2;
INposition.xy = INposition0;
INposition.z = 0.0;
INposition.xyz += modelView.xyz;
OUT.position = mul(UNITY_$$anonymous$$ATRIX_P, INposition);
OUT.texcoord = -IN.texcoord;
//*/
return OUT;
}
fixed4 frag(v2f IN) : COLOR
{
float4 color = tex2D(_$$anonymous$$ainTex, float2(IN.texcoord.xy));
return color;
}
ENDCG
}
}
}
its not showing the texture when i apply this shader my texture disappears suddenly dnt knw what is the problem
It's made to work with the Unity Cube object.
You may want to consider if a billboard shader is the right choice for your trees as well.
Using this simpler _cutoff / shadow shader
Shader "J$$anonymous$$/CutoffShadowCast"
{
//By Jona $$anonymous$$arklund 27th of August 2013
Properties
{
_Cutoff ("Cutoff", Range(0.0,1.0)) = 0.5
_$$anonymous$$ainTex ("Particle Texture", 2D) = "white"
}
SubShader
{
AlphaTest Greater [_Cutoff]
Pass
{
Tags { "Light$$anonymous$$ode" = "ShadowCaster" }
SetTexture [_$$anonymous$$ainTex]
}
Pass
{
Tags { "Queue" = "AlphaTest" "RenderType"="Transparentcutout" }
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
struct appdata
{
float4 position : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float4 texcoord : TEXCOORD0;
};
uniform sampler2D _$$anonymous$$ainTex;
v2f vert(appdata IN)
{
v2f OUT;
OUT.texcoord = IN.texcoord;
OUT.position = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, IN.position);
return OUT;
}
fixed4 frag(v2f IN) : COLOR
{
float4 color = tex2D(_$$anonymous$$ainTex, float2(IN.texcoord.xy));
return color;
}
ENDCG
}
}
}
along with a script such as.
using UnityEngine;
using System.Collections;
//Put this script on your tree objects
public class ForestFacing : $$anonymous$$onoBehaviour {
public Transform target; //The target of the rotation (An example would be your camera)
public int rotSpeed = 1; //How quickly the trees rotate
private Transform myTransform; //Current object's transform
void Awake()
{
myTransform = transform;
}
void Update ()
{
//Look at Player on the X and Z axis
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(new Vector3(target.position.x, 0, target.position.z) - new Vector3(myTransform.position.x, 0, myTransform.position.z)), rotSpeed * Time.deltaTime);
}
}
This one.