three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Depth plugin issue

Open MKHenson opened this issue 13 years ago • 8 comments

Hi guys

I have been struggling to get the depth renderer plugin to work. I have gone through all the tutorials I can find and I think understand it, but I can't seem to get it working correctly. To cut a long story short I am getting the plugin to a render to a render target. To debug the problem I try to render to that target to the screen using a texture pass whose tDiffuse is set to the plugin's render target. I have attached an image of what it looks like and as you can see it does not look correct. Do you know what this issue might be?

fov_problem

MKHenson avatar Dec 17 '12 15:12 MKHenson

It looks ok to me, that's more or less how RGBA encoded depth is supposed to look like, what did you expect to see?

alteredq avatar Dec 17 '12 17:12 alteredq

Thanks altererdq, I guess I was expecting it to be grayscale; but that makes sense, I just wanted to make sure it was not the problem. I'm just trying to isolate the problem... Let me try a few things and see if I can isolate it.

Thanks

MKHenson avatar Dec 17 '12 21:12 MKHenson

Uhm, shouldn't there be more banding of blues than greens?

mrdoob avatar Dec 17 '12 22:12 mrdoob

Hi guys

Ok so I've been digging up as much on this as I can and I think I have some information which might need to be addressed. In a previous issue( #2713 ) I mentioned that if you use the overrideMaterial property of a renderer it does not work for skinned or morphed meshes. This seems to be the case because if you look at the Depth material you find that it uses the shader 'depth'. When I investigated the depth shader I can see that it does not implement any kind of skinning or morphs.

The Depth pass plugin actually uses a different shader altogether. It uses the 'depthRGBA' shader and that one does support morphed and skinned meshes.

I have a scene where I am using a FOV (BokehShader) post process. I feed the tDepth uniform the depth target.

When I use the override material method it works - but any animated meshes do not animate. When this is rendered to a texture I can see that its a gray scale image.

When I use the depth pass plugin, the meshes are animated, but if I feed that depth target to the FOV shader it makes it look broken. (Everything is hazy and nothing is in focus.). When I render this to a texture it looks like the original image I posted.

With override method (no animated objects) with_override :

Without override (animated objects - but does not work with Brokeh shader) without_override

In both screenshots the effect parameters are the same (though the camera position is slightly different). But you can see that when I use the depth plugin it incorrectly makes everything fuzzy.

MKHenson avatar Dec 18 '12 13:12 MKHenson

Bokeh depth-of-field shader uses simple monochrome depth. If you want to use it with RGBA encoded depth you will need to unpack depth first:

https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLShaders.js#L1488

alteredq avatar Dec 18 '12 14:12 alteredq

Thanks alteredq - I'll give it a shot and see if I can get it to work.

Out of curiosity, is adding animation capabilities to the 'depth' shader on the cards? Was there a particular reason why it was not added?

MKHenson avatar Dec 18 '12 16:12 MKHenson

Hi guys

Im struggling to get this to work. So i decided to use the plugin approach and I think that means I have to edit the Bokeh shader.

I changed these lines in the fragment shader:

First I added the unpack:

"float unpackDepth( const in vec4 rgba_depth ) {",

"const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
"float depth = dot( rgba_depth, bit_shift );",
"return depth;",

"}",

Then I changed the fragment main:

//"vec4 depth1 = texture2D( tDepth, vUv );",

//"float factor = depth1.x - focus;",

"float depth = unpackDepth( texture2D( tDepth, vUv ) );",
"float factor = depth - focus;",

But it still doesn't seem right... A focus value of 1 is making the whole scene clear.

@alteredq Is this the approach you were suggesting?

thanks

MKHenson avatar Dec 19 '12 17:12 MKHenson

Hi guys

So here is what i've found... Unfortunately in its current form there is no silver bullet solution here. Basically if you use the override material method (or the 'depth' shader). The values sent to the Bokeh shader are correct and work as expected.

If you hover you use the plugin method (even after unpacking the value in the bokeh shader), it will not work in Bokeh. During my debugging I found that even after unpacking the value, the scene is not the same as when you use the 'depth' shader.

To resolve the issue I tried taking the code from the 'depth' fragment shader and puting it into the 'depthgrba' fragment shader. Once I got the z value using the same process I then packed it.

ie:

//ADDED by Mat
//=========================================
"float depth = gl_FragCoord.z / gl_FragCoord.w;",
"float color = 1.0 - smoothstep( mNear, mFar, depth );",
"gl_FragData[ 0 ]  = pack_depth( color );",
//=========================================
//"gl_FragData[ 0 ]  = pack_depth( gl_FragCoord.z );",

My presumption was that this would be fine as we will have the correct depth values packed into the vector and then simply use the unpack shader in the Bokeh. This worked successfully and the scene rendered perfectly :) The problem however is that other shaders do not work with this change. The SSAO shader for example expects the z value stored and therefore doesn't work.

So now I'm not sure what to do...

MKHenson avatar Dec 19 '12 23:12 MKHenson