Setting a voxel type on an isosurface

I’m trying to set the voxel type on an isosurface material, and I’ve been a little stuck for the past few days on how to best go about solving it.

I’m using an array of integers to specify the voxel type (which i then pass to the geometry). The voxel array is the same size as the isosurface DensityVolume array as seen in the isosurface demo @pspeed wrote. Basically this:

float[] densityArray; // size 19x19x19
int[] voxelArray; // same size as the density array - dictates what material to visualize.

So when I modify the density value in the densityArray, I also modify the same index in the voxelArray.

At first I use a noise-backed biome generator to define initial voxel material values (grass, sand, dirt, etc). This works fine. I then use what I call a brush to modify the terrain - as shown in the video below. The brush modifies the terrain fine. When it does this, I also modify the voxel types too. So if I modify index 12 on the densityArray, I modify index 12 on the voxelArray too. It works in theory, but I get a wierd bleeding issue on the negative side of all axis (left, bottom and back).

I know that the point of a voxel (it’s definition) should be at the center of the voxel, not the edge, and I’ve tried offsetting the values by 0.5f to cater for this but it doesn’t seem to make any difference. The negative sides are still set to the voxel they were - as if unchanged - and even more annoyingly - the intersection between them is always grass - like some kind of wierd interpolation that I can’t get my head around.

Can anyone give me a hint in the right direction?

How are you accessing them in the shader? Have you debugged that the values you are setting to the geometry are sensible?

Im passing the voxel data as texture coordinates on the mesh. The material is just a bog standard trilinear set up using a texture array. Nothing fancy. The voxel id from the texture coords chooses the array index in the frag shader.

Iv noticed that when i delete terrain, the positive sides of the shape (up, right and front) changes the edges of the deleted terrain to the voxel id i set. It appears as if the voxel data is shifted positively slightly :confused:

Im going to publish this on git as open source when iv solved this problem. I didnt want to publish semi-broken code, but maybe it would be easier if i did?

I don’t know that I’d have time to look at it myself right now, though. Maybe others would have insight but it starts to get pretty specific I guess.