Dissolve + Depth Mask?

Hi, I’m looking to combine the effects of two shaders on the wiki–

Depth Mask – http://www.unifycommunity.com/wiki/index.php?title=DepthMask
–and–
Dissolve with Texture – http://www.unifycommunity.com/wiki/index.php?title=Dissolve_With_Texture

I want an object to cut holes in objects behind it using the alpha channel of its texture as a guide. I’m new to ShaderLab and have been muddling through tutorials and trying to stack them together in various ways, but having no luck.

Can anyone tell me of an easy way to achieve what I’m looking for or if it’s even possible on an iPhone 4?

Did you read the page on Alpha Testing or watch my tutorial about it? That’s what you need if you just use ShaderLab. If you use GLSL, you use the discard keyword for the same thing, and in a surface shader, you apparently use clip, as shown on the examples page.

Thanks for the tips. I completely got the effect I wanted. Your tutorials are invaluable!

I know this post is kind of old, but I was curious as to what your solution was. I’d like to use do a Depth Mask with Soft/Alpha edges, so I’m anxious to know if you’ve figured it out.

Depth is either written/tested against or not; it doesn’t work the way I think you think it could. For what you want to do, you’d have to use render textures with blending shaders, or incorporate the alpha channel of other objects for blending the currently rendering object.

Hey Jessy,
Could you please elaborate about making a Depth Mask with soft alpha edges?
I’m currently using the technique and shader in this postpost.
If you can provide a small example that would be awesome. Would I have to create two materials, one with the depth shader and another with a blending shader? Can you explain more about how to “incorporate the alpha channel of other objects for blending the currently rendering object” as you say?

thank you!

Example:
Render all objects in the scene that you’ll see through the depth mask.
Render the alpha channel of a “depth mask object”, using an alpha texture or something that fades from black to white.
Render your scene with ZTrest Greater and Blend DstAlpha OneMinusDstAlpha.
Render normally.

I haven’t tested that, but it sounds right to me. Give it a shot and let me know if it works. :-/

Hi Jessy. I’m trying to follow your suggestion but I’m a bit confused.

I’m trying to mask some terrain but I want a soft edge. So first I rendered the terrain, then the masking object using an alpha mask. So at this point I have my terrain and the mask geometry which fades from white to transparent at the edges.

Where I’m stuck is at the last two renders. You say to render the scene with ZTest Greater and Blend DstAlpha OneMinusDstAlpha. How exactly do I do that? I’m assuming just write a super simple shader with those properties? But how do I apply it to the whole scene? Do I need to duplicate all the geometry and materials and render them again with this shader?

Thanks for any help you can give.

I’m confused on that point; I think the rest of us have been talking about mask geometry that doesn’t render in RGB.

Geometry, no. Replacement shaders are probably what you should use, if your meshes are too complex to just use multiple materials with different queues.

I’m just using a black and white alpha mask texture with a transparent shader. So the black parts are transparent and the white parts are showing as white. Is that not right?

This is what I have so far. It sort of works.

  1. Render anything that should be visible through/behind the mask. I found that for these things I had to use a shader with a color property, and have the alpha of that set the 0. Otherwise the areas of the mask with geometry behind them wouldn’t mask.
  2. Render the mask geometry with a Depth Mask shader
  3. Render the geometry to be masked normally.
  4. Render the mask geometry with an alpha mask.
  5. Render the geometry to be masked with a soft edge using ZTest Greater and Blend DstAlpha OneMinusDstAlpha

So basically it’s masking and then putting the soft edges on top. Is this the way to go? How come the mask doesn’t work unless the geometry in the first render has an alpha of 0?

This seems to work in Unity but not on the iPad… I’m trying to use Vuforia with this, so I’m guessing that may be the issue…

I don’t see how it would be, unless you need white rendered.

I don’t follow what you’re doing, with typed words. Here is a package that represents what I think I was talking about. Maybe I wasn’t thinking clearly, or maybe I wasn’t considering having multiple objects (or one not-flat object) behind the depth mask, because that won’t sort right, with ZTest Greater. Unless you’re working with the special case where your objects don’t overlap themselves in camera-Z, I don’t think you’re going to get good enough results where your depth mask’s alpha is grey, unless you use render textures.

1210982–49227–$Soft Edge Depth Mask.unitypackage (68.9 KB)

Duh, now that I see your test package I see that I should have checked “Alpha from greyscale” in the texture import settings. I was just making an alpha channel in photoshop and using a transparent shader…

I’ve tried your test package, but I’m a little confused about the Opaque material? I’ve tried making a really simple scene with your shaders but can’t get it to blend. Would you mind taking a look at what I’ve done?

1211144–49255–$Soft Edge Depth Mask Test.unitypackage (80.2 KB)

Thanks so much for your help so far. I’m pretty dumb when it comes to shaders, so I really appreciate it.

Well, you’re multiplying the depth mask’s texture by zero, via the material color, so the texture won’t have any effect.

Great, I got it working in Unity 4.0. But when I try the same thing in 3.5 it doesn’t work. Is there any reason this wouldn’t work in 3.5?

I got it working in Unity 3.5. Here’s the shaders I used in case anyone else needs them. The “blended by depth mask shader” could be modified to use normal maps, etc but copying and pasting the body from one of the other built in shaders.

1211369–49267–$Blended Depth Mask Shaders.unitypackage (1.41 KB)

Also, to get this to work on iOS you need to check “Use 32-bit display buffer” in the Resolution and Presentation built settings.

Thanks so much for all your help!

Anyone able to update this to current unity? The Unity 3.5 shaders no longer work …