Skip to content

Flipped triangles when simplifying mesh #346

Closed
@andreiKDI

Description

Hi!
First of all, thank you for working on this great tool, much appreciated! 👍

I'm trying to simplify some terrain tiles which are basically just grids with a bit of elevation. The problem is that sometimes, the simplification process generates weird meshes, with some triangles flipped, others overlapping and some very thin. You can see all three of them in the "after.obj" attached.
Here's the code I use for simplifying the mesh, although the same happens without the index and vertex remapping, just that the buffers will be bigger. Also, I tried tweaking the threshold and targetError, with similar results.

    // Simplify mesh
    float threshold = 0.2f;
    size_t targetIndexCount = size_t(indexes.size() * threshold);
    float targetError = 1e-2f;

    indexes.resize(meshopt_simplify(indexes.data(), indexes.data(), indexes.size(), &(positions[0].x), positions.size(), sizeof(glm::vec3), targetIndexCount, targetError, nullptr));

    std::vector<unsigned int> vertexRemap(positions.size());
    size_t newVertSize = meshopt_optimizeVertexFetchRemap(vertexRemap.data(), indexes.data(), indexes.size(), vertexRemap.size());
    vertexRemap.resize(newVertSize);

    meshopt_remapIndexBuffer(indexes.data(), indexes.data(), indexes.size(), vertexRemap.data());
    meshopt_remapVertexBuffer(positions.data(), positions.data(), positions.size(), sizeof(glm::vec3), vertexRemap.data());
    meshopt_remapVertexBuffer(texCoords.data(), texCoords.data(), texCoords.size(), sizeof(glm::vec2), vertexRemap.data());
    positions.resize(newVertSize);
    texCoords.resize(newVertSize);

Here you can find the before and after meshes with the fewest grid cells I could reproduce the problem with and without normals, uvs etc: Desktop.zip

I briefly tried to debug the problem myself, but it wasn't obvious to me what happens there. Please let me know if you need more details.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions