Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AR_Rizvi · Aug 26, 2013 at 10:25 AM · shadershadowtreesbillboards

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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"
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jona-Marklund · Aug 27, 2013 at 01:02 PM 0
Share

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

avatar image
0

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
     }
 }
 }
 
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AR_Rizvi · Aug 27, 2013 at 04:47 AM 0
Share

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 alt text

image.jpg (16.7 kB)
avatar image Jona-Marklund · Aug 27, 2013 at 01:00 PM 0
Share

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
     }
 }
 }
 
avatar image AR_Rizvi · Aug 27, 2013 at 02:07 PM 0
Share

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

avatar image Jona-Marklund · Aug 27, 2013 at 03:00 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Surface Reflection Shader with Shadows 0 Answers

Only standard shader objects cast shadows in build. 0 Answers

Help with making a water shader receive light 1 Answer

How to make billboard grass drop shadows in Unity 5? 0 Answers

How to measure the area of shadow on a GameObject? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges