1
\$\begingroup\$

I am using projectors for shadows...When I use particles for bike speed up i.e., nitro speed the particles get cutout by those shadows....

Here is screenshot of it,

enter image description here

Here is my shader code of projectors ,

Shader "Projector/Projector Multiply Black"
{
    Properties
    {
        _ShadowTex("Cookie", 2D) = "gray" { TexGen ObjectLinear }
    _ShadowStrength("Strength",float) = 1
    }

        Subshader
    {
        Tags{ "RenderType" = "Transparent"  "Queue" = "Transparent+100" }
        Pass
    {
        ZWrite Off

        //Fog { Mode Off }

        Blend DstColor Zero

        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"


        struct v2f
    {
        float4 pos : SV_POSITION;
        float2 uv_Main     : TEXCOORD0;
    };

    sampler2D _ShadowTex;
    float4x4 unity_Projector;
    float _ShadowStrength;

    v2f vert(appdata_tan v)
    {
        v2f o;


        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

        o.uv_Main = mul(unity_Projector, v.vertex).xy;


        return o;
    }

    half4 frag(v2f i) : COLOR
    {
        half4 tex = tex2D(_ShadowTex, i.uv_Main);
        half strength = (1 - tex.a*_ShadowStrength);
        tex = (strength,strength,strength,strength);
        return tex;
    }
        ENDCG

    }
    }
}

Here is my particle code,

// Simple additive particle shader.

Shader "Custom/Particle additive"
{
Properties
{
    _MainTexture ("Particle Texture (Alpha8)", 2D) = "white" {}
}

Category
{
    Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    Blend SrcAlpha One
    Cull Off Lighting Off ZWrite Off Fog {Color (0,0,0,0)}

    BindChannels
    {
        Bind "Color", color
        Bind "Vertex", vertex
        Bind "TexCoord", texcoord
    }

    SubShader
    {
        Pass
        {
            SetTexture [_MainTexture]
            {
                combine primary, texture * primary
            }
        }
    }
}
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ I'm not very proficient in shaders, but it looks like it's enough to fix the relative order of this two shaders. In other words, why not set "Queue"="Transparent+110" in your particle shader? \$\endgroup\$ Commented Sep 15, 2016 at 11:47
  • \$\begingroup\$ Tried that upto +2000 but still same occurring.... \$\endgroup\$
    – idurvesh
    Commented Sep 15, 2016 at 12:38
  • \$\begingroup\$ While waiting for a better specialist to answer, can you make a playground project with these shaders and this issue? It'll be easier to check if a potential answer is correct, or even stumble onto one. \$\endgroup\$ Commented Sep 15, 2016 at 13:40
  • \$\begingroup\$ Sure, check this out bit.ly/2csN85L \$\endgroup\$
    – idurvesh
    Commented Sep 16, 2016 at 6:52
  • 2
    \$\begingroup\$ There seems to be a bug in some recent versions of Unity that disregards the order information in the shader and overrides it. You can use the debug inspector as described in this answer to check for & fix this problem. \$\endgroup\$
    – DMGregory
    Commented Sep 20, 2016 at 3:16

2 Answers 2

0
\$\begingroup\$

Seems like the problem is that your shader does multiply blend, not additive blend, contrary to its name. The shadow shader configured to produce fully black color and when you multiply it by particle color, it's still, expectedly, completely black.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The layering of the particle over the tire/road sure doesn't look like a multiply blend mode... \$\endgroup\$
    – DMGregory
    Commented Sep 20, 2016 at 3:14
  • \$\begingroup\$ Note that the multiplication in the additive shader is multiplying the texture by a tint colour, not multiplying it with the background. The blend with the background comes from this line: Blend SrcAlpha One which is exactly what we expect for additive blending. \$\endgroup\$
    – DMGregory
    Commented Sep 24, 2022 at 18:57
0
\$\begingroup\$

To try to give this old Q&A some closure:

The direct cause of this issue is the queue settings that have been applied to the shadow projector and particle system shaders.

The additive particle shader has "Queue"="Transparent", meaning it will render after all of the opaque and alpha-tested content has been drawn. It also has ZWrite Off, as we'd expect from a transparent effect, so it does not write to the depth buffer.

The "multiply black" shadow shader has "Queue" = "Transparent+100", meaning it will render after anything in the transparent queue above, (and after anything with queue Transparent+1 to +99). Because the additive effect did not write to the depth buffer, the shadow was allowed to draw over the flame, erasing it.

The fix is to change the queue settings so that the shadow renders before transparent effects that you want to render on top of it. Using "Queue"="Transparent-1" for the shadow would be suitable.

At the time, OP reported trying to change the queue setting of the flame, to no effect, but there was a bug in a Unity version around that time that could cause it to not handle queue settings correctly - it's possible this bug gave a false negative when trying to apply this fix.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .