What is the best approach to mask players vision?
I am making a top down shooter, where players have a limited viewing angle. Now I have a dynamic lighting system down pat, however I've run into issues with hiding anything that is NOT in your vision cone.
Now the guys at Monaco posted their approach for their sexy lighting and vision, and was wondering how this could be done in Unity? They mention stencil and stencil buffers, and inverse stencil, but as someone who has never touched shaders before, it quickly goes over my head.
Monaco Game: https://www.facebook.com/notes/monaco/vector-lighting-explained/395139091995/
What would be the best approach? Would learning ShaderLab (through the Unity Manual) teach me what is required to pull of something like this?
Any tips, tutorials, or guides are appreciated!
Answer by Eudaimonium · Mar 31, 2016 at 10:36 AM
What exactly do you wish to hide from view cone? Only enemies and some objects, or everything and just leave it black?
If it's just objects, consider implementing a large cone-like collider as a child of your Player object, which is set as Trigger. Anything it touches should fade in the view, and anything not touching it should fade out. You can implement a Hidable interface and extend enemy and object scripts from that, and have the collider cone search for that script in collided game objects.
If you wish to hide everything, you can simply only have one lightsource which is the player, leaving everything else dark. Or if you've already lighted up and lightbaked your scenes, consider simply using a screen-wide HUD sprite with cone-like cutout, rotating on the screen mimicking the player's rotation in the game world.
However as a fellow gamer, I would strongly urge you to re-consider a design decision which makes 80% of my screen useless. Why hide anything not in view cone? Is this really necessary for the mechanics and atmosphere of your game? There have been countless top-down shooters without this mechanic which are working fine. There have also been quite a few with this mechanic that I disliked specifically because of it.
The game I am making is very much like Door $$anonymous$$ickers, slow and tactical, and not showing players who are around or behind walls is very important. This means that Static Objects are always rendered, but Players are only visible when inside you vision cone.
All I want to know is if it is possible to create a shader that does something like what $$anonymous$$onaco here has done.
Though your cone trigger method is a very interesting idea!
Oh you mean something more like XCO$$anonymous$$? Got it.
In that case, one solution I can think of is to do a Raycast from the player, to all enemies. If the raycast makes it (returns an enemy object) - it's visible, if not, you need to hide the enemy.
As to shaders, I believe you may want to just write a custom shader that will draw enemies with transparency control, so that you can fade in/out enemies when you transition between visible and invisible states, but it's not necessary.
So is it not possible to create a mask from a players vision cone via shaders?
draw vision cone > create a mask > render visible object > mask that > render everything else > combine them?
Though raycasting sounds like an easier approach, there would be issues like if a player was half visible, the raycast approach would show his whole body.
Ah, I meant fullscreen shaders, sorry about that. I think I will go with your approach however, as it seems the most logical for a game mechanic! Thanks for sticking with my stubborn thoughts on shaders and what not :P
Thank you for answers!
Your answer
Follow this Question
Related Questions
Making a shader ignore masks 0 Answers
Inverse Sprite Maskin 0 Answers
Shadowcaster Fragment Shader Discard 0 Answers