Reading and Writing to the ZBuffer

Hi,

This problem might take a bit to explain but i’ll try my best.

I have a bunch of characters, each has a mesh for their haircut. Sometimes they might have a hat, so obviously I don’t want the hair to stick out the top of the hat.

First I went with the DepthMask found in the Unify wiki (which basically sets the renderDepth of the hat and the hair, so the hat is rendered second and just wipes the RGB, essentially “removing” the hair), this works fine, adding a “CullingMesh” to all the hats correctly stops the hair sticking out.

However…the hats will cull all hair materials in the game. So if someone with say…a huge afro…walks behind someone with a hat, their afro will suddenly be cropped as if they were wearing the hat, and this looks really really bad.

I then tried experimenting with the render depth of each hair and hat to get them in order, firstly I just tried pairing up hats and hairs to have “similar” depths but all different to each other, this wasn’t working, but what I could do is write a script that works out the render order of all the hats and hairs based of word coordinates, and updates all the materials to have the correct render depth each update…

However I was wondering, is there a way to read whats currently in the ZBuffer in a Shader, and also write a custom value there? One possible idea was to have all the Hats render first, and each hat will write its own custom id to the zbuffer, then render all the hair, and each hair will check if its ID is in the zbuffer or not, if it is then don’t render as it’s hat will be in the way.

That may all sound a bit confusing, bit hard to explain, but even if this isn’t the best way to do it (or if that definitely won’t even work anyway), and a script sorting render order is the best way (or if anyone has any other suggestions i’m all ears!), i’d still like to know for interests sake if it is possible to read and write to the zbuffer in this way?

-Iain

Are your hats really complicated below the ears, or might you be able to modify the hair mesh every time a new hat/hair combination is created? Perhaps starting with simply removing hair triangles that have vertices above a certain point.

me too!

although I am reasonably sure the answer is no.

Morphing the mesh was considered as its only needed when the hats are on (also having a seperate hair mesh for “hair with hat”). With morphing the mesh however, it would need to meet perfectly with the hat otherwise bits could still stick out over the top, or the hair could appear to have tears in it (again, assuming we can pull the camera in really really really close). This complication is added with the vertices of the hair are most likely not going to coincide with every hat set up (assuming we could have any hair style with any hat style).

Basically, I want to try and keep any limitations on hair style, hat style, camera position ect to a minimum.

I believe I might just end up with a script depth sorting all hats and hairs so that the shaders are rendered in the correct order, I still think i might have issues with hats clipping the wrong hair so I’m still intrigued by accessing the zbuffer directly.

But again, I think ivkoni is right, googling is coming up with nothing, and my knowledge of CG is very basic…