sp614x / optifine

1.81k stars 418 forks source link

Request: Backface depth texture #1346

Open robobo1221 opened 6 years ago

robobo1221 commented 6 years ago

Hey sp614x,

Could we have a depth buffer for backfaces? This would be handy for stuff like proper water fog. Since currently waterfog goes basically into infinity with depthtex1.

You can see that result here: image As you can see, the waterdepth basically goes to infinity instead to the backface of the water.

What the backface depth would do is act like depthtex0. But instead of including only the front face of every block, only include the backface of every transparent block.

You could make it so this depth buffer can only be used whenever backFace.translucent is set to true in shaders.properties

You could call this texture "depthtex3" or something like that.

Thanks in regards,

sp614x commented 6 years ago

Water blocks have no backface where it touches solid blocks. Not sure if "solid + water backface" depth buffer is easy to add.

robobo1221 commented 6 years ago

I think that would be easy. Maybe you can say for instance if the solid depth is less than the transparent backdepth, use the solid depth instead. And viseversa

sp614x commented 6 years ago

Can't this be done in the shaders? They have access to the terrain depth (depthtex2).

robobo1221 commented 6 years ago

Yeah this can also be done in the shader. However, I do not know if that's faster than doing it in optifine. Alot of time, we use the depthbuffers in a loop. And having two texture lookups wouldn't be very practical compared to just one. It would also be two times slower.

robobo1221 commented 6 years ago

Using two lookups is slower in general actually

dotModded commented 6 years ago

The big thing here is not using more color attachments then we need, could we separate the depth when backfaces are enabled and write it to a buffer, yes. However doing so is incomvienient and adding read write buffers slows down the shader more than you could imagine. Not to mention being terribly inconvenient to set up. Being able to know how thick something is is very useful for absorbtion, some light scattering, and even refractions.

sp614x commented 6 years ago

This would need a second translucent render pass, another copy of the depth buffer and another sampler. All 16 composite samplers are already used: https://github.com/sp614x/optifine/blob/master/OptiFineDoc/doc/shaders.txt#L215

robobo1221 commented 6 years ago

So after some thinking I think I came up with an idea:

Make another boolean in the shader.properties called something like: useBackfaceDepthtex What you could do instead of creating a whole new sampler uniform is to actually change depthtex1 to the backface depth. Basically how depthtex1 already is but instead of not rendering transparents at all, only render the backface of them, Also ofcourse include the solid depth.

This way you don't have to add any buffers. Plus we need to have the backface anyway for everything that uses depthtex1.

I think this way we keep compatibility with older packs aswell.

jcm2606 commented 6 years ago

Repurposing depthtex1 isn't the best idea. Depthtex1 is required to calculate volumetric effects from the eye to an opaque block, or a transparent block to an opaque block.

The idea is good, though depthtex1 isn't the depth texture that should be repurposed. Instead we should repurpose depthtex2, as the only use case of depthtex2 that I know of is to calculate a hand mask, and you can do that quite easily by adding a hand mask to the existing material masks shaders create.

robobo1221 commented 6 years ago

Hmm yeah. The best solution is to use depthtex2