Hello Guest

Author Topic: Casting Shadows  (Read 15123 times)

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Casting Shadows
« on: September 24, 2013, 11:06:43 am »
Hi!
Currently i'm trying to make my sprites cast shadows from directional light, but i can't seem to find the right settings for it to happen. I searched forums, but never found any specific answer.
My first goal is to make at least some kind of shadows, and after that i need to make it count alpha channel so i get correctly shaped ones.

Can anyone give me some advices or directions where to look for an answer?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #1 on: September 24, 2013, 11:22:48 am »
Transparent shaders can't cast shadows in Unity - switch to a Unity Cutout/Diffuse shader and it should cast shadows.

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #2 on: September 27, 2013, 07:26:55 pm »
Hmm... I've found cutout/Diffuse shader only under "Transparrent" in shader lisrt and it give me no shadow  :(

Check out this screenshot, is this the right one?
http://take.ms/HCVmYZ

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #3 on: September 27, 2013, 11:23:33 pm »
Transparent/Cutout/Diffuse should definitely cast shadows.
If it doesn't, it means your scene isn't set up properly - make sure your light is actually casting 3d shadows first (test with 2 boxes).

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #4 on: September 28, 2013, 09:38:09 am »
I managed to cast shadows in scene view (was suposed to be using directional light), but now i have another problem.
I can see shadows in scene view: http://take.ms/SSO6RZ
But they still don't show up in game view: http://take.ms/v0LGbc

I tried to move and rotate light around, but had no luck (only managed to get really long messed up shadow at extreme angles and really high intensity)

Looks like tk2d camera wouln't render them unless i switch it to perspective
« Last Edit: September 28, 2013, 09:43:38 am by Demonocrat »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #5 on: September 28, 2013, 10:08:47 am »
What version of Unity are you using?

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #6 on: September 28, 2013, 11:17:16 am »
It's 4.2.1f4

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #7 on: September 28, 2013, 11:56:37 am »
There was a bug in unity 4.0 and 4.1 that broke this - coulda sworn it was fixed in 4.2... I'll take another look to confirm and get back to you.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #8 on: September 29, 2013, 04:28:22 pm »
This looks like a combination of things that is breaking the shadow casting with the tk2dCamera. If you select the camera, get it to show the Unity camera by clicking the little settings icon next to the tk2dCamera and click "Toggle unity camera". Changing the unity "orthographic size" setting seems to change the shadowing when it shouldn't - its using a custom projection matrix at that point, but the shadowing isn't respecting it. I'll have to investigate this properly - its definitely a bug in the Unity camera that isn't using the projection matrix, but I think from my initial experiments that it is possible to somehow convince it to use the correct settings.

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #9 on: September 29, 2013, 04:58:01 pm »
I toogled unity camera as you said, but i can't seem to be able to change it from orthographic to perspective (if that's what you were offering to do). No matter what i change at setting of unity camera it jumps back to be equal to settings of tk 2d Camera (Script).

Or maybe i understood you wrong? Is there a temperory fix for this situation at all?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #10 on: September 29, 2013, 05:09:09 pm »
No I meant to change the orthographic size (size under the projection box) on the Unity camera. Reduce it from 480 or whatever its at right now, to 10 or something. You should see the shadow creep in but see no change to the viewport otherwise. If you want something to work with now, use a normal Unity camera without the tk2dCamera script attached.

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #11 on: October 01, 2013, 06:17:56 pm »
Thanks that helped! :)

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #12 on: October 01, 2013, 10:28:52 pm »
Argh!
Celebrated too soon =(

Now I have a new problem - camera shows only shadows in the middle but completely ignores shadows of the objects on the sides.

Check it out:
First there is no shadows for bar chairs - http://take.ms/OkBVPj
And now there is no shadow for a chair that previously had it, but now on the edge of the camera - http://take.ms/YgB9uZ

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Casting Shadows
« Reply #13 on: October 01, 2013, 11:48:00 pm »
So what did you actually try here?

Demonocrat

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Casting Shadows
« Reply #14 on: October 02, 2013, 01:22:07 pm »
Well, i want shadows to be shown for all objects on the screen ))
I use single directional light for this purpose, but for some reason camera shows only shadows for the objects in the middle. If you move camera around you could see that shadow disappears from one objects and appear on others.

As i understand this shouldn't happen on orthographic camera, or am i wrong?