Search Unity

Question Particle Blending via RenderTexture

Discussion in 'Universal Render Pipeline' started by chrismarch, Feb 1, 2021.

  1. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    471
    If I render a ParticleSystem to a RenderTexture with URP 7.5.1 (Unity 2019.4.16f1, Metal), the alpha values from the PS seem to be ignored, and only the Background color alpha used for the Camera rendering to the RT is used, except where opaque objects rendered to the RT.

    You can see my settings for the Camera rendering to the RT and the ParticleSystem material here (the sphere on the right surrounded by the dim particles is the quad with the RT assigned to the unlit material):
    upload_2021-2-1_13-50-38.png

    Here are the RenderTexture settings:
    upload_2021-2-1_13-57-49.png

    Here is the URP quality settings asset:
    upload_2021-2-1_13-57-6.png
     
  2. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    Which shader is the particle material using?

    Some particle shaders such as URP/Particles/Unlit have ColorMask RGB in the shader, which means they don't write to the target texture's alpha channel.

    Simple Lit and Lit particle shaders don't have the ColorMask specified though, so they should output their alpha as usual.

    Make sure you clear the alpha channel of the RenderTexture to 0 as well, before rendering. (As well as depth ofc)
     
    chrismarch likes this.
  3. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    471
    I've made some progress with your tips, thanks. I see that the URP/Particles/Simple Lit (and Mobile/Particles) include the alpha in the color mask, and things are starting to work. The one remaining bit is that the blending of the RenderTexture quad with the particles on it seems to be faded a bit from the source particle system. For this image, I am using URP/Particles/Simple Lit with Blending Mode Alpha and Color Mode Multiply for the ParticleSystem. For the RenderTexture quad, I am using URP/Particles/Simple Lit with Blending Mode Alpha and Color Mode Overlay. My RenderTexture capture camera is set to Background Type Color and Background (0,0,0,0):
    upload_2021-2-2_17-49-41.png
     

    Attached Files:

  4. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    471
    Getting pretty close, now that I am using a Premultiply Blend Mode on the ParticleSystem (plane using RenderTexture is on the right):
    image (26).png

    Here is Premultiply Blend Mode with Additive Color Mode on the ParticleSystem, and Additive blending on the plane that uses the RenderTexture that captures the ParticleSystem. Also pretty close, if you ignore the sphere:
    image (27).png
     
  5. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    184
    It should be possible to get them to match exactly.

    I think you'll need to use premultipled alpha when rendering into the texture. (Eg multiply the output by the alpha, but
    output the alpha as usual. (Eg return float4(color * alpha, alpha), and Blend SrcAlpha OneMinusSrcAlpha)

    Then when sampling the final texture, you'll want to do Blend One OneMinusScAlpha.
    Or if you're doing it in the shader itself, "float4 result = background * (1 - texture.a) + texture;"

    I could be slightly wrong about the order of things or the blend modes, but with a bit of fiddling you should be able to get it to match.