Re: tangent maps

Date : Thu, 25 Jan 2007 17:32:02 +0100
To : XSI(at)Softimage.COM
From : André Adam <a_adam(at)49games.de>
Subject : Re: tangent maps
Hi Ben,

thanks for the kind words! Very nice work on the displacement mapping!
A vertex colour property stores float values, not 8bit data, so you're practically lossless when converting back to vectors. The smoothing works similar, since it is angle based, but is not connected in any way to the geo approximation or the normals. In theory, the normal map should be able to compensate cracks in the tangent space, but practically the normal map's texels would have to line up perfectly with the polygon edges which is a very unlikely occasion. So, as soon as you try to fit a normal map texture with its pixel raster onto uneven 3d geometry, the tangent space needs to be smoothed to avoid cracks and seams.


I've attached the script to this mails, but really, this is nothing fancy, just a one-time creation of null objects pointing in the vector's direction. A compiled operator with live-input certainly would be way nicer. Oh, and it is slow, be careful with meshes heavier than - say - a thousand polygons, trying it out on small objects will be more fun... :)

Cheers!

   -André


Ben Rogall wrote:

Andre, very very nice explanation. I've been using XSI tangents recently in experiments with tangent space vector displacement:
http://www.xsibase.com/forum/index.php?board=29;action=display;threadid=28834;start=0
It took me a little while to realize I had to do the [0,1] to [-1,1] interval correction. I'm also curious to see whether having only 8 bits per channel for the tangent vector is enough to avoid jumping though I haven't seen anything obvious yet.


Another issue with tangent space is difficulty when you try to get different 3D applications to work with eachother. Once you have the normal and tangent vectors, there are still several ways to determine your T,B,N space depending on which of these vectors you decide to orthoganalize to the others. This choice has to match between the applications.

I hadn't even thought about the smoothing issue you mentioned. Is that analogous to the normal automatic discontinuity control?

Also is your tangent display utility publicly available?

Thanks,
Ben


