It is currently Thu Dec 12, 2019 6:27 am


All times are UTC




Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: New to polyvox and game development, looking for examples.
PostPosted: Fri Sep 30, 2016 7:28 am 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
First of all, thank you for inviting me to the forum. I've been spending all my free time on this project and I love what you have made here.

Please excuse me if this question has already been addressed, I tried my best to read over the existing forum posts.

I've been working with PolyVox for a couple months now. At first I was using Cubiquity for unity3d, and now I've switched to using PolyVox directly, with UE4. I've had some great success generating endless terrain with the blocky minecraft style, but I'd like to generate a smooth terrain, like the one shown in the Cubiquity plugin. I've been trying for many days now to generate a smooth sphere.

Using the marching cubes method, I keep getting results like this:
http://imgur.com/a/TGrZ4

And I want results like this:
http://www.cubiquity.net/cubiquity-for-unity3d/1.2/docs/smooth-terrain.jpg

I experimented with the LowPassFilter, and it did seem to smooth it a little, but I can never get it to be round like a sphere. I guess I don't understand how the density of each voxel effects the outcome of the mesh.

Could anyone provide a short block of code to generate the voxel density values for a sphere? This is what I was trying but it doesnt seem to work.


Code:
VoxelData_sp createSphereInVolume(const PolyVox::Region& region)
   {
      const int32_t xSize = region.getUpperX() - region.getLowerX() + 1;
      const int32_t ySize = region.getUpperY() - region.getLowerY() + 1;
      const int32_t zSize = region.getUpperZ() - region.getLowerZ() + 1;
      float fRadius = xSize / 2.2;
      VoxelData_sp voxelData = MakeShareable(new VoxelData(xSize, ySize, zSize));
      Vector3DFloat v3dVolCenter(xSize / 2, ySize / 2, zSize / 2);
      const float innerBoundary = 0.1f * fRadius;
      for (int z = 0; z < zSize; z++)
      {
         for (int y = 0; y < ySize; y++)
         {
            for (int x = 0; x < xSize; x++)
            {
               Vector3DFloat v3dCurrentPos(x, y, z);
               float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
               MDP88 Voxel;
               if (fDistToCenter <= fRadius && fDistToCenter >= (innerBoundary))
               {
                  Voxel.setDensity(255 * (5 * (fDistToCenter / fRadius) - 4));
               }
               else if (fDistToCenter <= (innerBoundary)) {
                  Voxel.setDensity(255);
               }
               else {
                  Voxel.setDensity(0);               
               }
               Voxel.setMaterial(3);               
               voxelData->at(x, y, z) = Voxel;
            }
         }
      }
      return voxelData;
   }


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Sat Oct 01, 2016 8:38 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Working with density fields can be complex and unintuitive, and editing them is even worse (it took me a lot of thought to get the editing tools working in Cubiquity). Even generating them from heightmaps is surprisingly tricky, and I think this is the reason why most Marching Cubes terrain demos just use Perlin noise (which is quite easy but boring).

Sharp transitions are likely to be resulting from your if/else statements, though I can can see you are making some effort to make the transition smooth. Here is a basic sphere which worked for me:

Code:
void createSphereInVolume(RawVolume<uint8_t>& volData, float fRadius)
{
   //This vector hold the position of the center of the volume
   Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);

   //This three-level for loop iterates over every voxel in the volume
   for (int z = 0; z < volData.getDepth(); z++)
   {
      for (int y = 0; y < volData.getHeight(); y++)
      {
         for (int x = 0; x < volData.getWidth(); x++)
         {
            //Store our current position as a vector...
            Vector3DFloat v3dCurrentPos(x, y, z);
            //And compute how far the current position is from the center of the volume
            float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();

            uint8_t uVoxelValue = 0;

            float val = fRadius - fDistToCenter; // val is positive when inside sphere
            val = PolyVox::clamp(val, -1.0f, 1.0f); // val is between -1.0 and 1.0
            val += 1.0f; // val is between 0.0 and 2.0
            val *= 127.5f; // val is between 0.0 and 255

            // Cast to int
            uVoxelValue = static_cast<uint8_t>(val);

            //Wrte the voxel value into the volume   
            volData.setVoxel(x, y, z, uVoxelValue);
         }
      }
   }
}


Note that this worked ok for a sphere of radius 30 and that I am using uint_8 values for the voxel density. In practice you might find it easier to work with float data (though it is bigger of course) as this gives you more range and would let you adjust the size of the sphere by changing the threshold, instead of adapting the data.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Sun Oct 02, 2016 4:19 pm 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
Your solution works great, thank you. I set the threshold to 127 (half of the uint8_t range), and I got great results. Here's a sphere with a radius of 8 voxels. The LowPassFilter was not used here.

http://imgur.com/a/ra3g8

Now I just need to figure out how to get ANL to generate smooth transitions like this for my procedurally generated terrain :)

I'll report back with my results at a later date, and source code for anyone else who is interested.

Thanks again David!


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Sun Oct 02, 2016 10:14 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Great!

mgumley wrote:
Now I just need to figure out how to get ANL to generate smooth transitions like this for my procedurally generated terrain :)


Smooth terrain should be easy with a noise library. The noise will be -1.0 to 1.0 (or maybe 0.0 to 1.0?) and you just need to scale this to the 0-255 range and set the threshold to 127.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Wed Oct 05, 2016 5:11 pm 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
Things are going quite well with my endless terrain project. I've included some screenshots here. The ocean doesn't use PolyVox, but it looks nice :)

Image

I did notice some strange artifacts in the terrain mesh. In the screenshots, it's shown how the grass texture buckles and looks like a darker green when the mesh gets compressed on some of the contour lines. Is this normal for PolyVox? or is this caused by the way I'm generating the data with ANL?

Screenshots of my project:
http://imgur.com/a/kejNN

source code for my voxel value generation:
http://pastebin.com/Mq1GnqTE

Librarys used:

Unreal Engine 4.13

ANL
http://accidentalnoise.sourceforge.net/

OceanProject (cascade256 4.13 fork)
https://github.com/cascade256/OceanProject/tree/4.13

PolyVox
https://bitbucket.org/volumesoffun/poly ... ch/develop


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Thu Oct 06, 2016 10:45 pm 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
Looking good! I only skimmed you code but this struck me as odd:

Code:
bool bSolid = EvaluatedNoise >= 127;
Voxel.setDensity(EvaluatedNoise);


Aren't you adding a discontinuity to the data here, so that it jumps from 0 to 127 and then smoothly increases to 255? Can you remove that condition? You values should transition smoothly from 0 to 255, and PolyVox will then generate the mesh where they cross over 127.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Fri Oct 07, 2016 12:17 am 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
David, I think you were correct in pointing out the error about my threshold, but it wasn't enough to fix it. I really appreciate your help with this.

I posted another screenshot, just showing the wireframe of a sphere that you helped me make earlier. The buckling effect is there too. Is there anything that can be done to make it evenly spaced polygons?

Sorry for being so terse, I'm late for a class!

Image


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Sun Oct 09, 2016 8:21 am 
Developer
User avatar

Joined: Sun May 04, 2008 6:35 pm
Posts: 1827
I think it is important to clarify whether you are concerned about the geometry of the meshes of the lighting. In the terrain image you posted I see that there are light and dark stripes running along the hillside to the right of the image. However, I would be surprised if this was a problem with the geometry and is more likely a problem with the normals causing incorrect lighting.

The sphere is only wireframe, so I assume you are concerned about the presence of long, thin, triangles when the majority of triangles have much more equal side length? I don't think this is a cause for concern and is a valid output of the Marching Cubes algorithm.

For the banding effect in the terrain image you should first disable texturing (or just use a white texture?) on your materials to make sure there are not problems with texture aliasing. Then you should try disabling lighting. Do the artefacts disappear? If you want to get fancy, you could try writing the XYZ components of the normal into the RGB of the pixel to give a visualization of the normals. This would let you see any normal errors.

In terms of fixing it, try the approach described in this comment in the code: https://bitbucket.org/volumesoffun/poly ... or.inl-150

Otherwise you could try a floating point volume rather than an integer one, or you could even discard PolyVox's normals completely and compute them yourself as the average of adjacent faces.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Tue Oct 11, 2016 4:35 pm 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
you're right about the lightning. The problem is less noticeable with the lightning disabled. Here's some screenshots show that:

http://imgur.com/a/g7Yv5

It's possible my normals are not being computed right. I modified some code from a tutorial for the normals calculations. Here's the code:

http://pastebin.com/3S4EsjgC

The tutorial designed it for a minecraft-style cube mesh, maybe thats my problem. I'm going to try to change the code to use the normals provided by Polyvox.

also,
Using the sobel method didn't have any noticeable effect.
I haven't yet tried using floating points for the voxels.

Here's a video showing where I'm at right now:
https://youtu.be/hhwmsSBDI3Y

I'll report back with results soon.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: New to polyvox and game development, looking for example
PostPosted: Sat Oct 15, 2016 12:27 am 

Joined: Fri Sep 30, 2016 3:30 am
Posts: 14
Yep you were certainly right about the normals. I changed it to use the normals from Polyvox. The other normals I was using are good for cubes with different textures on each side, but bad for smooth terrain. The texture mappings are not perfect, but they are much better now. I'm also using a different grass texture, but the texture isn't the thing causing the noticeable change. Here's a screenshot of my latest build:

Image

http://imgur.com/C1u2gIb

Now I just need to figure out how to blend the materials between grass, dirt, and stone. :)

I'll let ya know what I find.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net