Search Unity

3D character cast a 3D shadow on a 2d background?

Discussion in 'General Graphics' started by BBDCo, Sep 19, 2019.

  1. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Can a 3D character cast a 3D shadow on a 2d background? The character is 3D. The environment is 2D. The background is a flat 2D image.

    There are examples in Unity Augmented Reality where a 3D character casts a shadow on live mobile video. Is it possible to have a 3D character cast a shadow on a 2D flat background?
     
    ron-bohn likes this.
  2. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    If the background is a single flat image, then you could create a 3d object with your background image textured onto it. Then if you put a 3d character or object in front of it with a light then boom you have your shadow. Just make sure your 3d character object is marked as a shadow caster.

    There's probably other ways to do it that could possible be better for what you're trying to do.
     
  3. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi,

    Thank you for your reply. I apologize for my late response. (I thought I had set an alert if someone replied. But apparently it didn’t work.)

    You said “If the background is a single flat image, then you could create a 3d object with your background image textured onto it.” The background is a single flat image.

    If you place the background as a texture on the 3D object— is the 3d object locked to the same plane as camera? The camera is static not moving. Can you then use a ground plane that the 3d character can cast shadows onto? Do you need 2 different 3d objects? I just know that Augmented reality apps take 3d characters and have shadows that I assume are mapped on a transparent 3d object ground plane.

    Do you know the specific technical stepson Unity?


    Thanks Again!
     
  4. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    You would need the following in your 3D scene. Just in case, most of these things can be added to your scene under the "game object" tab that is located 4th from the top left in unity: File | Edit | Assets | GameObject | etc

    1) Place a cube game object or geometry (for your background) (under gameobject > 3D )
    2) Create a material to place on your background geometry (assets > create > material). The texture of your background would go in diffuse portion of your material. This is done in the inspector tab to the right-hand side in unity once you have created your material and selected/click on it.
    3) Place the material on your cube or geometry, set the smoothness to "0"
    4) Stretch the tiling of the material so it looks correct for how your background is displayed
    5) Place your character in front of the 3d wall that has your "background" image on it.
    6) Place a directional light in your scene and make it rotated so your character casts a shadow on the 3d wall.
    7) Insert a camera camera perpendicular to a cube that has your background texture on it.

    Note: if you are doing augmented reality, it seems possible in theory that you could create geometry, cube, plane, etc (your background or floor) and make it transparent while making it able to receive shadows. In theory this would require the surface of your background material to be matched up relative to a tabletop, ground etc IRL. I haven't tried this, but it's the first thing I would try in going about this.
     
  5. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi, Thanks for your quick reply and for your detailed instructions!

    Is the example for a wall shadow exclusively? The shadow needs to be on a ground plane rather than a wall shadow. I had tied to solve this before and went back to our notes and found some suggestions. (I am not doing augmented reality. I just used those as an example.)

    The project uses a 3d character over 2d illustrated background. The look is cel shaded. We need to use one light for cartoon cel shade look. We need a second light for the character ground plane shadow. The second light also influences on the cartoon cel shader look. And this is main problem.

    One option suggested was:

    1. You can't really identify lights in Unity's renderer from a shader point of view, so in this case if you only use one directional light you could edit the shader to use a user-defined vector instead, and it would still use the dynamic light for the shadows. You would then have to define the light direction vector in each material (or to go further, one could make script that takes a GameObject's forward vector and pass that to a global shader variable so that it's easier to tweak and automatic for all materials that use this shader).

    2. Transparent objects can’t receive shadows in Unity, the shadow map isn’t available in the transparent drawing queue, and there are other considerations with propagating the shadows through transparent objects.

    Try this shader : https://github.com/keijiro/ShadowDrawer

    You make it so the 2D object is rendered between the 3D and shadow matte to get the effect you want.

    There may new ways to solve this either with Unity or possibly a new plugin.


    Thanks so much for your time and information!
     
  6. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    Sounds like you wouldn't need a transparent shader. BTW I do believe it is possible to case a shadow on a transparent shader/object. YOu just might not make it 100% transparent.

    The way I suggested is way easier to do imho.

    The process I described will work the exact same with a ground plane. You can have a ground plane simultaneously with a background or vertical plane, cube etc.
     
  7. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Great! Glad to hear it will work the same with a ground plane. What about the lights needed to create the cel shader cartoon look conflicting with the light needed to create the direction of the shadow? Do we need two different lights that exclude the other from certain render calculation?
     
  8. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    Assuming you are talking about the character, then just use a toon shader for that object. Also, you should just try and experiment with what I explained if you haven't already. Without you telling me exactly what kind of media you want to make (and what art style/look), it's hard to give the best recommendation. Like are you making a cartoon or cutscenes? What kind of device are you targeting? There's really so many different ways to go about it, depending on what you want to accomplish in the end. Thanks!
     
  9. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi,

    Thank you for being so generous with your time! Yes the toon shader is for the 3D character. The look is very flat. For simiplicity here is a reference to cel shading on wiki. https://en.wikipedia.org/wiki/Cel_shading
    In the most basic terms its similar to this in terms of flat colors with shadow and cartoon outline. It is an interactive project rather than a game. It is for IOS and Android. There are cut scenes in that the camera and background scene is locked and the character moves in that enviroment.

    I will send this to the designer and see if he is able to implement this. Thanks so much for your advice!
     

    Attached Files:

  10. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    You're welcome. Your designer might not be thrilled about your approach though. I thought you were the one that was doing this directly and I thought I was helping you learn/introduce you to unity. Honestly, I've worked as a designer for a very long time and if a pm or client approached me with some random guy's input on what is "technically possible" rather than what is actually the best tool (or process) based on my experience, I would be inclined to run for the hills and find work elsewhere. Based just on your last post, I would say that what I described above is probably NOT the best way to go about what you are aiming for.

    There are quite a few variables that will likely come up in this project for your target device that have not been covered in this thread...like dealing with audio and optimization for example. You might not have audio. Unity is used for animation and film all the time so if you just said your exact specs, it's highly likely that someone has done it before.
     
  11. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi,

    Your provisional explanation makes a lot of sense. I was just trying to help him find solutions with expert Unity users who are far more experienced. He is a highly experienced programmer versed in a variety of software which makes him a quick study. Originally he had done a test of the character in a 3d environment with a shadow. When the 2d background was introduced, the shadow became an problem. His reasoning was it required two separate lights to achieve the effect. So I was looking for answers on the forum to find a solution that could solve the problem.

    I have had an extensive background in compositing, vfx and animation. So this seems like it would be simple from a compositing POV. But I completely respect that the Unity environment is entirely different. The project is almost complete. But these shadows still require a solution.

    I very much appreciate all of your suggestions, and taking so much time and your quick responses!
     
  12. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    Thanks for letting me know. That makes sense. JMO has an excellent toon shader that allows you to control shadow ramping and a lot of different settings/variation (like in the example you provided.) For the shadow on the background, the biggest thing I'm confused about is how you want it to look.

    It seems for best results, an unlit shader that receives shadows would be the best of both worlds https://discourse.urho3d.io/t/solved-unlit-material-that-receives-shadows/1264/4 ...your programmer could add a shadow pass to the unlit shader which might be pretty simple based on what you described. Sort of like if you're doing compositing with a green screen, you ideally greenscreen lit evenly which is probably why your guy said that about having additional lights. The problem with that is additional lights might diffuse your shadow on your background/floor where you want the shadows cast. So an unlit shader that receives shadows would be the most simple/direct/easiest/best way to go....that way you don't have to deal with extra lights and potentially have the most control over your shadows.

    Here are the shaders I use often for compositing 3d and 2d together:
    https://assetstore.unity.com/packages/vfx/shaders/toony-colors-pro-2-8105
    https://assetstore.unity.com/packages/tools/particles-effects/outline-toolkit-98020

    You might find the second link helpful for making 3d environments in unity that look 2d (think futurama, family guy, etc...you can achieve TV show level results with this combination in unity). I realize it might be a little late in your project timeline to do it this way, but in the future it's an enormous time saver because once you create an environment in 3d, you can screen capture your "2d" backgrounds from any angle potentially turning months into hours depending on the scope of your content. If you can, let me know how it turns out. Cheers!
     
  13. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Hi,

    Thank you again for your continued detailed advice! It is much appreciated.

    We are using Toony Colors Pro and are very happy with the results. It is able to achieve outlines with excellent result. The Outline toolkit is not something we are using. It appears it is an outline plugin rather than a compositing solution.

    I will share the unlit shader. It appearsthe link you provided there is some discussion of allpha which allows it to be rendered unlit. Does it also allow you to cast a shadow on a transparent object? Does this mean the same thing? Since the background is 2D, and character is 3D my thinking is we could make a 3d ground plane that is rendered tranaperent but accept shadows. This is in post production 3D rendering and compositing terms, which is what I know.

    To review 3d character rendered with Toony Colors pro. The 3d character lives over the 2d background. Needs solid black shadows rendered over transparent groundplane (so as not to obscure 2d BG). Does that make sense?

    My plan is to consolidate all of your advice which hopefully give the programmer a great solution.
    Let me know if these cover the needed solutions.

    Cheers!
     
  14. ron-bohn

    ron-bohn

    Joined:
    Oct 5, 2015
    Posts:
    314
    I'm happy to help, but I you should probably do what your programmer suggests because I don't know them and they are the one doing it. Also, have you looked at the documentation on realtime lighting in unity?
     
  15. BBDCo

    BBDCo

    Joined:
    Jul 6, 2018
    Posts:
    8
    Yes thank you. I will follow up with my programmer and recommends. Cheers!
     
    ron-bohn likes this.