--- On Thu 01/25, =?ISO-8859-1?Q?Andr=E9_Adam?= < a_adam(at)49games.de > wrote:


    *From: *=?ISO-8859-1?Q?Andr=E9_Adam?= [mailto: a_adam(at)49games.de]
    *To: *XSI(at)Softimage.COM
    *Date: *Thu, 25 Jan 2007 11:20:09 +0100
    *Subject: *Re: tangent maps

    Hi Bernard,

    the tangent property in XSI is the so called tangent map, or tangent
    space. Encoded in there you can find a given texture space's
    u-direction
    (the reason why you have to reference a texture projection when
    creating
    a tangent property) in 3d space (you could call this the "texture
    mapped" u-direction) on a per sample basis, so we're talking of
    vectors
    here. In absence of a more convenient property Softimage decided to
    encode the tangents into vertex colors. That means you have a
    little bit
    of math to do to revert them back to vector data: (VC-0.5)*2. This
    formula stretches the RGB vertex color values, which have a 0-1
    range,
    back to a vector's -1 to +1 range.

    The reason you need a tangent space when dealing with normal maps is
    that a "normal map in tangent space" (there is also an object
    space flavour)
    stores relative information as to where a given normal should be
    bent,
    which after all is what bump mapping is all about (bump mapping and
    normal mappinng are practically the same thing). The lila-blueish
    color
    in a tangent space normal map means that the geometry's input normal
    should not be altered, where cyan, magenta, etc colours indicated a
    shift in normal orientation.
    This shift, which is a relative bit of information as it does not
    point
    to global world coordinates, needs a reference on the object
    itself from
    which the relative shift should be calculated - the tangent space.
    The
    tangent space offers a predefined vector for each sample point and is
    directly generated from the texture projection the normal map is
    mapped
    with onto the object.

    I've written a small utility that creates little pointers from the
    tangent space right on the object's surface to better show the
    vectors
    encoded there. I've attached two screenshots which might clearify
    that a
    bit. The first one shows a straight spherical mapping, the second
    one is
    twisted. As soon as a texture space shows distortions (or even worse:
    seams), just like the second one, tangent spaces become tricky; the
    samples for a single vertex point in different directions, which
    finally
    would create artifacts during normal mapping. To cope with this issue
    the tangent operator in XSI has a smoothing value, which basically
    middles the different sample's tangent vectors using an angle-based
    approach.

    Since the normal map stores its data relative to the tangent space,
    generation, as done with the ultimapper for example, therefore always
    needs to reference it during generation to provide correct results.
    Ultimapper basically marries referenced object curvature from a high
    resolution source object with the normals of the low resolution
    source
    object and the tangent space of the low resolution object.

    The Photoshop plugins and tools provided by graphics hardware vendors
    don't have this kind of 3d information available when generating
    normal
    maps from heightmaps. They assume that the object the generated
    normal
    map might get mapped onto shows an ideal tangent space, which
    usually is
    nonsense. The more distorted the texture space on the mapped
    object is,
    the wronger (is that english?!) the bump mapping results with such a
    textures are. I'd stay away from these tools for production work.

    Hope that makes some sense, cheers!

    -André

  /*========================================================
    "Show Tangents" / André Adam
    Tool to create null object pointers on an polymesh object's surface
    to visualise direction in a tangent space.
  ========================================================*/
  
  
  function fGetPointer(oObj, oSize){
    var oNull, oDisplay
    oNull = oObj.AddNull();
    oNull.Parameters("Primary_Icon").Value = 9;
    oNull.Parameters("Size").Value = oSize;
    oDisplay = oNull.AddProperty("Display Property");
    oDisplay.Parameters("wirecol").Value = Math.round(Math.random()*1023);
    return oNull;
  }
  
  function fGetTangents(oPolyMsh){
    var eVCs = new Enumerator(oPolyMsh.VertexColors);
    while(!eVCs.atEnd()){
      if(eVCs.item().Name == "Tangents"){
        return new VBArray(eVCs.item().Elements.Array).toArray();;
      }
      eVCs.moveNext();
    }
    return false;
  }
  
  function fGetSamplePositions(oPolyMsh){
    var aSamplePositions = new Array();
    var ePoints = new Enumerator(oPolyMsh.Vertices);
    for(var i=0; i<oPolyMsh.Samples.Count; i++){
      ePoints.moveFirst();
      while(!ePoints.atEnd()){
        var eSamples = new Enumerator(ePoints.item().Samples);
        while(!eSamples.atEnd()){
          if(eSamples.item().Index == i){
            aSamplePositions.push(ePoints.item().Position);
          }
          eSamples.moveNext();
        }
        ePoints.moveNext();
      }
    }
    return aSamplePositions;
  }
  
  function fShowTangents(oObj, oSize){
    var oPolyMsh = oObj.ActivePrimitive.Geometry;
    var aTangents = fGetTangents(oPolyMsh);
    if(aTangents){
      var aSamplePositions = fGetSamplePositions(oPolyMsh);
      for(var i=0; i<aTangents.length; i+=4){
        var vY = XSIMath.CreateVector3((aTangents[i]-0.5)*2, (aTangents[i+1]-0.5)*2, (aTangents[i+2]-0.5)*2);
        var vX = XSIMath.CreateVector3(0, 1, 0);
        var vZ = XSIMath.CreateVector3();
        vZ.Cross(vY, vX);
        vX.Cross(vY, vZ);
        vX.Normalize(vX);
        vY.Normalize(vY);
        vZ.Normalize(vZ);
        var oM3 = XSIMath.CreateMatrix3(vX.X, vX.Y, vX.Z, vY.X, vY.Y, vY.Z, vZ.X, vZ.Y, vZ.Z);
        var oTrans = XSIMath.CreateTransform();
        oTrans.SetRotationFromMatrix3(oM3);
        oTrans.SetTranslation(aSamplePositions[i/4]);
        oTrans.SetScalingFromValues(0.25, 1, 0.25);
        var oPointer = fGetPointer(oObj, oSize);
        oPointer.Kinematics.Local.Transform = oTrans;
      }
    }
    else{
      LogMessage("aa_ShowTangents: No tangents found on object!", siError);
    }
  }
  
  /*========================================================
    MAIN
  ========================================================*/
  
  if((Selection.Filter.Name == "object") && (Selection.Count == 1) && (Selection(0).Type == "polymsh")){
    fShowTangents(Selection(0), 1);
  }
  else{
    LogMessage("aa_ShowTangents: Not a single polymesh object selection!", siError);
  }

Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.