Hello Guest

Author Topic: Tangents  (Read 6841 times)

virgilw

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Tangents
« on: December 05, 2014, 09:09:23 pm »
I might be dumb here as I just started messing with lighting on sprites, but the tangents that tk2d seems to auto generate are (1, 0, 0, 1).  Should this not be (1,0,0,-1) to work properly with normal maps that Unity imports from height maps? (The normals tk2d generates are (0,0,-1) as expected).

For example start with a grayscale height map, import it into unity, set it as a normal map and check the checkbox for gray scale.  Use that normal map on a sprite with a directional light coming from the top left of the screen.  For tk2d sprites the light appears to be coming from the bottom left.  Now I know I can just edit tk2dSpriteCollectionBuilder.cs and change the tangents that get generated (this works, I've done it), but before I do that and move on I wanted to see if there was something I was missing first.  For all I know Unity's conversion from height map to normal map is upside down. 

(Note that I have the linked sprite collection atlas that tk2d generates set to Normal map, grayscale.  So I'm using Unity's built in grayscale height map to normal capability).


As a side note, I just started using 2DToolkit (like 2 hours ago) and have been using Unity's straight up SpriteRenderer on my current project.  I wanted to use 2dtoolkit because of the linked sprite collections that simplify dealing with normal maps and atlases.  And it seems to do the beautifully.  That alone makes 2dtoolkit  awesome.  Throw in that you can slice sprites and it's a clear winner over the stock SpriteRenderer in Unity 4.6.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tangents
« Reply #1 on: December 06, 2014, 01:49:31 pm »
Quote
I might be dumb here as I just started messing with lighting on sprites, but the tangents that tk2d seems to auto generate are (1, 0, 0, 1).  Should this not be (1,0,0,-1) to work properly with normal maps that Unity imports from height maps? (The normals tk2d generates are (0,0,-1) as expected).

I'm not sure about that - the programmer who did that doesn't work for us any more but I do remember him showing it to me working as expected compared to a Unity plane, but I wouldn't bet that it is 100% correct as I didn't personally write it...

Quote
As a side note, I just started using 2DToolkit (like 2 hours ago) and have been using Unity's straight up SpriteRenderer on my current project.  I wanted to use 2dtoolkit because of the linked sprite collections that simplify dealing with normal maps and atlases.  And it seems to do the beautifully.  That alone makes 2dtoolkit  awesome.  Throw in that you can slice sprites and it's a clear winner over the stock SpriteRenderer in Unity 4.6.

Awesome!

virgilw

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Tangents
« Reply #2 on: December 06, 2014, 03:09:00 pm »
I did some further research and basically "it depends".  The Y axis of a normal map can be either way and different sofware generates normal maps either way.  Blender does it one way, other things do it other ways.  For instance: http://answers.unity3d.com/questions/28850/tangent-normal-map-produces-reversed-effects.html

Looking at a app like Shader Map 2, it defaults to the "Standard" but you can flip it easily (on any axis).

In the case for Unity, it looks like they are converting grayscale height maps the same way Blender might.  That makes the default tangent in tk2d be inverted.  For anyone using an external tool like Shader Map, the default output normal map would be correct.

Long story short, it would be cool if there were a little checkbox under "create normals and tangents" that said 'invert tangent".  This would cause the tangent to be 1,0,0,-1 rather than 1,0,0,1.    Or, I suppose you could allow full entry of all 4 numbers for a tangent.  Having an option like this would allow folks with normal maps to work with them without having to reprocess all of them to flip the red/green (which is such a chore, it's far easier just to make a code change to tk2d... but then I have to remember to keep doing that every time I upgrade tk2d).

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tangents
« Reply #3 on: December 07, 2014, 11:54:35 am »
added that to trello, since its a simple thing it'll probably get done in the next version but no promises.

thecosmicboy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Tangents
« Reply #4 on: May 26, 2015, 06:44:58 pm »
Any updates on this? Or a location I can find the code to edit myself?

The issue can be corrected by inverting normal maps in the Y direction,
but I am also using height maps, and this cannot be rectified in the heightmap image,
the tangent on the y-axis needs to be flipped otherwise it will not display correctly.

Any help with this would be great as I really need it working for this project.

Thanks :)

thecosmicboy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Tangents
« Reply #5 on: May 26, 2015, 07:16:04 pm »
Never mind, its in tk2dSpriteCollectionBuilder.cs

line 1939:
tangents[t] = new Vector4(1, 0, 0, 1);

Changed it to this :
tangents[t] = new Vector4(1, 0, 0, -1);

Worked like a charm :)