Generating from mesh?

Questions & AnswersCategory: ProgramingGenerating from mesh?
KennyRoy asked 2 years ago

I am trying to generate a terrain from a custom mesh, e,g. a torus.  I’m starting from the SingleCube3DGeneratorModule class.  In the SingleCube3DGeneratorModule the point’s distance is subtracted from the unit sphere’s size and returned as the density.  Therefore, it seems that uTerrains (and pretty much every terrain mesher) draws the terrain at the point where density transitions from NEGATIVE TO POSITIVE.  Is this correct?  
density
So, for a torus, without doing raycasting, it’s very, very difficult for me to generate values that graciously transition from negative to positive. (I can test if a v3 point is inside a mesh without collisions or raycasting – necessary because uTerrains seems to be creating threads for generation and Raycasting and collisions have to run on the main thread).  I can very easily put a negative value inside the mesh, and a positive value outside, but for some reason this doesn’t create any geometry.  Can you think what could be happening?
 
 
 
 

3 Answers
KennyRoy answered 2 years ago

I am trying to edit my question but I can’t. 
Do I need to use the UnitConverter when generating the mesh?  Or is the float x, y, z, that are checked in the SingleCube3DGeneratorModule for example in Unity worldspace? 

uTerrains Staff replied 2 years ago

The floats x y z in generators functions are in voxel unit. So yes, you’ll probably need to convert it to Unity unit. But I doubt this will solve your initial problem because if you didn’t change anything the size of a voxel is 1 (so the conversion won’t change anything even if you should do it).

KennyRoy replied 2 years ago

I figured out what is going wrong. All the meshes I’ve tested have been at v3.zero and scale of 1. I was not transforming the vert object space to world space when testing the points. I fixed it, but now there seems to be a wierd issue with the normals of the vertices of a Unity cube.

https://imgur.com/a/k1hKgl0

I’m going to try to make my own cube with no shared vertices and see what happens

KennyRoy replied 2 years ago

So my alogrithm is based off a paper I found and it has a tolerance factor that is based on a calculation of a certain radius after all the vertex interior angles are calculated. Anyway, long story short, it seems the Unity cube does have odd multiple verts, and I need to work with the tolerance setting to get the right effect. Here’s the smallest polyhedron possible (4 verts) and aside from some oddness based on the tolerance, it looks like we’re getting somewhere now.

KennyRoy replied 2 years ago

Opps here’s the image https://imgur.com/a/AIsX0xY

KennyRoy replied 2 years ago

♫♫ Look what I diiiiiiiiid……. ♫♫♫

https://imgur.com/a/1MZDhxG

KennyRoy replied 2 years ago

Smoothing and intersections added…
https://imgur.com/a/7rIgpyk

uTerrains Staff replied 2 years ago

Excellent!!! You’ve made a great job here! Would you be agree to share your work (ie. your generator) so I can include it in Ultimate Terrains package? (of course don’t feel obliged to say yes. This is your work)

uTerrains Staff answered 2 years ago

Therefore, it seems that uTerrains (and pretty much every terrain mesher) draws the terrain at the point where density transitions from NEGATIVE TO POSITIVE. Is this correct?

Yes, this is correct.

I can test if a v3 point is inside a mesh without collisions or raycasting – necessary because uTerrains seems to be creating threads for generation and Raycasting and collisions have to run on the main thread

Yes indeed, generators are called from threads (I have to make that clear in the upcoming documentation).

I can very easily put a negative value inside the mesh, and a positive value outside, but for some reason this doesn’t create any geometry. Can you think what could be happening?

It’s hard to tell. What you describe should work. If you are able to tell that a given 3d point in space is inside or outside (ie. negative or positive value) the torus, this should work. If you want a perfect torus, your voxels’ values have to be equal to the (min) distance between the voxel and the surface of the torus.

Be sure that your camera is close to where the torus should be generated (to be sure it’s within terrain draw distance). Be sure also that your torus is big/small enough.

Let me know if you manage to make it work or if you need more help (but in this case I will probably need to see your code).

KennyRoy answered 2 years ago

AH I see there are some classes for converting from voxel and chunk position to world position.   Which should I use within:

public float GetValue (float x, float y, float z, float[] values2D)
{
(my point testing code within mesh verts in world space)
}