- Home /
Putting this aside for now
Shader problem with 2D fake lighting
Hey,
I'm rendering my game on a rendertexture, i'm also rendering some black sprites with the Layer tag "Masks" on a second rendertexture against a white background, in shader i'm lerping between my game rendertexture and a black color, using the "mask" rendertexture as the lerp's alpha, the black color is shadowing my scene and the sprite masks act as 2D lights, digging in that shadow.
I'm trying to have some sprites not be affected by this shadow, but still render behind some of the sprites that are affected by it, the only solution i can think of is having that sprite have a second identical black sprite that acts as a mask, but that will also cut through the shadow while the sprite is behind other sprites, and the only solution that i can think of for that is for those other sprites to have a second identical white sprite that acts as a counter-mask.
I'm trying to find an alternative to having to add a mask to each sprites in the game, could this somehow be done in shader instead?
Here's a detailed visual explanation:
Some help would be extremely appreciated.
I'm not sure what a replacement shader is, i'll look it up, thanks for the suggestion.
I've looked at replacement shaders and tried them a bit, they're really interesting but i don't see how they can help me in this case, maybe if you could give me an example that would help me get started.
Answer by Petroglyph · Oct 23, 2017 at 11:39 PM
Hello KhenaB!
Why not having all the "shadow-free" objects in a separate layer, rendered exclusively by a second camera with a higher depth than your main camera? In this setup, the main camera would be say depth 0 with a culling mask including everything but the shadow-free objects. It would have the post-render process that you currently use to add shadows. Then the second camera with eg. depth 1 would add any objects it renders over the scene, but still occluded by the scene. It is a setup I used several times and it worked well for my needs. Do you think that would work for you?
Your game looks cool, by the way!
Thanks for answering,
I've thought about doing this but i don't understand how those shadow free objects could be occluded by some of the other sprites rendered by the main camera, i blend all my rendertextures in one shader.
Thanks again
The part i don't get is "but still occluded by the scene", if i render the shadow-free objects on a separate rendertexture they will be rendered alone without the sprites that occludes them, so when i'll render that over the main scene and the shadows it won't be occluded!
I see, the setup I mentioned will work with both cameras rendering to the screen directly, and with a Blit on the $$anonymous$$ain Camera to achieve a post-process screen effect that applies shadows. Have you tried this? I'm not sure if it would break your setup.
I haven't, but it's important that my cameras output to a rendertexture, that is then captured by the final camera.
I am unsure about your setup, but I overlooked the part where you would blend all the RendertTextures in one shader. If so, then you have to check for depth buffer manually and do basically what a Blit does in itself.
I invite you to visit the Unity Doc for this function: https://docs.unity3d.com/ScriptReference/Graphics.Blit.html
I think you might find this an interesting lead. It seems to me it would be easier to achieve by letting the Camera render to screen and using their depth values though, but eitherway doable.
I'm rendering on a rendertexture because i'm rendering the game at a lower res, then upscaling, something like this: http://www.returnofthebrain.co.uk/pixel-perfect-virtual-camera-unity3d/
I'll give what you linked a read and see if it could be of any help, thanks again
Follow this Question
Related Questions
Shader mask using greyscale instead of alpha? 0 Answers
Masked RenderTexture not working in WebPlayer 1 Answer
How to control the alpha output of vertex lit shaders? 0 Answers
Alpha values not blending in RenderTexture 2 Answers
shader problem 0 Answers