Hello Guest

Author Topic: Sprite "Shadow" Rendering on tilemap when target sprite is not visible  (Read 7059 times)

RCBricker

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
  • Would you realy hit litt'l ol Giddeon?
    • View Profile
Hello guys, Unikron, I've been trying to accomplish something in the last few days to no avail, to decribe what is trying to do, I have set up my tilemap in the scene, each layer has a z offset of 1 from the previous creating 7 different layers, each layer has a sorting order in layer that increases from back to front like this:

Back layer sorting order = 1
Back layer detail sorting order = 2

Back Mid layer sorting order = 4
Back Mid layer detail sorting order = 5

Mid layer sorting order = 7
Mid layer detail sorting order = 8

Mid Top layer sorting order = 10
Mid Top layer detail sorting order = 11

Top layer sorting order = 13
Top layer detail sorting order = 14

Walls Filler layer sorting order = 16

if we see each layer as a depth i leave a open number between each so i can make my player and enemies sprite componen sorting order that number like this:

If player is at bottom depth, player sprite sorting order  = 3

If player is at bottom Mid depth, player sprite sorting order  = 6

If player is at Mid depth, player sprite sorting order  = 9

If player is at Mid top depth, player sprite sorting order  = 12

If player is at top depth, player sprite sorting order  = 15

I do this since I'm using a perspective camera so my sprite don't draw on top of layers that they are actually below to, but want I want to do is to draw a "shadow" of such sprite on top of the layer above like this:

If player is below N depth, draw shadow on top of N depth

I have tried setting the sorting order of such shadow to 20 but if draws on top of the player also, what I want is to draw such shadow in front of everything else BUT the player itself, I tried using rendering queue setters to no avail, and have no idea how to use the sorting order in other way that the said above to acomplish this.

So, help please.

And even if you don't have and answer thanks for reading all my ramblimg up there. :D
Hoghs Bossoms and an atom were talking, the bossom mocked the atom because he could not be everywhere and anywhere at once, the atom though about splitting.....BOOM.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
I really can't visualise this :) You'll have to post some pics or a simple diagram to explain this!

RCBricker

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
  • Would you realy hit litt'l ol Giddeon?
    • View Profile
Here are some images attached:

- First Image Shows a starting position so it can be compared to the second image so you can appreciate the perspective

- Second is a refference image to see the shadows displacement

Note: on both of these images the tilemap material is using a transparent/cutout/diffuse shader

- Third image shows the player at the base of the room ej the bottom floor, as you can see both the bottom mid platform (the thing with blue borders) and the mid floor (the brown wood floor where it was before) are drawn on top of the player

-Fourth image is the player standing on the bottom mid platform, being drawn on top of it but still below the mid floor (as it should be)

- On the fifth image i activated the "shadow refference", and as you can see the player looks a bit darker beacause the shadow is drawn on top of him

- Sixth Image is the player at the bottom floor again and the shadow is now drawn on top of both the player and the mid floor becauase it has the highest sorting order

Note: This time I changed the tilemap shader back to tk2d/litblendvertexcolor so it doesnt cast shadows and draws the "shadow" as i want it to.

- Sevent Image the player is below the mid floor but you still see the shadow on top as a refference to the player position (like when you entered the sewers in mario sunshine), and this is what i want.

The Problem: I've been trying to make this shadow draw above the tilemaps but below the player at all times, but for the player to be accuratly drawn below and on top the tilemap layer it should i have to change dinamically its sorting order (as can be seen in the editor in the images, this is due to the perspective camera)

On a side note: how can I cast and recieve shadows as with the transparent/cutout/diffuse shader but still use 2d toolkit shader properties? Is there a 2d toolkit shader that does this or it has to be custom?

Btw: Thanks for the reply Unikron.
Hoghs Bossoms and an atom were talking, the bossom mocked the atom because he could not be everywhere and anywhere at once, the atom though about splitting.....BOOM.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
This is tricky. If you take out all the rest of the details, you just have 1 requirement - the player draws with a shadow if its in front of the tilemap, draws just the shadow on top of everything otherwise. I think you might have to think this through and refactor it in such a way that you remove that dependency - one way of doing that is -
1. Draw Tilemap / player + shadow for the "in front of the tilemap case".
2. Modify all tilemap shaders to write a unique number into stencil.
3. When  likewise the player should write a second different number into the stencil buffer.

At this stage you'll have the player + his shadow drawn when the player is obscured by a layer, but the tilemap will be drawn above the player and his shadow, but thats fine.

Now draw the player shadow again on top of everything, this time with a shader that only draws when stencil buffer is equal to the tilemap stencil number, i.e. this layer only draws in the part the player is obscured by a tilemap, i.e. the "shadow".

Hope that helps.


Quote
how can I cast and recieve shadows as with the transparent/cutout/diffuse shader but still use 2d toolkit shader properties?
LitCutout should work I think - to be honest I wouldn't really bother with Unity shadows with sprites, its a complete waste of resources doing that when you can simply redraw the bits you need with a black "shadowy-looking" shader, offset.

RCBricker

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
  • Would you realy hit litt'l ol Giddeon?
    • View Profile
Thanks, the stencil solution has nearly done the trick so far, since I'm not familiar with stencil I'm still trying to figure it out, but now that I added the stencil to duplicates of the LitBlend and LitCutout 2D Toolkit shader I keep getting a "shader wants normals but mesh doest have them" message, how can I modify the shader and get back the "d Toolkit spritecollection normals?
Hoghs Bossoms and an atom were talking, the bossom mocked the atom because he could not be everywhere and anywhere at once, the atom though about splitting.....BOOM.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
That isn't an issue - stencil and normals don't really have anything to do with one another. If you want normals you can enable them in the sprite collection editor and commit. Its not needed though, unless you're using lighting...

RCBricker

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
  • Would you realy hit litt'l ol Giddeon?
    • View Profile
The game does indeed run and I'm using lightning, but before the stencil solution, there was no warning message on console, the sprite had their normas generated in the sprite collection, does it really not affect the final product?
Hoghs Bossoms and an atom were talking, the bossom mocked the atom because he could not be everywhere and anywhere at once, the atom though about splitting.....BOOM.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
If it complains that it doesn't have normals it probably means normals aren't being generated, or something doesn't have normals turned on somewhere... I don't understand how adding stencil to the shader would have changed that.

RCBricker

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 15
  • Would you realy hit litt'l ol Giddeon?
    • View Profile
They are affecting each other, here have a look in the attachments man, now I really am clueless as to what to try now, the sprite colletion has setting to generate normal and all the object renderers are active, any sugestions?

Hoghs Bossoms and an atom were talking, the bossom mocked the atom because he could not be everywhere and anywhere at once, the atom though about splitting.....BOOM.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
I don't think I can debug this any further without actually seeing this. For what its worth, I know its possible to do this as I've done a similar thing in the past.