Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDFRaymarch - Read the depth value #491

Closed
RenaudRohlinger opened this issue Nov 19, 2022 · 8 comments
Closed

SDFRaymarch - Read the depth value #491

RenaudRohlinger opened this issue Nov 19, 2022 · 8 comments
Labels
question Further information is requested

Comments

@RenaudRohlinger
Copy link

I'm looking for a way to get the correct depth value of a raymarching result. I will open an issue here as I think it could benefit other developers too.

I made an example with a post process splitting the screen into 4 sections:
1 - scene albedo
2 - raymarching albedo
3 - scene depth
4 - raymarching depth (using custom gl_FragDepth = length(point.xyz))

https://codesandbox.io/s/romantic-sunset-uqhc13?file=/src/RayMarchSDFMayerial.js
image

@RenaudRohlinger RenaudRohlinger added the enhancement New feature or request label Nov 19, 2022
@gkjohnson
Copy link
Owner

gkjohnson commented Nov 20, 2022

Hmm - I'm not able to see the raymarch depth buffer on my machine. Is there something I need to enable?

image

@gkjohnson gkjohnson added question Further information is requested and removed enhancement New feature or request labels Nov 20, 2022
@RenaudRohlinger
Copy link
Author

RenaudRohlinger commented Nov 20, 2022

Oh, my bad indeed!

It seems that some WebGL Implementations do not like the texelFetch syntax (Metal for instance). I changed it with a basic texture()

readDepthFrag(texelFetch(sceneDepth, fCoord, 0).x => texture(sceneDepth, vUv).x

It should be fixed @gkjohnson 👍

https://codesandbox.io/s/romantic-sunset-uqhc13?file=/src/RayMarchSDFMayerial.js

@CodyJasonBennett
Copy link
Sponsor

Looks like I get a correct result with viewZToPerspectiveDepth, initializing fragCoordZ = -1. Taking note of the math linked from https://github.com/mrdoob/three.js/blob/bb23058464420d8fd510337ff400182ba662e8dd/src/renderers/shaders/ShaderChunk/packing.glsl.js#L54.

float cameraNear = 0.1;
float cameraFar = 5.0;

gl_FragDepth = viewZToPerspectiveDepth(-(fragCoordZ / gl_FragCoord.w), cameraNear, cameraFar);

image

@gkjohnson
Copy link
Owner

gkjohnson commented Nov 21, 2022

Thanks @CodyJasonBennett this is great!

A few changes to make still, I think. You shouldn't need to do anything with fragCoord.w since the raymarching is happening in view space (no projection matrix applied) and the w coordinate should be 1.0 anyway since it's just a full screen quad being rendered in that pass.

And I believe the fragCoordZ should be set to point.z rather than length( point.xyz ) (my bad 😁) since it just needs to test z distance for depth:

float fragCoordZ = - 1.0;

// ...

if ( intersectsSurface ) {

  fragCoordZ = - point.z;
  // ...

}

// ...

gl_FragDepth = viewZToPerspectiveDepth( - fragCoordZ, cameraNear, cameraFar );

The depth visually matches a lot more precisely after these changes though more testing should still be done to make sure the type of shifting you originally showed me is addressed @RenaudRohlinger.

image

For performance reasons in the raymarch loop, though, I would transform the scene depth from the existing depth buffer beforehand to check in the for loop.

@CodyJasonBennett
Copy link
Sponsor

Awesome, I had to set fragCoordZ = -point.z, but it works nicely.

@gkjohnson
Copy link
Owner

gkjohnson commented Nov 21, 2022

Awesome, I had to set fragCoordZ = -point.z, but it works nicely.

Oops - you're right. I'd inadvertently been taking the absolute value of point.z. I've updated my notes above to specify your change. Thanks!

Tough I guess we could just remove the negation in all the places, instead 😅

@RenaudRohlinger
Copy link
Author

Awesome! Thank you so much for this guys, it's working perfectly 😊

@gkjohnson
Copy link
Owner

Awesome! Thank you so much for this guys, it's working perfectly 😊

Twitter pics please! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants