prime31 / SpriteLightKit

Blended lighting system for Unity that works with standard Sprites
241 stars 43 forks source link

2D Occluded/Occluder shader issue... #8

Closed Hexxiss closed 6 years ago

Hexxiss commented 6 years ago

Forgive me, as I didn't really know where else to go. I've posted about this issue, I've posted a few different times to the Unity forums and haven't received any help.

I'm actually having issues with the demo here: http://prime31.github.io/stencil-buffer-occlusion/

My issue is, I'm trying to use it for a 2D isometric view game (think the legend of zelda), not a side scrolling game like the example. The issue comes with the depth sorting I'm using:

`using UnityEngine; using System.Collections; [ExecuteInEditMode] public class IsometricSpriteRenderer : MonoBehaviour { void Update() { GetComponent().sortingOrder = (int)(transform.position.y * -10); }

}`

Literally, that's all the code. Nothing complicated. All items that use this code, sit on the same layer.

Now, the shaders work however, only when the Player is in FRONT of an object, not behind. However if I change the "-10" portion of the code to "+10" it will work as I need, but it breaks the depth sorting...

I've reached the end of my rope, does anyone have any ideas?

prime31 commented 6 years ago

You have to manage your draw calls properly for any stencil buffer access to work. A batch must be fully sent to the GPU before any stencil data will be available to read from. For an isometric game you will have to deal with the properly and cant rely on sorting which doesnt do anything besides change the z-position of the Sprite vertices.

tidyui commented 6 years ago

I highly doubt this comment actually helped :) His technique is valid and is commonly used in top down games to sort objects depending on z depth

tidyui commented 6 years ago

Now come to think of it I’ve actually used this package together with the same ordering technique as you describe without any problems, I’ll take a look at how I did it

tidyui commented 6 years ago

Only difference is that I use it in late update, see code here:

https://github.com/blitbusters/BlitCore/blob/master/Scripts/MovableObject.cs

prime31 commented 6 years ago

I am not sure if you are following with regard to how the stencil buffer and draw calls/batches work. If a batch hasn’t yet been sent to the GPU then all of that stencil data has also not been written to the stencil buffer. The shadow is only drawn where it sees a bit in the stencil buffer so it is key that any occluders’ batches are already sent off to the GPU.

Hexxiss commented 6 years ago

Well, I appreciate both of your help! And Tidy's sorting method definitely works! Unfortunately like before, the stencil buffer only seems to work when I'm in front of an object, instead of behind like I would like. I may need to find another alternative.

tidyui commented 6 years ago

Well @Hexxiss, if you're looking to achieve shadows & lighting in your 2D game there are several ways of doing it. Here's a screenshot of a completely different technique I'm using which actually uses some elements of the prime31 package! So your imagination is the limit :)

technocrat-header