Search Unity

Combining Shaders

Discussion in 'Shaders' started by uonlyliveonce, Nov 28, 2018.

  1. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    9
    Hello, I'm a complete newbie when it comes to shaders. I have a ghost creature (frag) with a shader that I'm trying to combine with a dissolve effect (surface). I've put them together but the surface shader is on top of the ghost shader. How do I take the output of the ghost shader and feed it into the dissolve? Is it possible?

    Thanks in advance.

    Edit: Nevermind, I figured it out!
     
    Last edited: Nov 28, 2018
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,115
    You don't. You write a new shader that does both in one shader. When you put both a vert / frag shader and a surface shader in one file they're effectively acting like two separate shaders still, just that Unity will try to render one after the other.
     
    uonlyliveonce likes this.
  3. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    9
    Thank you. I've fixed the problem by converting the frag shader into a surface shader and combining the outputs.
     
  4. Quakeulf

    Quakeulf

    Joined:
    Mar 26, 2013
    Posts:
    40
    uonlyliveonce likes this.
  5. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    9
    That's the answer I saw before I posted this thread. I tried it and the object had a standard look which dissolved to reveal the ghost shader underneath instead of the ghost shader dissolving to complete transparency. So basically, while both shaders were successfully combined into one shader, the surface shader was on top of the frag shader instead of using the frag shader's output and dissolving that.

    I had a similar problem when I wanted to put a black outline on an object with a dissolve shader. When I combined the shaders, instead of dissolving to complete transparency, the object was completely black underneath.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,115
    Having multiple shader passes in one shader file is great for all kinds of effects, but it's an additive thing. Once a pass has been rendered you can't "remove" it with a later pass to show what's was behind it, only draw something else on top. Outlines are a good example of using multiple passes, but like you saw when you make the main color pass transparent you just end up showing the outline pass underneath*.

    * You can somewhat solve this problem by drawing the main object with a depth only or stencil pass first, then drawing the outline pass which is hidden by the depth or stencil, then drawing the object again using the main color pass. When you fade out, just make sure to fade both the outline and the main color passes.

    You can also do a dissolve with a shader that uses multiple passes just fine, as long as all of the passes do the exact same dissolve. However the original question was "can I do a fade in one pass and a dissolve in another and have both affect both passes", to which the answer is no... well, unless you get really complicated with stencils ... but really it's way better 99% of the time to merge the functionality into one pass.
     
    uonlyliveonce likes this.
  7. uonlyliveonce

    uonlyliveonce

    Joined:
    Nov 6, 2018
    Posts:
    9
    Wow thank you!!! I'd given up on that outline dissolve shader but with your input I finally got it to work!
    I've been messing with all these shaders for several days and now everything works! Ugh, it's so beautiful. :D