2D Toolkit Forum

2D Toolkit => Support => Topic started by: Afro-Ninja on June 22, 2014, 09:27:20 pm

Title: Proper steps for bump-map lighting on a tilemap?
Post by: Afro-Ninja on June 22, 2014, 09:27:20 pm
I've searched and found a lot of topics on the subject but sometimes it's hard to figure out what is the most up to date method.
I have a collection that I use only for tiles, and I would like to apply a normal map shading technique to it.
Here's what I have done so far:

1. I set the normal generation for the collection to "normals and tangents"
2. I duplicated the collection's atlas and turned it into a normal map, and UNchecked 'create from grayscale' (the map has a blue hue to it)
3. I changed the collection's shader to unity's 'transparent - bumped diffuse' and applied my new normal map to it.

It certainly changed the way the lighting looks, but something is just odd about it. I move my lights around and the shadows don't really fill in like I would expect.

Should I be taking a different approach? Thanks.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: Afro-Ninja on June 23, 2014, 07:10:16 pm
as a side note, it seemed like I could get this working on a stand-alone sprite that wasn't part of a tilemap. It's not a big priority, just something I was curious about. If this is a large undertaking you don't need to go into specifics.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: unikronsoftware on June 23, 2014, 08:54:07 pm
I wrote a reply to this but must've forgotten to send :(
The tilemap doesn't generate tangents, you can get it to generate tangents by doing that after this line -
chunk.mesh.RecalculateNormals();
The tangents should be calculated using the UVs, but you can simply use any vector perpendicular to the normal...
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: Afro-Ninja on June 23, 2014, 08:56:53 pm
which .cs file is that found in?
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: unikronsoftware on June 23, 2014, 09:04:35 pm
tk2dTileMapMeshBuilder.cs
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: thecosmicboy on September 16, 2015, 03:58:50 pm
Any chance this could be elaborated on?

To calculate the normals in the tk2dSpriteCollectionBuilder.cs
it contains these lines after the normals are recalculated to calculate the tangents.
Quote
                            tmpMesh.RecalculateNormals();
               
               coll.spriteDefinitions.normals = tmpMesh.normals;

               if (gen.normalGenerationMode == tk2dSpriteCollection.NormalGenerationMode.NormalsAndTangents)
               {
                  Vector4[] tangents = new Vector4[tmpMesh.normals.Length];
                  for (int t = 0; t < tangents.Length; ++t)
                     tangents[t] = new Vector4(1, 0, 0, -1);
                  coll.spriteDefinitions.tangents = tangents;
               }


I have found "chunk.mesh.RecalculateNormals();" inside the tile tk2dTileMapMeshBuilder.cs
but I am unsure of how to apply the simple tangent code above to the TileMap.

Any help would be greatly appreciated :)

Edit : I have solved the problem by using the attached script called "TangentSolver.cs"
and implementing this line of code "TangentSolver.Solve(chunk.mesh);"
inside the tile tk2dTileMapMeshBuilder.cs script just after the normals are recalculated.

Like so

Quote
chunk.mesh.RecalculateNormals();
         TangentSolver.Solve(chunk.mesh);

Works like a charm,
Maybe you could implement this code into the next update.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: unikronsoftware on September 17, 2015, 11:26:32 pm
Hi there,
Thanks! I'll look into this.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: windjammers on October 08, 2015, 09:02:27 am
I notice that the file uploaded is a .meta file rather than the associated .cs file.I have a similar problem with tilemap bumpmaps and would love to see the code used to fix it.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: unikronsoftware on October 08, 2015, 10:50:19 pm
I'm not sure but I think it might be this -
https://github.com/danielbuechele/SumoVizUnity/blob/master/Assets/Helper/TangentSolver.cs
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: unikronsoftware on October 08, 2015, 10:52:39 pm
I'm not sure but I think it might be this -
https://github.com/danielbuechele/SumoVizUnity/blob/master/Assets/Helper/TangentSolver.cs

It isn't the same code but should work in principle, TangentHelper.TangentSolver(chunk.mesh) in this case.
Title: Re: Proper steps for bump-map lighting on a tilemap?
Post by: windjammers on October 09, 2015, 01:41:35 pm
Worked a treat. Looks exactly as I'd hoped. Thanks for all the help with this.
(https://dl.dropboxusercontent.com/u/43129774/lighting%20example%202.PNG)