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 penneyworth1 · Jun 16, 2015 at 02:13 PM · shaderadditive

Can I limit each color component to 1?

I have a very simple additive shader. I have noticed that if I place quads using this shader on top of each other, and if the textures of the two quads combine on a pixel where the color adds up such that the resulting color components of that pixel are greater than one, the color (white) seems to bleed onto other pixels. Sometimes I can handle this by dimming the texture of one or both quads, but I would really like to be able to put logic into my shader that limits the result color to 1 for all components. Can this be done?

Here is the current shader:

 Shader "Custom/AdditiveTexture" 
 {
 
     Properties 
     {
         _Color ("Main Color", Color) = (1,0.5,0.5,1)
         _MainTex ("Texture", 2D) = ""
     }
 
     SubShader 
     { 
         Tags {Queue = Transparent} Blend One One ZWrite Off 
         Pass 
         { 
             SetTexture[_MainTex] 
             {
                 // Sets our color as the 'constant' variable
                 constantColor [_Color]
                 
                 // Multiplies color (in constant) with texture
                 combine constant * texture
             }
         }
     }
 } 
Comment
Add comment · Show 1
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 Cherno · Jun 22, 2015 at 05:07 PM 0
Share

I assume that $$anonymous$$Athf.Clamp, or checking if the value is higher than 1, is out of the question in shader code?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dave-Carlile · Jun 22, 2015 at 06:03 PM

There is a clamp function for shaders, but I don't know if it works with the Fixed Function syntax that you're using.

Comment
Add comment · Show 5 · 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 penneyworth1 · Jun 22, 2015 at 06:14 PM 0
Share

Do you know what the code would be using non-fixed function syntax?

avatar image Dave-Carlile · Jun 22, 2015 at 06:25 PM 0
Share

Something like this...

 float4 your_color_value;
 your_color_value = clamp(your_color_value, 0, 1);

 
avatar image penneyworth1 · Jun 22, 2015 at 06:43 PM 0
Share

Thanks. Unfortunately with my severe lack of experience in shader writing, I have no idea in what context this code is applied, or how I would first get the color combination that includes the original color (to be added to), the specified texture, and the specified color that tints the texture by multiplying - and then getting the individual components to then clamp, and then reassemble into a color to pass out. I'm assu$$anonymous$$g this has to all happen within a CGPROGRA$$anonymous$$-ENDCG block, and that the code to combine the colors from my original shader will be quite different.

avatar image Dave-Carlile · Jun 22, 2015 at 06:51 PM 0
Share

Yes, you'd have to write all of the shader code yourself.

Another thought - the behavior you describe is often what additive blending is used for - to get a bright/glowing effect by adding the values. Have you tried multiplying the values ins$$anonymous$$d of adding? It seems counterintuitive, but since the values are

In fact you can see that behavior in your shader - the tint color is multiplied by the texture color to combine them. I'm fairly certain you can set the blend mode to multiply ins$$anonymous$$d of add, but I can never remember those settings off the top of my head.

Here are the docs for the blending: http://docs.unity3d.com/$$anonymous$$anual/SL-Blend.html You might try the alpha blending or multiplicative.

avatar image penneyworth1 · Jun 22, 2015 at 07:16 PM 0
Share

I appreciate the thought. I can't use multiplicative color blending unfortunately because the background is a mostly black starfield. Now, I was under the impression that additive blending is much more efficient than alpha blending. Is that true? Of course, switching to alpha blending makes the whole problem go away, but I had hoped to get the efficiency of color adding but still avoiding the unwanted glow via clamping. I'll keep digging and try to write a shader with the CGPROGRA$$anonymous$$ stuff (that I currently have zero understanding of) Thanks!

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

23 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

Related Questions

Additive shader with zbuffer off 0 Answers

Particle Additive Shader make white strap 1 Answer

Particles/Additive Shader not showing in Android 0 Answers

Additive particle shader with higher max alpha? 0 Answers

Is additive transparency incompatible with this implementation of falloff transparency? 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