Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by kernjiro · Jun 05, 2016 at 06:55 PM · shadershaders2d-platformershader programming2.5d

Add transparency to sprite shader with cast shadows and receive shadows and lighting

Alright... I give up and I preface this with saying I know next to nothing about writing shaders. The following is a frankenstein conglomerate of some examples I found people post. I need a shader for my 2.5D game for sprites that casts and receives shadows, has ZWrite On, is affected by lighting, and allows transparency

Right now, it seems to do all but allow transparency.

Any help is appreciated!

     Shader "Sprites/Bumped Diffuse with Shadows"
     {
 Properties
 {
     [PerRendererData]  _MainTex ("Color (RGB) Alpha (A)", 2D) = "white"{}
     //_BumpMap ("Normalmap", 2D) = "bump" {}
     _Color ("Tint", Color) = (1,1,1,1)
     [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
             _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5

 }

 SubShader
 {
     Tags
     { 
         "Queue"="Transparent" 
         "IgnoreProjector"="True" 
         "RenderType"="Transparent" 
         "PreviewType"="Plane"
         "CanUseSpriteAtlas"="True"
         
     }
         LOD 300


     Cull Off
     Lighting On
     ZWrite On
     Fog { Mode Off }
     

     CGPROGRAM
     #pragma surface surf Lambert alpha vertex:vert addshadow alphatest:_Cutoff 
     #pragma multi_compile DUMMY PIXELSNAP_ON 

     sampler2D _MainTex;
     sampler2D _BumpMap;
     fixed4 _Color;

     struct Input
     {
         float2 uv_MainTex;
         float2 uv_BumpMap;
         fixed4 color;
     };
     
     void vert (inout appdata_full v, out Input o)
     {
         #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
         v.vertex = UnityPixelSnap (v.vertex);
         #endif
         v.normal = float3(0,0,-1);
         v.tangent =  float4(1, 0, 0, 1);
         
         UNITY_INITIALIZE_OUTPUT(Input, o);
         o.color = _Color;
     }

     void surf (Input IN, inout SurfaceOutput o)
     {
         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
         o.Albedo = c.rgb;
         o.Alpha = c.a;
         o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
     }
     ENDCG
 }

     Fallback "Transparent/Cutout/Diffuse"
     }
Comment
Add comment · Show 3
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 Namey5 · Jun 06, 2016 at 07:26 AM 0
Share

It depends what kind of transparency you need. If you don't require any areas to be semi-transparent, then using alphatesting / clipping will allow for this. Otherwise, there isn't that much we can do. The way transparency is drawn, receiving shadows, depth and the such aren't compatible. It is true that by using multiple passes you can receive shadows but this comes with its own issues. It does appear (from your fallback) that you want to do alphatesting. In which case, you should remove the "alpha" from the surface shader declaration. Also, you will want to remove the shader from the transparent queue, and put it in:

 Tags { "Queue"="AlphaTest" }

Along with the other tags.

avatar image kernjiro Namey5 · Jun 06, 2016 at 02:40 PM 0
Share

So it's pretty much impossible to have a semi-transparent diffuse shader with shadows? Explains why I've been having so much trouble finding it.

avatar image Racak94 · May 14, 2017 at 05:41 PM 0
Share

@kernjiro Did you find a solution for this problem?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Jun 06, 2016 at 04:46 PM

This is a hard problem to solve (and it's not specific to Unity). I'll try to explain my understanding of it:

The shadowing process which Unity follows is firstly to loop through each light and calculate the shadows it casts to create a shadow "buffer" in screenspace. Then, when objects that receive shadows are rendered, they sample from this buffer, again in screenspace. The assumption is therefore that any given pixel drawn to the screen corresponds to a unique position in the world, and this is what the shadow buffer uses to represent whether a point lies in shadow or not.

The problem is that, as soon as you introduce semi-transparent objects, this assumption no longer holds true: any given pixel when seen through a semi-transparent object is a composite of at least two objects at different depths, but the shadow buffer is unable to represent this. In the rendering pipeline, the problem is solved by transparent objects being sorted furthest to closest before rendering to make them appear correct, but the shadowmap processes lights in an arbitrary order - no sorting is done.

The reason why Transparent/Cutout shaders work is because they have no transparency, as such - they're opaque but with some discarded fragments.

It is possible to fix, but it's not on the current Unity roadmap, and the performance might be horrible. Aras has said that it certainly won't be coming any time in the next few months. See here for a full discussion.

Comment
Add comment · Show 6 · 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 kernjiro · Jun 06, 2016 at 04:53 PM 0
Share

there's an option for "shadows only" on a sprite. so theoretically, couldn't i duplicate every sprite, have one set up for shadows only, and then have one set up with the sprites-diffuse shader? sounds terribly inefficient

avatar image tanoshimi kernjiro · Jun 06, 2016 at 05:09 PM 0
Share

"there's an option for "shadows only" on a sprite" Is there? What version of Unity are you using? To my knowledge, that option is hidden for a sprite renderer (except for in Debug view) specifically because the standard sprite shader doesn't support shadows. If you use your own shader then you can of course enable this option via script or using debug mode, but you won't get shadows other than as a transparent cutout. Would love to be proved wrong if you have different results though!

avatar image kernjiro tanoshimi · Jun 06, 2016 at 05:26 PM 0
Share

yes i was talking about in debug. under "Cast Shadows" there's a "Shadows Only" option.

Show more comments
avatar image
0

Answer by Racak94 · May 14, 2017 at 07:34 PM

@kernjiro Did you find a solution for this problem?

Comment
Add comment · 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

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

64 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 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 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 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

Is it possible to avoid writing to certain g-buffers? 1 Answer

Shader issue with a 170 upper angle 0 Answers

UnityObjectToClipPos is inverted? 0 Answers

post shader: reconstruct worldspace plane coordinates 1 Answer

Shader Color 1 Answer


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