Search Unity

Particles Premultiply Blend shader incorrect alpha premultiplication?

Discussion in 'Shaders' started by voldemarz, Feb 24, 2013.

  1. voldemarz

    voldemarz

    Joined:
    Sep 19, 2011
    Posts:
    25
    I am rendering some particles (Unity's default particle texture) to a rendertexture. It is later used as a texture for plane with alpha blend shader (player's inventory item preview).

    The defaut Premultiply Alpha Blend shader used for particles doesn't write to alpha, so first I had to slightly modify it's color mask.

    The problem is that when rendered to a texture the alpha channel seems darker than it should (or particle color brighter than it should). Here's how it looks:

    $premul_default_rga.PNG $premul_default_a.PNG

    I think it's because vertex color alpha premultiplies ifself. As far as I understand only color should be premultiplied. Here's the code of the fragment shader.
    Code (csharp):
    1.  
    2. Blend One OneMinusSrcAlpha
    3.  
    4. fixed4 frag (v2f i) : COLOR
    5. {
    6.     return i.color * tex2D(_MainTex, i.texcoord) * i.color.a;
    7. }
    8.  

    I modified it to premultiply only color and now I get alpha channel which correctly represents transparency. Here's code and pictures:
    Code (csharp):
    1.  
    2. Blend One OneMinusSrcAlpha
    3.  
    4. fixed4 frag (v2f i) : COLOR
    5. {
    6.     // premultiply only color with alpha
    7.     fixed4 res = i.color * tex2D(_MainTex, i.texcoord);
    8.     res.rgb *= i.color.a;
    9.     return res;
    10. }
    11.  
    $premul_modif_rgb.PNG $premul_modif_a.PNG

    So the question is is if this is an error in the default shader or it was done on purpose to make particles look brighter (knowing that by default it doesn't render to alpha) ?
     
    Last edited: Apr 1, 2013
  2. voldemarz

    voldemarz

    Joined:
    Sep 19, 2011
    Posts:
    25
    Anyone?
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    None of Unity's alpha blended shaders have ever done anything sensible with the alpha channel. I believe this was originally an oversight, and has been maintained either for compatibility or due to continued oversight.
     
  4. voldemarz

    voldemarz

    Joined:
    Sep 19, 2011
    Posts:
    25
    Thanks for clarification. If anybody has other explanation I'm willing to hear it.
     
  5. sameer-mirza

    sameer-mirza

    Joined:
    Nov 14, 2011
    Posts:
    36
    Hi, I'm doing something similar (without using vertex colors):

    I get borders around my paint strokes, even after changing the blend mode to use One OneMinusSrcAlpha, I'm getting a black border. I'm using a Projector to paint with colors and output to a rendertexture which is then used to show the painting in the scene.
    This is what is returned at the end of the Projection shader:

    float4 output = float4(paintValue, alpha);​

    where paintValue is the color value of the paint and alpha is taken from reading the tex2Dproj's alpha value.
    Capture.PNG RT_RGB.PNG RT_A.PNG