Special opacity shader wanted

Hi,

I am developing an AugmentedReality Projekt where parts of the mesh are located under the floor (Imagine a house with a basement level).

To get a realistic visual effect I need to “open” the floor over that basement area.

Are there any ideas how to do that?

Thanks
Carsten

Another way would be a shader that makes an object (a fake floor mesh with a hole in the middle) full transparent and culls all objects behind it.
Would this be possible?

The technique is known as Z masking.

Create a mesh that represents the floor and give it a material with this shader (remember and cut out a hole for the bit you want to see through).

It’ll write to the ZBuffer, so it’ll occlude any geometry that’s behind the polygons of the floor (because a depth test will tell it your floor mesh is in front of it, so don’t render there) but it will be otherwise invisible.

The Queue of Geometry-50 that this shader will make it draw before the rest of the geometry in the scene.

The ColorMask 0 means this shader doesn’t render to either the RGB or A channels of your screen output (but WILL still render to the depth buffer).

Shader "Custom/ZMask" {

	Properties {
	}

	SubShader {
		LOD 100
	
		Tags { "Queue"="Geometry-50" "IgnoreProjector"="True" "RenderType"="Opaque" }
		
		Pass {
			ColorMask 0
		}
	}
}
3 Likes

WOW, that´s it!
Thanks so much!!!

Carsten

Any idea how to recreate this using a fragment and vertex customised shader program? I need to apply this effect along with a couple of others and am struggling to understand how to output this

The important part is that tag of “Queue” = “Geometry-50” - this ensures it draws before things with “Geometry” as it’s queue.

So anything you write - either custom vert/frag or surface shader - that has a tag saying “Queue” = “Geometry” will be hidden by a mesh with the above shader applied.

For more on what the Queue tag is, read up on this; http://docs.unity3d.com/Documentation/Components/SL-SubshaderTags.html

Now I run into clearing-problems when I use a skybox.
Could you help me once again?

Could you post pics? It shouldn’t affect the skybox, that’s the first thing that’s rendered.

Here´s a screenshot:
https://dl.dropbox.com/u/1040921/00_Permanent/Bildschirmfoto%202012-09-18%20um%2016.16.06.png

I tested by changing the “RenderType” to “Background” but it doesn´t help.

I’ve had a look at this and honestly I’m a bit stumped.

As far as I can tell, the skybox shader should render before everything else, so the ZMask shouldn’t affect it at all.

I dunno how to fix that :frowning:

Which skybox shader are you using?
Can you post the ZMask shader if it is any different from the one Farfarer posted?
What are the “Clear Flags” of the camera?

For what it’s worth, I was testing it with the default skybox shaders (the regular 6-image skybox and the cubemapped skybox) and the mobile one (which is the same as the 6-image one but without a tint, I think).

Also created a custom skybox shader based on the 6-image one and messed with it’s render queue and ZTest settings… to no avail.

Camera has to have Clear Flags set to Skybox otherwise it doesn’t render it at all.

I guess you could do a Source Engine thing and have a second camera in the centre of an actual box, with it’s rotation slaved to the main camera, and set it to render first.

Hmmm, ok - so I have to wait until someone find´s a solution.
Anyway, thanks for helping!

It’s rather difficult to fix problems based on screen shots only.

I think you should prepare a simple Unity project that shows the problem and make that project available. (We are good here, but we aren’t wizards. :wink: )

I appreciate this is an old thread - but this is exactly the shader I’m looking for at the moment. I’m getting the clearing issue as well. I’ve put a unity project together to see if anyone can help us get this working.

The sphere in the test scene has the shader above attached to it, and I’ve extrapolated the unity skybox shader into a modifiable shader file. The skybox just reuses a single texture, just to save zip file size. Looks naff, but does the job.

Weird thing is, the scene view is rendering the shader properly, but the game view is displaying the artefacts.

Any help is appreciated, I’ve been reading about Shaders / Cg for days, but I’m still yet to figure out what I’m doing - it’s sinking in very slowly :frowning:

1443990–77709–$shaders.zip (30.7 KB)

I just wrote a similar shader for a different thread, and I noticed it suffered from the same bug after reading your post.

I fixed it by creating a dummy camera (culling mask set to “Nothing”) with a lower depth that the main camera. Clear the dummy camera to “Skybox” and set the main camera to “Don’t Clear”.

This seems to work properly. I don’t know why this would be necessary. It looks fine in the editor, but doesn’t clear properly in-game. Strange.

Hmm… luckily, in my main game project I already do this (or at least something similar that I can play around with) as I have different cameras for rendering things at different distances. I’ll give it a go… though you’re right, I’m not sure why I need to!

I can confirm that this works. It’s a bit annoying you have to go to this extra step to get it to work, though :frowning:

1 Like

Something else I just noticed - you have to have “Receive Shadows” set on the mesh renderer of the object in order for it to even be visible.

2 Likes

2022 and it still works! Thanks :slight_smile: