Search Unity

help with editing a shader please

Discussion in 'Shaders' started by melonhead, Feb 25, 2020.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    i am trying to add a fade by distance to the additive particle shader for unity 2018.1

    i have added a fade distance to the shader but it is not seeming to work, i am not good with shaders so help would be great so i know what i am doing wrong

    if possible i want to adjust the alpha of the particles as they get further away from the particle system transform so i need to be able to have an adjustable fade start distance in the shader

    i have now a working shader but below post is error with one line can anyone help with me please
     
    Last edited: Feb 25, 2020
  2. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    I have a shader that works now but i am getting an error with the following line of code

    Code (CSharp):
    1. o.pos = mul((half4x4)UNITY_MATRIX_MVP, v.vertex);
    this is the error i get:

    Use of UNITY_MATRIX_MV is detected

    can anyone help convert it using the new code: UnityObjectToViewPos

    when i try to replace it i just get further errors

    thank you
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    That error won't be from that line of code. And that line of code should be:
    o.pos = UnityObjectToClipPos(v.vertex);


    If you're trying to get the view space position of something, then use that
    UnityObjectToViewPos
    function you listed.
    float4 viewPos = UnityObjectToViewPos(v.vertex);


    The view space depth (not distance) will be
    -viewPos.z
    since -Z is forward in view space.
     
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    thanks for the help so far but i would like a little help again please, here is the code i have for the shader below, but now it ignores the start color and the color over lifetime in the particle system, i need to be able to make it so that the particles use the alpha and fade out over lifetime and still be faded by distance, can this be done?
     
    Last edited: Feb 27, 2020
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Particle colors are setting the particle mesh's vertex color. You're not passing that data along from the vertex shader.
    Code (csharp):
    1. o.color = v.color * _Color;
    Additionally, if your particles are all just camera facing quads, there's not really a reason you couldn't fade the color's alpha in the vertex shader instead of the fragment shader. That would remove the need to pass the original vertex position to the fragment shader. Even if you're using more complex meshes it might be fine. Alternatively you should probably calculate the view position in the vertex shader and pass only the Z. Or even the scaled (but un-clamped) alpha value and then use saturate() on the value in the fragment shader.



    Note: If you don't need a per vertex color value, you should never do this:
    Code (csharp):
    1. o.color = _Color;
    That's assigning a material property alone to an interpolator (values passed from the vertex to the fragment). Interpolators have a cost, and there's no need for one there since you could just use
    _Color
    directly in the fragment shader essentially for free. So that would be making your shader more expensive for no benefit.
     
  6. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    thanks, just realised to just copy your line into the shader and it works, thank you.
     
    Last edited: Feb 27, 2020