i cannot seem to understand how to code sample an sdf. when i sample (0, 0, 0) and (1, 1, 1), they both give .301 red (even though both samples are defo outside of the true sdf volume. points that i know are inside give something close to .07, and ones that are outside, but closer then the edges are also .07 r, and the rest of the color values are 1, 1, 1). for a color of (.07, 1, 1, 1) for both points inside and outside the sdf when sampling the main branch.
heres my code that uses the sdf and samples it(https://paste.ofcode.org/3byc7F7YcQdeJPSTFvNvyXk) thank you guys for any help provided.
#How to Properly Sample a SDF Bilinearly
1 messages · Page 1 of 1 (latest)
Sdfs are usually defined by just one channel of data, with a predefined value being a threshold above(usually) which the space is considered inside the shape. The threshold is usually something simple like 0 or 0.5. In your case it's either a weird threshold with lower values inside the shape, or just an invalid sdf entirely.
Looking at the screenshot it says the format is r16 - just one channel of data.
first off thank you for the help dlich
i baked this sdf using the meshtoSDF VFX bake tool, then taking the data from the gpu and reading it into a 3d Texture then saving it. i can read the data as a sdf in editor, so i dont understand how im misunderstanding it this much. https://paste.ofcode.org/mMXNzhwJNrZ36XxbV8RmXr (heres the code creating and saving it, the reading part is above)
I did assume it was just an r value that if the value was below or equal to .5f it was inside the sdf, or if it was above .5f it was outside, but its defo not that
heres a picture of the sdf as a volume
heres an updated output, whiich probably means im somehow reading the SDF into a texture wrong.
This values and the screenshot actually make sense. You see that the color is darker in the center(the red component is lower), than at the edges. You see the whole 3d texture volume here. As for why it's not at the 0.5 threshold, I don't know. That's how it's created. Perhaps you can config it to provide different values.
https://docs.unity3d.com/Packages/com.unity.visualeffectgraph@15.0/api/UnityEngine.VFX.SDF.MeshToSDFBaker.-ctor.html#UnityEngine_VFX_SDF_MeshToSDFBaker__ctor_UnityEngine_Vector3_UnityEngine_Vector3_System_Int32_UnityEngine_Mesh_System_Int32_System_Single_System_Single_UnityEngine_Rendering_CommandBuffer_
I'm just using the SDF Baker tool with a threshold of .5f. maybe im misunderstanding just how a sdf works. the threshhold doesnt seem to change the actual value, just control what pixels are said to be in the field
Yeah, so you should use 0.5 as the threshold. Anything below is outside the mesh. Anything above is inside.
i did and thats how i got this baked texture. i used .5f,
i did try 1 and 0, and they made wierd partial edits to the sdf, but they didnt seem to change the value of any of the 3 color uvs i used to test
(i know that the thershold should be .5, just tested because i couldnt think of anything else to test).
Thank you again for helping.
I'd try using the baking and visualizing tools to confirm if the issue is with your baking or visualizing process.
im using that, just gotta edit my code to output the meshes as well (havent been saving the mesh)
What does it look like if you set the alpha to 0.5?
Ah, OK, I guess it compares the actual alpha channel.
Does the view in this sdf mode correspond to the actual mesh shape that you expect?
yes, it compares exactly to the actual mesh shape
a little off due to level of detail, but well enoughk
Then I'd assume the sdf is correct.
What is the actual use case for that sdf? Is there a problem during that time?
yes, i cannot sample it correctly, the values give back .31 to .7, and .7 can be both inside or outside the mesh
the use case is for fast runtime checking to see if a point is inside that volume
What does it look like with 0.5 used as a threshold in your use case?
thats what i have been using as a threshhold this entire time
Perhaps what you think is outside is actually inside the mesh?
the point at 0 0 0 is defo outside the mesh
the point at bottem center is defo inside the mesh
the points im check are for branches, and i know for a fact the first 8 are checking points inside the mesh, the 9th is outside the mesh,
Is the texture you're sampling, created as cpu readable?
yes, it threw an error when i tried to read it after making acidentally making it unreadable
the code is all present above if you need to take a look, the original "read" that isnt giving what i want is in the first post. the one that i included with my response has the code for reading the texture to a file, and the code for creating the meshes
Well, considering the visualization is correct, I can only assume that you're confusing something about the coordinates.
it says "normilized coordinates are coords between 0-1 in all axis's
you can do GetPixel, but im using GetPixelBilinear which takes u v and w, im guessing it coresponds to the same as 2d,, with z repersenting w and the width or length
What values are you using for bottom center though?
I'd try 0.5,0.5,0.5 just to be sure.
It's not impossible that there are some inaccuracies at the edges.
yes,
I see. I'd suggest testing with simpler shapes that you can confirm more easily. Like a sphere for example.
i meant like, i know for a fact that 000 and 111 are both outside
and they give .31
and if a value is outside but on the edge of a mesh, it should give back values just above .5f, to denote being close to the edge no?
thats the values for the first branch test, different from the others, but still not right
oh. this is an sdf.
Normally, I'd expect values outside to have the min value(0), even very close, but I guess it depends on the algorithm.
signed distance field, not a regular one. let me check
only part i hate is that it takes 20s to do a new check,
Again, sdf can mean many things and generated in many ways. You use sdfs for marching cubes as well, and the values there can be totally different to what you're getting.
alright, somethings wrong with my checking algorithm, but the signed distance field is from -1 to 1
What threshold value did you use to bake this though?
thank you so much for helping me, i defo feel dumb for not figuring it out sooner
.5f
im pretty sure the baker takes the .5f to bake a distance field, then remaps to -1 to 1
well now im pretty sure lol
that, was a really dumb problem i created, and i should of been able to figure it out.