Tangent space baking

Ethatron

Regular
Supporter
Hey;

I'm writing a height-/normal-map baker for for an orthogonal terrain mesh, so U is along world-x and V is along world-y. The mesh is passed to the shaders just with the vertex-normal and I calculate the TNB-matrix in the vertex shader.

Okay, I just have a bit of a feedback-loop trying to think how do I convert the object-space height-field and normal-map I already have, to the same tangent space which is used in the shaders.

In the vertex shader the camera- and the light-direction are transformed into tangent-space and then linearly interpolated over the face. Just to understand it: does the interpolated tangent-space light-vector cancel out any of the "virtual normal smoothing" over the face and the projection of the normal-map onto the face is in effect flat (constant over the face-normal)? If I'd interpolate the normal directly and modify my interpolated vertex-normal with the normal-map normal in the pixel-shader, using that to perturbate the un-tranformed light-vector in object-space, would that be exactly the same?
Sorry if I can't find the right words, I hope it's understandable.

If that would be the case, I can calculate the tangent-space normal map simply by converting all normal-map texels on the face-surface into the face tangent-space (not vertex tangent-space). Is that correct?

This would leave me with the height-field. Initially I was thinking to calculate the z-position of each heightmap-texel and subtract the face's z-position at the same coordinates. This would enable me to do view-space z-axis aligned reverse heightfield-tracing (position = float3(pos.x, pos.y, pos.z + texel)). Though I'd like to reuse the POM-code I have, so I ask myself what exactly would be the object-space to tangent-space transform for heightmap-texels.

If the texture-projection onto the face is flat (as in the case with the normals above), I could just cast a ray originating from the texels position on the face-surface and intersect it with the heightfield and write the length of the resulting vector to the height-map. Is that correct?

I think the case is rather simple, the UV coordinates are a top-projected regular square on a square terrain-mesh. I just strugge to mentally visualize the linearity or non-linearity of the texel-projection in tangent-space.

My biggest doubt is, that if I do this, won't I get normal-map normal discontinuities at the face-edges? Because there is an interruption from one face's tanged-space to another's.

Thanks
 
More Oblivion stuff? You gotta make sure to post back here when you're done with all this; I'm always looking for an excuse to return to Cyrodiil :)
 
Yeah, I managed to hacked the LOD system and now I want to supply tangent space normals-maps + heightmap for terrain-POM. The original system uses object-space normal-maps, which I can generate without problem. Dunno if normal-map baking is one of the more hard topics, but I have a hard time mentally imagine all the transforms that happen between the source heightmap and the final tangent-space lighting.

Here are some cookies: :)
LOD-Scattering-plus-SnowSpec-plus-tree-2k.png
 
I hate you. I hate you a lot right now. Not because you're a bad man or have done something bad -- far, far from it! My hate stems from your insight into my upcoming life where I will spend yet another 100 hours replaying Oblivion.

Here's hoping I can get sucked into Skyrim first so that the siren song of Oblivion looking like your screencap doesn't completely pwn my life :(
 
Hehe, but there are lot's of thing to solve, this just being one. The sun-lighting is an especially extensive "problem" because in the the end I have to take over all the game-internal lighting variables and it just concerns so much shaders. It's rough on the edges right now. You can make nice shots, but in other situations it obviously lacks completition.
 
So? No pointers to "The math of tangent space baking" by some PhD.?

Edit: Well going through some papers about some totally different topic (tesselation) I actually found a graphic in "Tesselation in a low poly world" by AMD. On page 36 they show a graphic which indeed would confirm that tangent space baking is done via the face's tangent space system (derived from UV and face-normal).
 
Last edited by a moderator:
Well I think I worked it out:

Face Normals:


Interpolated Vertex Normals:


The original Object-Space normal-map:


The new Tangent-Space normal-map:


I understood I have to use the interpolated normals and in reality that I can use which-ever TNB-transform as long as I use the same for the generation and for the shader. In this case the TNB is:

<code>
/* Tangent along X */
float3 tTangent = float3(1.0, 0.0, 0.0);
float3 BiNormal = cross(tTangent, IN.Normal.xyz);
float3 Tangent = cross(IN.Normal.xyz, BiNormal);
</code>

I transform the Object-Space normal-map via the inverse TNB-matrix. I know it's not right yet. I first have to do something with "old normal" vs. "interpolated normal", but I don't know what. Subtracting would come to mind ... though I think it should like detecting the angle between "old" and "intp." and build the difference normal from that ...
 
Uh, okay, I figured out all the math, it was a nice and enlightening excercise. Though because of this:

Heightfield-Problems.png

... I will completely dump the idea to do anything tangent-space with the terrain. Instead I'll implement POM z-axis aligned. It's not that difficult I think. I just do the all the calculation with untransformed vectors, EyeDir in world-space, height as (face.z + hmap.z). It's a bit more costy but what the heck, it should look magnificent.
 
Back
Top