Extrusion algorithm

Hi!



Does anyone have a good extrusion algorithm? By extrusion I mean moving every vertex in the model along its normal.



The purpose is to create a "shell" around the model, kind of like a layer of clothes.



I need to be able to control the distance between original and target position of the triangles, rather than the vertices.



This means that the needed amount of extrusion of the vertex changes according to the relation of the angles of the surrounding triangles.

Thanks!



Some meshes have double vertices, like Box f.ex. Those vertices normals point "their own" way,  so I have made a function who merges double vertices. After that the mesh has no normals, so I have to generate them.



I have found NormalGenerator in JME2.0. That does the job. Not completely satisfactory though. If I run a double-vertices-merged box through it, the normals in the corners are a bit off, not pointing where they should. I'm guessing NormalGenerator doesn't take in to the account that some faces are made up of two triangles. I've had the same results when generating normals myself.



My main problem is getting the right distance between the original face to the extruded face. If I extrude a vertex in the corner of a box, the distance between the original and the extruded face will be less than for a vertex on f.ex a sphere, where the angular difference between the surrounding triangles are smaller.

This is what I have so far:

public static TriMesh getExtrudedTriMesh(TriMesh triMesh, float extrusion) {
        Vector3f[] sourceVertexBuffer = BufferUtils.getVector3Array(triMesh.getVertexBuffer());
        IntBuffer sourceIndexBuffer = triMesh.getIndexBuffer();
        Vector<Triangle> triangles = new Vector<Triangle>();
        Vector3f[] targetVertexBuffer = new Vector3f[sourceVertexBuffer.length];

        for (int i = 0; i < sourceVertexBuffer.length; i++) {
            triangles.clear();
            sourceIndexBuffer.rewind();
            while (sourceIndexBuffer.hasRemaining()) {
                int a = sourceIndexBuffer.get();
                int b = sourceIndexBuffer.get();
                int c = sourceIndexBuffer.get();
                if (a == i || b == i || c == i) {
                    Triangle triangle = new Triangle(sourceVertexBuffer[a], sourceVertexBuffer[b], sourceVertexBuffer[c]);
                    triangles.addElement(triangle);
                }
            }
            Vector3f[] normalsVertices = new Vector3f[triangles.size()];
            for (int j = 0; j < triangles.size(); j++) {
                Triangle triangle = triangles.elementAt(j);
                triangle.calculateNormal();
                normalsVertices[j] = triangle.getNormal();
            }
            Vector3f addition = polygon_centroid_3d(normalsVertices);
            targetVertexBuffer[i] = sourceVertexBuffer[i].add(addition.mult(extrusion));
        }
        return new TriMesh("Extruded " + triMesh.getName(), BufferUtils.createFloatBuffer(targetVertexBuffer), null, null, null, sourceIndexBuffer);
    }



polygon_centroid_3d(normalsVertices); means getting the center of that polygon that the normals form.

The problem with this code is that the distance between the original faces and the extruded varies, depending on the angles of the triangles that surround the vertex, like I said in the previous post.

Don't try to recalc All the triangles. And dont generate the Normal yourself.



You just need every Vertex itself, and its Normal, from the normalBuffer. Then its always the movedirection, you want