Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How do I convert a Sprite to a Quad with texture atlas texture to light it in 3D?

Discussion in 'General Graphics' started by darthdeus, Dec 27, 2023.

  1. darthdeus

    darthdeus

    Joined:
    Oct 13, 2013
    Posts:
    94
    I have a "2.5D" game where I have a regular 3d scene but 2d sprites in it, WIP example in screenshot.

    The thing is, regular `SpriteRenderer` doesn't react to 3d point lights. From what I understand it should be enough to just turn this into a regular MeshRenderer with a quad and everything would work, right?

    The problem is I'm not sure how to create the material with a texture atlas. Ideally I'd like a solution that still lets me to animations with atlas regions as if it was using a sprite renderer, but I'm not sure if that's possible.

    Is there some kind of simple solution, or maybe an asset that can do this?

    upload_2023-12-27_18-37-8.png
     
  2. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    1,005
    The simplest solution is to simply use a sprite shader that supports lighting.
     
  3. darthdeus

    darthdeus

    Joined:
    Oct 13, 2013
    Posts:
    94
    Do you have any tips on which one? I found a few random tutorials but I don't have much experience with shadergraph.
     
  4. darthdeus

    darthdeus

    Joined:
    Oct 13, 2013
    Posts:
    94
    Okay I actually figured this out based on one of the tutorials and it works quite well, here's the full shader.

    upload_2023-12-28_0-47-9.png


    The only problem I have is that in this case the lighting is only "from the front", as in the light on the left doesn't light the sprite at all. I guess that's kind of expected due to how 3D works, but I wonder if there's a way to make it somehow work so the lighting is independent of face direction but distance only?

    upload_2023-12-28_0-48-0.png
     
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    1,005
    Most any shader provided by Unity in the material's shader drop-down menu will do the trick. There should even be one in the Sprites category called 'Diffuse' that specifically handles some of the issues of sprites.

    As you already noted, there is the issue with lighting from behind. In this case what you'll want to do is use an unlit shader and then implement a lighting system yourself using a custom node and some relatively simple HLSL. This video should get your started.
    The video is a couple years old and meant for URP so you might end up having to dig around for updated details if anything doesn't seem to work.

    The most relevant sections will be 'Project Setup', 'Diffuse Lighting', and 'Additional Lights'. One thing I didn't see mentioned was applying any ambient light so keep that in mind as well. The section for specular highlights, shadows, GI, shadow masks, and fog may or may no be as important to you.

    For the lighting model you'll specifically want to ignore the normal direction and light direction and only consider the light intensity and distance. Obviously there is no distance when considering directional lights so its up to you how to handle them versus other light sources such as point lights.