Search Unity

How to use normal Maps on Spritesheet

Discussion in '2D' started by Marrt, Oct 21, 2015.

  1. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    613
    I want to know if i can do light effects with spritesheet-animated 2D characters (not body-part animated)

    I found how to do this with a single normal map:
    http://robotloveskitty.tumblr.com/post/33164532086/legend-of-dungeon-dynamic-lighting-on-sprites

    But our characters have different normal Maps depending on the current Sprite of the animator, see that mockup:
    upload_2015-10-21_15-34-35.png

    How can i achieve that the normal maps are linked to each Sprite? Do i have to create a new Material with the right normal Map for every character?

    I am clueless, my current setup is a SpriteRenderer with Animator
    upload_2015-10-21_15-41-12.png upload_2015-10-21_15-36-45.png
     
  2. CodeAndWeb

    CodeAndWeb

    Joined:
    Jul 31, 2014
    Posts:
    60
    Create 2 sprite sheets - one with the normals and one with the sprite. They must have the same layout - this is important.

    Import the normal map, set the Texture Type to Normal Map. Make sure to uncheck Create from Grayscale - Unity will otherwise interpret your image as height map and create a normal map from it.

    Create a material that uses the normal map as Normal Map and the sprite texture as Albedo.

    Add the material to the sprite. See Screenshot.

    We've created a short tutorial here: Unity Normal Mapped Sprites
     

    Attached Files:

    Last edited: Oct 22, 2015
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    What is actually linking the normal map spritesheet to the albedo spritesheet?

    When you change the sprite on the renderer (in an animation clip for instance), is it that the material knows which region that sprite represents, and updates the maps to the same region?

    Is this possible for any map, like Emission for instance?
     
  4. Andreas Loew

    Andreas Loew

    Joined:
    Feb 27, 2013
    Posts:
    17
    Yes - it works with animations.

    The sprite renderer uses the regions from the sprite sheet. They are used for all textures/map assigned to the material.

    The important thing is that they all use the exact same layout.
     
    LiterallyJeff likes this.
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Thanks for the quick response.

    Cool. That's some magic under the hood that I was not aware of! I am reading that playing an animation with a different spritesheet cannot also switch to the appropriate secondary maps, so either multiple materials are necessary, or switching them in code is necessary.

    I found this script which builds a dictionary of maintexture to secondary maps, and watches for when the maintexture changes to swap the maps automatically. I'm not sure this is the most efficient way, but it's easier than having to worry about one material per spritesheet and swapping them appropriately.

    Download in this article:
    http://www.snakehillgames.com/multiple-sprite-sheets-sprite-lamp-and-unity/

    Thanks again! Took me awhile to find some definitive information about how this works for spritesheets.
     
    glenneroo likes this.
  6. timcommandeur

    timcommandeur

    Joined:
    May 14, 2013
    Posts:
    4
    Thanks for the info. Do you know how to flip the sprite in another direction?

    I got the lighting working with an animation, but if I want to face my character the other way (either by the flipX option in the sprite renderer or setting the scale to -1) it turns invisible.
     
  7. Deleted User

    Deleted User

    Guest

    Sorry if I must dig up an old question, but I have the same problem and I see that @timcommandeur never got an answer... :(

    For now the only way I see would be:
    1. creating a flipped version of the player,
    2. flipping the normal maps,
    3. adding the two versions of the player to a single empty object,
    4. creating the flipped animations,
    5. using a script to specify what sprite renderer to use when the player must be flipped...
     
  8. TPEUnity

    TPEUnity

    Joined:
    Jan 17, 2018
    Posts:
    36
    you probably want a shader that flips your normal map depending on if ur rendering front face or back face.
     
  9. Deleted User

    Deleted User

    Guest

    Well well, you made me curious. :) I explored the shaders list on the material I use for the player and selected "Sprites", I tested and it appears that "Diffuse" is doing the trick. The player doesn't react to the light as realistically as it does with the Standard shader but it still reacts to the lights. And now I can flip it! :)

    Sans-titre-3.jpg