Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Transparency with 2D sprite shader

Discussion in 'Shaders' started by DrSpoutnik, Nov 6, 2022.

  1. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    64
    hi !

    i'm a newbie and something is not clear to me with shaders :

    my game project is a 2.5D platform game where the player is 2D : he's made of several 2D sprites animated in Unity. i use the Shader " sprite/diffuse" to allows the lights affect the sprites of my player.

    the environnement is a mix of 3D and 2D (ground, rocks are 3D, and trees, and other ting are 2D)
    i want my player to cross a river and i would like this water to be transparent. the water is a 3D cube, with a transparent material on it.

    the problem is :
    my player is always on top of the water, he doesn't get through the water as soon there is transparency.
    here screencaps:
    The first one shows the water transparent, and the player over it...

    the second one, i change the water material with antoher similar but set in 'opaque' mode :


    the player is going throught, but there's no transparency...


    if i change the player shader to "standard", i get what i want :


    but as soon as the player flip to facing left, he disappears (?)

    i'm not sure to understand what's happening.
    could someone help me to understand, and maybe even find a solution ?

    thanks
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Transparents are sorted then rendered back to front. Transparents can't intersect other transparents like that. One or the other has to be completely in front.

    Transparents contrast to Opaques which are rendered first and these can intersect one another. The standard material is opaque.

    What's happening when your player flips left, is probably culling related. With a 3D mesh, the back side is culled away because you can't even see it so this helps performance. Sprites, however, typically don't cull. My guess is that when you flip the sprite, you're showing the backface - but with the standard material this is then culled away!

    The solution is to turn Cull Off (both backs and fronts visible) for your character if you use the standard shader on the character. Alternatively, your water could use a cutout shader (opaque but with tiny holes in it) to pretend to be transparent.
     
  3. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    64
    Thanks Lo-renzo !

    I did add "Cull Of" to a copy of a standard Shader.
    it seems to resolve my problem thanks.
    but i have another, maybe it's the same king of issue here :
    when my sprites are set to "sprite" defaut there fine (but i prefer to keep them in standard shader with cull off)
    when they are in "standardCullOff", the sorting of my layers is a mess... i'm not sure to undertand why it's like that and what i could change in the standard shader ?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Opaque messes up sorting layers. Maybe you could only set your character to the standard material? To get the water intersection with the character effect, your character material needs to not be transparent. But the rest of your sprites probably can still be transparent, so they should still be ok with your layers.
     
    Last edited: Nov 9, 2022
  5. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    64
    Oh, ok good to know that Opaque mess with sorting layers. it's too bad.

    i don't know if your solution would be enough... my character has only the feet in the water here, but later, i would like him to be half imerged in a river, and even dive and swim at the surface or underwater.
     
  6. unity_IbS-7NuhtnJMtg

    unity_IbS-7NuhtnJMtg

    Joined:
    Nov 12, 2020
    Posts:
    1
    I had the same Problem, i managed to solve it by changing the Sprite Shader to the default 3D Shader and using the Rendering Mode Cutout. The Problem is, that the Sprite is rendered transparent by default and Unity has problem displaying a transparent object(the Player) in a transparent object(Water). By changing the rendering mode to Cutout you manage to fix the Problem because then the Sprite is not rendered Transparent anymore.