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 lkarus · Dec 09, 2015 at 05:49 AM · shaderspritetransparent

Making a color transparent in shaders

I have this shader that I have written. I have a 2d rectangular sprite, no textures. If the left side of the sprite is 0 and the right end of the sprite is 1, I am interpolating through and calculating its position within the sprite, and color it accordingly. If it goes out the range, I want to make it transparent. This is what I currently have for the pixel shader.

 Shader "Custom/rangeColorShader"
 {
   Properties
   {
     _RangeColor3("RangeColor3", Color) = (1, 0, 0, 1)
     _RangeColor2("RangeColor2", Color) = (1, 0, 0, 1)
     _RangeColor1("RangeColor1", Color) = (1, 0, 0, 1)
 
     _StartXPos("StartXPos", Float) = 1.5
     _EndXPos("EndXPos", Float) = 1.5
 
     _Bad("Bad", Range(0.0, 1.0)) = 0.5
     _Good("Good", Range(0.0, 1.0)) = 0.5
   }
 
   SubShader
   {
       Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
       Blend SrcAlpha OneMinusSrcAlpha
 
       Pass
       {
         CGPROGRAM
         #pragma enable_d3d11_debug_symbols
         #pragma vertex vert             
         #pragma fragment frag
 
 
         struct vertInput
         {
           float4 pos : POSITION;
         };
 
         struct vertOutput
         {
           float4 pos : SV_POSITION;
           float3 wPos : TEXCOORD1;
         };
 
         vertOutput vert(vertInput input)
         {
           vertOutput o;
           o.pos = mul(UNITY_MATRIX_MVP, input.pos);
           o.wPos = mul(_Object2World, input.pos).xyz;
           return o;
         }
 
         float _StartXPos;
         float _EndXPos;
 
         float4  _RangeColor3;
         float4  _RangeColor2;
         float4  _RangeColor1;
 
         float _Good;
         float _Bad;
 
         float4  frag(vertOutput output) : COLOR
         {
           float pos = (output.wPos.x - _StartXPos) / (_EndXPos - _StartXPos);
 
           if (pos < _Bad)
              return half4(_RangeColor1.r, _RangeColor1.g, _RangeColor1.b, 1.0f);
           else if (pos < _Good)
              return half4(_RangeColor2.r, _RangeColor2.g, _RangeColor2.b, 1.0f);
           else if (pos <= 1.0f)
              return half4(_RangeColor3.r, _RangeColor3.g, _RangeColor3.b, 1.0f);
           else //for debugging purpose 
              return half4(1.0, 1.0, 1.0, 1.0);
 
         }
         ENDCG
       }
   }
     FallBack "Diffuse"
 }

I thought setting the alpha to 0 would make it transparent, but when it hits the else statement, it just goes black. It does not show the color behind the sprite (which is purple). Is my approach to making a section go transparent incorrect?

Edit: Posted my entire shader code. Edit #2: Anyone??

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 wibble82 · Dec 11, 2015 at 09:42 AM 0
Share

There's nothing obviously wrong with your code - it'll be related to blending. If you could post your whole shader then I'm sure we can fix it up.

avatar image lkarus wibble82 · Dec 11, 2015 at 02:42 PM 0
Share

I've just uploaded my entire shader. Thank you in advance!

avatar image wibble82 · Dec 14, 2015 at 09:20 AM 0
Share

Hmmm. Are you aware that in that particular shader you aren't setting the alpha to 0 anywhere? All of the colour values you return have a final component (w or 'alpha') of 1. What happens if you set those to 0. Or even just return half4(1,1,1,0) at the top of the fragment shader?

I'll post a transparent shader when I get home tonight if you're still stuck!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessespike · Dec 09, 2015 at 08:02 PM

You didn't include the shader's Tags and Render State Setup in the question, which is important to know for transparency. Did you update the tags and enable alpha blending?

  Tags { "Queue"="Transparent" "RenderType"="Transparent" }
  Blend SrcAlpha OneMinusSrcAlpha
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 lkarus · Dec 11, 2015 at 05:20 AM 0
Share

Sorry I was away for a while. Yes I do have the tags. Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }

I did some googling on the 2nd line, the Blend stuff. $$anonymous$$ost of the example uses Pass and defines the 2nd line in it.

So I added the 2nd line and it still doesnt seem to work.

Edit: After adding in the Blend code my sprite is just pink. I tried commenting that piece of code out, and then my sprite was appearing as intended. And I'm getting this error in Unity saying that "unrecognized identifier 'Blend' ". I'm still able to run the game though.

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

43 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

Related Questions

Render Texture causing sprite transparency errors 1 Answer

Unlit Speed tree Bilboard shader 0 Answers

Semi transparent Object behind walls? 1 Answer

Sprite not visible from behind 0 Answers

Is there a way to get scene color after transparent pass in URP using the new 2D Renderer ? 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