- Home /
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
}
}
}
}
I assume that $$anonymous$$Athf.Clamp, or checking if the value is higher than 1, is out of the question in shader code?
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.
Do you know what the code would be using non-fixed function syntax?
Something like this...
float4 your_color_value;
your_color_value = clamp(your_color_value, 0, 1);
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.
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.
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
![](https://koobas.hobune.stream/wayback/20220613201040im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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