Conviniece function for normal mapping

This is a convenience method I made for normal mapping, Specifically for blender models. If anyone wants to use it.

public static List<Geometry> getGeometries(Node n) {
        List<Geometry> gl = new ArrayList();
        for (Spatial s : n.getChildren()) {
            if (s instanceof Node) {
                gl.addAll(getGeometries((Node) s));
            } else if (s instanceof Geometry) {
                gl.add((Geometry) s);
            }
        }
        return gl;
    }
    public static void makeNormalReady(Spatial s) {
        List<Geometry> gl = null;
        if (s instanceof Geometry) {
            gl = new ArrayList();
        } else if (s instanceof Node) {
            gl = getGeometries((Node) s);
        }
        for (Geometry g : gl) {
            if (g.getMesh().getBuffer(VertexBuffer.Type.TexCoord) != null) TangentBinormalGenerator.generate(g.getMesh());
        }
    }

(I think this is the right subforum)

Or… you know… just call:
TangentBinormalGenerator.generate(n);
https://javadoc.jmonkeyengine.org/com/jme3/util/TangentBinormalGenerator.html#generate-com.jme3.scene.Spatial-

…and let it do all of that for you. It also checks for a normal buffer which is also required.

By the way, next time you need to “visit every Geometry in a tree”, you might want to take a look at the scene graph visitor stuff. Even if TangentBinormalGenerator didn’t already do what you have manually done, the code could have been as simple as:

n.depthFirstTraversal(new SceneGraphVisitorAdapter() {
        public void visit( Geometry g ) {
            if (g.getMesh().getBuffer(VertexBuffer.Type.TexCoord) != null) {
                TangentBinormalGenerator.generate(g.getMesh());
            }
        }
    });

…but as said, the generator already does all of the work for you.

I was wondering if there was an easier way of doing this… thanks though

1 Like