Giter Club home page Giter Club logo

Comments (3)

rms80 avatar rms80 commented on September 28, 2024

I have not tried your code, but from what you have described, my guess is that the unity cube is actually 6 separate planes (almost certain about this). The plane-cut can work on that, but the result is still separate mesh patches. To do a hole-fill, the cube faces need to be welded along the borders.

An easy way to do this is to add the unity Mesh vertices to a Dictionary, where you assign a new vertex id only if the vertex is not already in the dictionary (my Vertex3d can be used as a Dictionary key). Then your mapV will map some input-triangle vertices to the same output vertex, and the result will be welded together.

This will break UVs, though. If you need to preserve UVs through this...that is more complicated.

from geometry3sharp.

nyblomper avatar nyblomper commented on September 28, 2024

Ahh, right. This must be the case since all the sides of the Unity cube have separate normals. So it should be possible to create a loop out of those CutSpans I guess.
I'll probably just go with your solution and always process the mesh as "closed". Then I'll break it up to create separate faces if needed.
Thanks!

from geometry3sharp.

nyblomper avatar nyblomper commented on September 28, 2024

It works :)

Ran into some numerical issues during cutting, but nothing that couldn't been fixed with small offsets.

Here is the code for creating the merged mesh if anyone is interested:

public static DMesh3 ToDMesh3Merged(this Mesh mesh) {
    DMesh3 result = new DMesh3(bWantNormals: false);

    var verts = mesh.vertices;
    var tris = mesh.triangles;

    Dictionary<Vector3d, int> vertexHashToUnityVertexIndex = new Dictionary<Vector3d, int>();

    int[] unityVertexIndexToDMeshVertexIndex = new int[verts.Length];

    for (int i = 0; i < verts.Length; i++) {
        var vert = (Vector3d) verts[i];

        if (vertexHashToUnityVertexIndex.ContainsKey(vert)) {
            var mergedUnityVertexIndex = vertexHashToUnityVertexIndex[vert];
            unityVertexIndexToDMeshVertexIndex[i] = unityVertexIndexToDMeshVertexIndex[mergedUnityVertexIndex];
        } else {
            int vertId = result.AppendVertex(
                new NewVertexInfo {
                    v = vert,
                }
            );
            vertexHashToUnityVertexIndex[vert] = i;
            unityVertexIndexToDMeshVertexIndex[i] = vertId;
        }
    }

    for (int i = 0; i < tris.Length; i += 3) {
        result.AppendTriangle(
            unityVertexIndexToDMeshVertexIndex[tris[i]], 
            unityVertexIndexToDMeshVertexIndex[tris[i+1]], 
            unityVertexIndexToDMeshVertexIndex[tris[i+2]]);
    }

    return result;
}

Also, here is some code to split all vertices, if you want to do some flat-shaded stuff:

public static DMesh3 ToSplittedDMesh3(this DMesh3 dm) {

    DMesh3 result = new DMesh3(bWantNormals: false);

    foreach (var triIndex in dm.TriangleIndices()) {
        var tri = dm.GetTriangle(triIndex);

        var a = result.AppendVertex(dm.GetVertex(tri.a));
        var b = result.AppendVertex(dm.GetVertex(tri.b));
        var c = result.AppendVertex(dm.GetVertex(tri.c));
            
        result.AppendTriangle(a, b, c);
    }

    return result;

}

from geometry3sharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.