Volumes Of Fun

New to polyvox and game development, looking for examples.
Page 1 of 2

Author:  mgumley [ Fri Sep 30, 2016 7:28 am ]
Post subject:  New to polyvox and game development, looking for examples.

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:

And I want results like this:

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.

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)) {
               else {
               voxelData->at(x, y, z) = Voxel;
      return voxelData;

Author:  David Williams [ Sat Oct 01, 2016 8:38 am ]
Post subject:  Re: New to polyvox and game development, looking for example

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:

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.

Author:  mgumley [ Sun Oct 02, 2016 4:19 pm ]
Post subject:  Re: New to polyvox and game development, looking for example

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.


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!

Author:  David Williams [ Sun Oct 02, 2016 10:14 pm ]
Post subject:  Re: New to polyvox and game development, looking for example


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.

Author:  mgumley [ Wed Oct 05, 2016 5:11 pm ]
Post subject:  Re: New to polyvox and game development, looking for example

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 :)


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:

source code for my voxel value generation:

Librarys used:

Unreal Engine 4.13


OceanProject (cascade256 4.13 fork)

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

Author:  David Williams [ Thu Oct 06, 2016 10:45 pm ]
Post subject:  Re: New to polyvox and game development, looking for example

Looking good! I only skimmed you code but this struck me as odd:

bool bSolid = EvaluatedNoise >= 127;

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.

Author:  mgumley [ Fri Oct 07, 2016 12:17 am ]
Post subject:  Re: New to polyvox and game development, looking for example

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!


Author:  David Williams [ Sun Oct 09, 2016 8:21 am ]
Post subject:  Re: New to polyvox and game development, looking for example

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.

Author:  mgumley [ Tue Oct 11, 2016 4:35 pm ]
Post subject:  Re: New to polyvox and game development, looking for example

you're right about the lightning. The problem is less noticeable with the lightning disabled. Here's some screenshots show that:


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:


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.

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:

I'll report back with results soon.

Author:  mgumley [ Sat Oct 15, 2016 12:27 am ]
Post subject:  Re: New to polyvox and game development, looking for example

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:



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.

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group