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 TowerOfBricks · Dec 27, 2013 at 09:39 PM · shadershadowalphacgsurfaceshader

Shader with zwrite, shadows and alpha (special alpha)

Hi

I know the answer to "can I make a shader with shadows, zwrite and alpha", that is tricky, if not impossible.

However I do not want exactly that. What I am working on is a tile based system with textures that transition into each other using transition alphas. My idea is that normally, a tile will only have a single texture, but sometimes, it needs several, when there is a transition, so when there is a transition, I was thinking that I should be able to just duplicate the tile mesh so that there are two meshes occupying the exact same space, but they have different materials. The lowest prioritized texture/material one will be rendered before the second one by specifying the render queue, the lowest prioritized mesh should simply show the texture. The second material/texture should be be alpha-blended with anything rendered before it (e.g the lowest prioritized texture/material).

Note that this does not rely in any way on that the transparent objects are rendered in back to front order. What I am asking for essentially is a diffuse shader which just blends in another way with the previously rendered pixels. Preferably it should be doable in deferred rendering, I notice that when I add "alpha" to the "#pragma surface" line, it refuses to use the deferred rendering path.

The below shader blends correctly, but does not have any shadows and does not have ZWrite enabled.

I have tried editing the compiled shaders, enabling ZWrite and changing blending modes and similar, but nothing have worked so far. I can get some features, but if I can get, for example correct blending and ZWrite, then shadows are suddenly not working.

 Shader "TerrainTransition"
 {
     properties
     {    
         _MainTex ("Base Texture", 2D) = "white"{}
         _SplatControl ("Splat Map", 2D) = "white"{}
         _Emission ("Emission", Float) = 0.1
     }
     
     subshader
     {
         Tags { "RenderType" = "Opaque" }
 
         ZTest LEqual
         ZWrite On
         
         CGPROGRAM
         //#pragma surface surf Lambert vertex:vert alpha
         #pragma surface surf Lambert alpha addshadow 
         #pragma target 3.0
         #pragma exclude_renderers flash
         #pragma exclude_renderers d3d11
         
         sampler2D _MainTex;
         sampler2D _SplatControl;
         fixed _Emission;
         
         struct Input
         {
             half2 uv_MainTex;
             half2 uv2_SplatControl;
         };
                     
         void surf (Input IN, inout SurfaceOutput o)
         {
             fixed4 t0 = tex2D ( _MainTex, IN.uv_MainTex );
             fixed4 splat_control = tex2D ( _SplatControl, IN.uv2_SplatControl );
             
             o.Albedo = t0.rgb;  // + splat_control.rgb*0.1;
             o.Alpha = splat_control.r;
             o.Emission = t0.rgb * _Emission;
         }
         
         ENDCG
     }
     Fallback "Diffuse"
 }

The below image shows how the current blending looks. Two planes with different materials are blended together. The grass material has a render queue set so that it is rendered after the dirt material. The dirt plane is completely opaque (but using the same shader as the grass material).

how the current blending looks

Notes:

  • Z sorting is not needed ( handled using render queues since I know the correct ordering at creation time )

  • Shadows are required, but they can completely ignore the alpha ( I don't think there is a way for them to respect it anyway, but it doesn't matter ).

  • ZWrite is required

  • A cutout shader is not sufficient because they have hard edges ( I need smooth blending )

  • I am not totally sure on when unity decides that a shader cannot run in deferred mode, but it would be nice if the shader could run in deferred mode.

Thanks in advance!

  • Aron Granberg

screen shot 2013-12-27 at 22.29.11.png (114.8 kB)
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
1
Best Answer Wiki

Answer by hausmaus · Feb 03, 2014 at 05:19 PM

Hi Aron,

First off, thanks for your great work on various plugins :)

As for the shader you're asking about, if you haven't found a solution yet, I wanted to post some information:

Deferred rendering doesn't support full alpha blending, as per http://docs.unity3d.com/Documentation/Components/RenderTech-DeferredLighting.html. You can verify this by adding "#pragma debug" to your shader and then clicking "Open Compiled Shader" in the Inspector panel with the shader selected, and you will see that the generated code only includes Forward lighting paths.

You may be able to simulate blending with a grab pass in the shader and doing blending that way, similar to the way refracted water is done.

Alternatively, since what you're describing is basically a terrain shader with a large number possible maps which can be in arbitrary order, you may be able to simply keep a pool of say 256 4-layer terrain materials on hand and configure them as needed depending on the tiles visible on the screen at that time. If the maps just tile in worldspace, this is a very easy solution, although it can run into problems with zooming out if you have a lot of variations or are otherwise unable to merge tiles with similar materials.

I have had success with the second method for a very large tile-based terrain system, but it didn't need a huge draw distance or any kind of crazy zoom out support.

Hope this helps, Adrian

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 TowerOfBricks · Apr 15, 2014 at 04:58 PM 0
Share

Accepted because this is actually the solution I have ended up using. It does have its problems as you say, but it works pretty good.

PS: Glad to hear my plugins are appreciated!

avatar image
0

Answer by TowerOfBricks · Apr 15, 2014 at 04:56 PM

I actually found a solution for this. Looking through the unity documentation, I find the little mentioned decal:blend option to be used with surface shaders. Applying that will give me exactly the result I want. The shader will use the shadow information from whatever object is behind it. The only drawback is that it does not write to the z-buffer, even where it is fully opaque, but that isn't really a large problem.

 #pragma surface surf Lambert vertex:vert decal:blend
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

19 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

Related Questions

Shader Help: "Cannot implicitly convert 'float3' to 'float4'" 2 Answers

Fade-In/Out shadows for a specific object 0 Answers

Semi-transparent shader with Shadows 3 Answers

Order for Surface Shader / CG frag passes? 1 Answer

Getting the world position of the pixel in the fragment shader. 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