How to add shadows to billboard shader help
Asked Answered
S

2

0

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

Sufflate answered 15/11, 2023 at 19:6 Comment(0)
S
0

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"
    }
Sufflate answered 27/8, 2013 at 5:2 Comment(1)

This shouldn't be necessary and might cause you some extra problems clip(color.a - _CutOff) ;

Izolaiztaccihuatl
I
0

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
    }
}
}
Izolaiztaccihuatl answered 26/8, 2013 at 21:3 Comment(2)

Thanks so much for your help Jona Marklund 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 [14794-image.jpg|14794]

Sufflate

its not showing the texture when i apply this shader my texture disappears suddenly dnt knw what is the problem

Sufflate
S
0

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"
    }
Sufflate answered 27/8, 2013 at 5:2 Comment(1)

This shouldn't be necessary and might cause you some extra problems clip(color.a - _CutOff) ;

Izolaiztaccihuatl

© 2022 - 2025 — McMap. All rights reserved.