0

I've been working on an implementation of Transvoxel since while. Now it works, it polygonizes, but it's kind of blocky. Like the image below:

enter image description here

This is the code for polygonization:

private static Mesh PolygonizeRegularCell (Vector3i pos, Mesh mesh, int lodIndex, Chunk samples)
{
    int lod = 1 << lodIndex;

    for (int i = 0; i < density.Length; i++) {
        density [i] = samples [pos + Transvoxel.cornerIndex [i] * lod].Density;
    }

    byte caseCode = getCaseCode(density);

    if ((caseCode ^ ((density [7] >> 7) & 0xFF)) != 0) {
        byte cellClass = Transvoxel.regularCellClass [caseCode];
        vertexLocations = Transvoxel.regularVertexData [caseCode];

        Transvoxel.RegularCell c = Transvoxel.regularCellData [cellClass];
        vertexCount = (long)c.GetVertexCount ();
        triangleCount = (long)c.GetTriangleCount ();
        indexOffset = c.Indizes ();

        localVertexMapping = new int[indexOffset.Length];

        for (int i = 0; i < vertexCount; i++) {
            ushort edgeCode = (ushort)(vertexLocations [i] & 0xFF);

            byte v0 = (byte)((edgeCode >> 4) & 0x0F);
            byte v1 = (byte)(edgeCode & 0x0F);

            Vector3 p0 = (pos + Transvoxel.cornerIndex [v0] * lod).ToVector3 ();
            Vector3 p1 = (pos + Transvoxel.cornerIndex [v1] * lod).ToVector3 ();

            long t = (density [v1] << 8) / (density [v1] - density [v0]);
            long u = 0x0100 - t;

            localVertexMapping [i] = vertices.Count;
            Vector3 Q = p0 * t + p1 * u;
            Q /= 256f;
            AddVertex (Q, samples [pos.x, pos.y, pos.z].block);
        }

        for (int t = 0; t < triangleCount; t++) {
            for (int i = 0; i < 3; i++) {
                triangles.Add (localVertexMapping [c.Indizes () [t * 3 + i]]);
            }
        }
    }
    return mesh;
}

And here is another one for generating heightmap and density values:

public void GenerateNewDensityField (int x, int z)
{
    densityColumn = new sbyte[128];
    noise = (float)(Noise.noise (x * 0.01f, z * 0.01f) * 0.25f);
    fHeight = ((noise + 1f) * 54f) + worldSize;
    height = Mathf.FloorToInt (fHeight);
    for (int i = 0; i < densityColumn.Length; i++) {
        if (i < height - 1)
            densityColumn [i] = -128;
        else if (i == height - 1)
            densityColumn [height - 1] = (sbyte)Mathf.Clamp (height - 128, -127, 128);
        else if (i == height)
            densityColumn [height] = (sbyte)Mathf.Clamp (-height, -128, 127);
        else if (i == height + 1)
            densityColumn [height + 1] = (sbyte)Mathf.Clamp (127 - height, -128, 127);
        else
            densityColumn [i] = 127;
    }

    densityCache [x, z] = new DensityColumn (densityColumn, height);
}

If you have used Transvoxel before or you have any idea about this then please comment or answer. I'm really stuck with this.

EDIT: Changes in density generation code, to not to generate too much zeros and so on, but it's still blocky.

Community
  • 1
  • 1
Statey
  • 631
  • 5
  • 10

1 Answers1

1

I haven't looked at your code in detail, but the blocky pattern in your screenshot is reminiscent of imprecise voxel density values. You would see this pattern, for example, if all voxel data values were -1.0 or +1.0 (or -127 and +127 as integer data) without anything in between. The voxels should represent the distance to the surface in units of a single voxel width, with +127 meaning the data point is one voxel outside the surface and -127 meaning the data point is one voxel inside the surface. You may just need to rescale your data accordingly, and then you'll see smooth results.

  • I'm generating a heightmap then i just subtract the y coordinate of the cell from the height at a given column. – Statey Oct 25 '17 at 19:38
  • Correct me if I'm wrong (you know more about transvoxel than me thats more than sure), but as I know this formula has to generate a smooth terrain.. – Statey Oct 25 '17 at 21:47
  • First, make sure that your subtracted y coordinates are not too large and getting clamped to the maximum extremes all the time. Second, note that just subtracting heights is not sufficient for generating the correct voxel values because the distance to the surface may be less in a direction that has a horizontal component. – Eric Lengyel Oct 26 '17 at 04:53
  • What do you mean? You can see in the second shorter code the way I calculate density values can you explain me little bit what is wrong with it? – Statey Oct 26 '17 at 08:55
  • I just don't really understand what do you mean under the distance could be less when there is a horizontal component. – Statey Oct 28 '17 at 09:03
  • can you help me with this? – Statey Oct 29 '17 at 08:59
  • why doesn't subtracting the height from y give a correct number? For example a voxel at the height of 43 and the surface is at the given (x,z) is 38 then the cells density is 43-38=5, isn't it? – Statey Dec 03 '17 at 14:18