In the first pic I’m using the standard unity diffuse shader and in the second I’m using my logarithmic shader: -
As you can see the planet has been flattened on the z axis and the terrain skirts are z fighting with the planet surface. Has anyone else experienced these kind of problems? Any help would be muchly appreciated.
I think he says Using the following equation to modify depth value after it’s been transformed by the projection matrix: The coordinates you are modifying are still in object space.
He sure is saying that. The adjustment should be in clip space instead of object space.
I’m not sure whether this is possible when combined with surface shaders. With a full shader it should be easy. I’m not sure where you would have to output you own custom clip space position and whether that would actually work with a surface shader. The whole idea of a surface shader is that you focus on the appearance of the material and let unity handle the normal decoding and z-buffer writing.
It’s a pretty interesting concept. I used to read that guys blog a lot it’s quite impressive. Yeah you are right on the surface shader stuff. I heard in Unity 4 you can actually modify the z-buffer. So rather than transforming the verts you might be able to just modify the z-buffer directly. Well I guess that might be what you are doing by moving the vertex coordinates:)
The color of the pixel is actually the only mandatory thing a pixel/fragment shader must write out. There is an optional depth value in for example HLSL. Writing a depth value in a surface shader should work just fine. (Wouldn’t break the generated prepasses, forward passes, etc.) The only place it probably would break things is in the depth passes for the shadow maps. But I’m guessing not many people are using custom depth shaders for the shadow maps anyway.
Thanks for the replies. I did try the same thing in a basic vert/frag shader but it produces the same results (apart from the lack of diffuse lighting ofc). I’m not clear on why as in this one you can see it is transforming to clip space first (or at least that is my understanding of multiplying by UNITY_MATRIX_MVP).
I’ll try editing the depth value in the frag shader and see what happens.
Here’s first attempt messing with depth. Creates some interesting results! I tried placing two spheres slightly apart scaled to xyz100,000 at z100,000 from camera. One sphere appears to overlap the other. What’s strange is moving the sphere that appears behind towards the camera does not cause it to draw in front of the other one even if you bring it really close to the camera!
Further testing shows that there is a problem writing depth values in general as setting the depth to any value even a fixed one e.g. 0 or 1 produces the same output as if it is ignoring the depth completely.
I’m reviving this thread to see if anyone has figured out how to do this in Unity yet. Here’s another thread where someone was able to get it working, but his solution didn’t entirely work for me. Custom Z-buffer problem on mac and windows - Unity Engine - Unity Discussions . When I tried it, using .01-1bill on the cameras culling planes, I didn’t have normal Z-tearing issues like you would expect. However the draw order seemed backwards, like it was drawing back to front, so I could see faces that should have been blocked by closer geometry.
I would like to get it working with surface shaders though (like OP was trying to do here) because I am still learning shaders and it seems like surface shaders do a lot of the boilerplate stuff for you. It seems like you can only edit the input vertices for the surface shader though.
I’m making a space exploration game and my current setup is with two cameras, one from .3-1000 and the second from 1000-1million, but I would like to reduce the overdraw of objects in both cameras (among other reasons). That is why I am searching for alternatives.
Can’t you use the _ProjectionParams built-in shader variable instead?
// x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix),
// y is the camera’s near plane,
// z is the camera’s far plane
// w is 1/FarPlane.
float4 _ProjectionParams;
I don’t believe it did, but it does now in the Unity 5.5 beta, so logarithmic z-buffer modifications should no longer be necessary for most scenes:
Graphics: Improve shadows precision for large worlds
Use reverse-float depth buffer for improved depth buffer precision on modern GPUs. Particularly improves directional light shadowing for large view distances.
According to Aras inverse-z is in 5.5, but only for DirectX 10+ ( DX9 has no floating point depth buffers and relevant GL-extensions are not available on Mac / mobile, so no inverse-z for DX9 and OpenGL)