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
0
Question by Gabahulk · Nov 06, 2015 at 01:45 PM · shader2d gamealphashader programmingalpha-blend

Problem with alpha blending on shader Unity 5

Hi!

I am trying to create a shader to use as fog of war, but when the alpha is changed nothing happens, here is the code to the shader :

 Shader "Custom/FoWMask" {
     Properties {
         _Color ("Main Color", Color) = (1,1,1,1)
         _MainTex("Color (RGB)", 2D) = "white"
     }
         SubShader{
         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" "LightMode" = "ForwardBase" }
 
             Blend SrcAlpha OneMinusSrcAlpha
             LOD 200
             CGPROGRAM
         #pragma surface surf Lambert
         fixed4 _Color;
         sampler2D _MainTex;
         struct Input {
             float2 uv_MainTex;
         };
 
         void surf(Input IN, inout SurfaceOutput o) {
             half4 c = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = _Color.rgb - c.b;
             o.Alpha = _Color.a - c.g;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

and here is what it produces: ![alt text][1] [1]: /storage/temp/57656-shader-white.png White parts should be transparent. If I append alpha to the #pragma the output is very strange!! [alt text][2] [2]: /storage/temp/57658-shader-bug.png What am I doing wrong? Thanks!

shader-bug.png (56.3 kB)
shader-white.png (49.2 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
0

Answer by helarts · Nov 06, 2015 at 02:35 PM

You need "alpha" in the surface declaration :

 #pragma surface surf Lambert alpha

And either multiply your color with the texture or clamp the result:

 o.Albedo = _Color.rgb * c.b;
 o.Alpha = _Color.a * c.g;

or

 o.Albedo = saturate(_Color.rgb - c.b);
 o.Alpha = saturate(_Color.a - c.g);

You can also use a specific fog color like so:

  o.Albedo = lerp(_FogColor.rgb, _Color.rgb, c.b);

Although I don't know what texture you are using so I'm just guessing. Note that "c.b" is the blue channel of your texture and c.g is the green channel.

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
avatar image
0

Answer by keeperkai2 · Mar 14, 2016 at 05:45 AM

I found a way: To be more specific, you can do it in Unity 5 in two ways: The first way, as @helarts says, you simply use the "alpha" option to do this. And the surface shader compiler would add "Blend SrcAlpha OneMinusSrcAlpha" to the compiled vertex/fragement shader.

The second way, is used when you want to specify your own blending ways. In Unity 4, you didn't have to add any option in the #pragma line of the surface shader(you just Blend SrcAlpha One in front of the surface shader), but in Unity 5, you need to add "keepalpha" and specify your Blending options like: Blend SrcAlpha One It took me all afternoon to figure out... I was even thinking about giving up and sticking to Unity 4(I got 700+ shaders so...)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i add a alpha value to this shader? 1 Answer

Custom shader converting to urp need help 1 Answer

Is there a way to create an fading alpha-cutoff effect? 0 Answers

ShaderGraph / Shader URP Cap Alpha Value of Rendered Pixel 0 Answers

Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 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