# Faster TangentBinormalGenerator for grid terrain

Hi guys !

I’m working on my terrain editor and I’m a bit annoyed by the TangentBinormalGenerator. Each time I update the terrain mesh (meaning each frame when you sculpt the surface), I have to call that method on my modified mesh :

TangentBinormalGenerator.generate(myNewMesh)

But this is very slow, even with my tiny 10*10 parcels. I’m not looking for optimisation at all costs, you know that, but I have the strong feeling that for the classic grid terrain, the binormal generation is a very simple and fast computation.

Before hacking into it, I would like to ask if you guys have by any chance a specific implementation of that code for terrain? Or some hint to implement it simply? I admit I am not comfortable with all those maths.

Thanks !

Edit : For the record I need binormals for normal maps drawing.

Note: you need either tangents OR binormals… generally JME uses tangents and you’d probably be better off doing the same.

Honestly, I thought JME’s terrain already had tangents… but maybe it calculates them in the shader. As you say, they are trivial to calculate for heightmapped terrain given the normal… so maybe the shaders do it automatically and save the huge cost of the tangent buffer.

1 Like

Interesting. I didn’t see any specific code in the terrain framework but I haven’t searched into the shader. I’ll try to find the code and put it into my own terrain implementation.

Pick which way you want tangents to face “east/west” or “north/south”.

Then it’s something like:

vec3 tangent = cross(normal, vec3(0, 0, 1)); // faces west I think
vec3 binormal = cross(tangent, normal); // faces south I think

I may have the arguments to one or both of those cross products backwards as I can never confidently remember which produces what in whatever left/right handed coordinate system we are in. (The down sides of having done this for a long, used various coordinate systems, and find it easy enough to try one then the other.)