Access particle system properties in shader.

I’m using the built-in Unity particle system to generate some water effects.
Instead of using the Particles/Alpha Blended Premultiply shader, I use a simple one because of mobile targetting:

Shader "Custom/Tinted Alpha Blend"
{
Properties {
    _Color ("Color Tint (A = Opacity)", Color) = (1,1,1,1)
    _MainTex ("Texture (A = Transparency)", 2D) = ""
}

SubShader {
    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    Blend One OneMinusSrcAlpha
    Cull Off Lighting Off ZWrite Off
    Pass {
        SetTexture[_MainTex] {Combine texture * constant ConstantColor[_Color]}
    }
}
}

This obviously doesn’t include any particle system properties like the “Color over lifetime” property so particles will become transparent at the end of there life. In the Alpha Blended Premultiply shader I found this:

fixed4 frag (v2f i) : SV_Target
{
                #ifdef SOFTPARTICLES_ON
                float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
                float partZ = i.projPos.z;
                float fade = saturate (_InvFade * (sceneZ-partZ));
                i.color.a *= fade;
                #endif
              
                return i.color * tex2D(_MainTex, i.texcoord) * i.color.a;
}

I don’t know much about shaders but when I remove the i.color.a part (last line), the transparency is gone so it looks like this is the crucial part.
Is it possible to implement this feature in the simple shader without including UnityCG.cginc or other advanced shader stuff?

Thanks in advance,
Thomas

This shader might work for you

Shader "Custom/Tinted Alpha Blend"
{
    Properties {
        _Color ("Color Tint (A = Opacity)", Color) = (1,1,1,1)
        _MainTex ("Texture (A = Transparency)", 2D) = ""
    }
    Category {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
           Blend SrcAlpha OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off
        BindChannels {
            Bind "Color", color
            Bind "Vertex", vertex
            Bind "TexCoord", texcoord
        }
        SubShader {  
            Pass {
                SetTexture [_MainTex] {
                    combine texture * primary
                }
                SetTexture [_MainTex] {
                    constantColor [_Color]
                    combine previous * constant
                }
            }
        }
    }
}
1 Like

Awesome! One little thing: I’ve changed “Blend SrcAlpha OneMinusSrcAlpha” to “Blend One OneMinusSrcAlpha” because I only have a premultiplied texture, so when using SrcAlpha there is a typical black glow. When I change it to One, the color seems fine but it doesn’t fade entirely.

not 100% sure this is correct but it looks like it works

Shader "Custom/Tinted Alpha Blend"
{
    Properties {
        _Color ("Color Tint (A = Opacity)", Color) = (1,1,1,1)
        _MainTex ("Texture (A = Transparency)", 2D) = ""
    }
    Category {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
           Blend One OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off
        BindChannels {
            Bind "Color", color
            Bind "Vertex", vertex
            Bind "TexCoord", texcoord
        }
        SubShader {  
            Pass {
                SetTexture [_MainTex] {
                    combine texture * primary alpha
                }
                SetTexture [_MainTex] {
                    constantColor [_Color]
                    combine previous * constant alpha
                }
            }
        }
    }
}

Perfect! Here’s the final result:

Shader "Custom/Alpha Blend Premultiply"
{
    Properties {
        _MainTex ("Texture", 2D) = ""
    }
    Category {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
           Blend One OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off
        BindChannels {
            Bind "Color", color
            Bind "Vertex", vertex
            Bind "TexCoord", texcoord
        }
        SubShader { 
            Pass {
                SetTexture [_MainTex] {
                    combine texture * primary
                }
               
                SetTexture [_MainTex] {
                    combine previous * primary alpha
                }
            }
        }
    }
}

(Removed tinting because particle system already does. Also first multiplied with primary like your first answer to color the texture.)