Field of view visualisation (E03: stencil shader)
In this bonus episode we create a stencil shader to achieve a cool limited FOV effect.
Source code:
github.com/SebLague/Field-of-...
Some links about stencil shader:
docs.unity3d.com/Manual/SL-Ste...
forum.unity3d.com/threads/unit...
Support my videos on Patreon:
bit.ly/sebPatreon
Пікірлер: 176
I thought the series was finished, but apparently you guys thought differently! As requested, in this episode we look at creating a stencil shader to render only what is visible to the player. Check links in description for some extra info on stencil shaders. Hope you enjoy :) Don't forget that you can support these videos on Patreon if you wish. www.patreon.com/SebastianLague?ty=h
@Mikekan13
8 жыл бұрын
+Sebastian Lague Totally support these. Only wish I could afford to give you more.
@SebastianLague
8 жыл бұрын
+Michael Kantack Thanks, I appreciate it!
@YuriNoirProductions
8 жыл бұрын
+Sebastian Lague Wow Sebastian this is great.. i'm a gamedesigner and had a concept a long time in my mind.. I planned to realise this via normal shadows and lighting but ewncountered so many problems..you really helped me to get back on that project ! thanks a lot!
@Wulcat
8 жыл бұрын
+Sebastian Lague Hello , how can i ask you question about the topic related to procedural cave generation tutorial>?
@krontexpit
6 жыл бұрын
+Sebastian Lague your tutorial is fantastic even until now! How would you create a stencil object shader for a 2D sprite?
The production value and quality of your videos is outstanding. You deserve any success that may come your way.
I can't thank you enough! i was dreaming of how to make a render mask for effects like glow or lens distortion. i leaned more in the first 3 minutes than i could've hoped. keep up the excellent work!
2020. 5 year old and still relevant tutorial series (the whole Field of View I mean). Holy damn man.
This is really awesome. It almost works for a perspective cam too.
Again, very cool stuff. Thanks for your wonderful tutorials helping us beginners learn the ropes.
Awesome tutorial series, your articulation is on point.
Literally everything in this video is 100% new to me Thank you !! And Happy new year =) !
shaders are black magic
@EdgarGarcia-kt7bv
6 жыл бұрын
I swear
Really good tutorial with many clear explanations. I love it. I think that there is something to do with this principle - a good puzzle game. Thank you Seb ++
Infinitely useful tutorial. Thanks!
Amazing work as always!
It's very cool!Hope you publish more tutorial like this!
Nice! I'm going to try and make a version where what was previously in your fov fades slowly, like a memory.
Exactly what I was looking for. Thank ya!
Thanks @Sebastian for this tutorial serie about FOV. However, i can't get the stencil mask to work on my perspective camera (but it works very well in orthographic mode). What can i do to make it working in perspective mode ?
TripleA. Amazing As Always!
Thank you so much for this series! A couple of questions: 1) If you were to implement the same mechanic now (7 years later), what would you change in your approach? Have you learned some better ways to do it or is this one as basic as it gets? 2) Is this approach good for 2d games? It feels like creating 3d meshes for 2d space is excessive
Hats off man! Beautiful!
hey sebastian! do you know an easy way to implement something like a stencil falloff? like a view range that slowly fades out?
Hello, I've been wondering on will you be continuing on the "Creating a 2D Platformer" -series?
Hi! Awesome video! It helped me out alot. I have one pretty urgent question though - the fieldvisualization viewfinder I made according to this tutorial can't seem to see cast shadows. Do you have any idea what might be the problem?
Great tutorial as always! Followed it all the way through perfectly. Just a question if you happen to know. I tried an approach of duplicating the ground and having a standard material on the version that I want to see in the background, but for me that material has shadows of different lengths on it than are being projected onto my shader material when I make those parts visible in the field of view. Any thoughts on why?
Hey Sebastian, i'm currently working on a little game using your technique to create long shadows. Is it somehow possible (maybe with a shader?) to soften the edge at the end of the field of view circle so that it fades out like a shadow?
Thank you for your work. The video and the resource are all perfect. I want to ask a question. Can I use it with sprite renderer?
Firstly, I love your videos and have learned so much about Unity, coding, math applied to games and more. Secondly, I have a question regarding the shading effect when the camera is tilted. Is there some kind of easy way to make it work even tho the camera is tilted a bit? Because the shader effect gets really weird if the camera is not directly on top of the scene. Thanks so much for your videos, it helps a lot!
Hmmm. Is it possible to make objects using the StencilObject shader only cast their shadows within the mask?
These are amazing thank you so much
As a question, wouldn't it be better to scale the View Visualization object so that if you are doing a 360 degree area it shows the color behind as well as in front?
This is so cool!
would be cool if you could still see the background with a reduced visibility
@maow9240
3 жыл бұрын
have you found any tutorials on how to do that? i'm currently trying to figure it out.
@atoastlover
3 жыл бұрын
@@maow9240 Did you manage to figure it out by any chance?
@hangyboi
5 ай бұрын
you managed that yourself maybe? @@atoastlover
Short but great. Thx a lot.
THANK YOU VERY MUCH! You are a wizard! :)
Is there a way to modify this shader so that instead of everything not in view being black it is simply darker (ie take the normal color for that pixel and multiply it by RGB(0.25f, 0.25f, 0.25f)?
could you maybe do a series on how to make a ray tracer after this i would really love that.
This is working fine with 3D scene too, but the hidden objects have visible shadow :/ Any solution without set off shadow on the gameobject renderer? (I like to hide only the object, without black fog...)
Any ideas on how to convert this to URP/Shader Graph?
@joepeaden1661
Жыл бұрын
Hella late but for future devs- (kzread.info/dash/bejne/d66Bmq6AqrDQncY.html). It's the best I could find at least.
@klubkofta
Ай бұрын
If you are using URP and the masking effect isn't happening, or your stencil shaders are giving a parse error claiming there is an unexptect "}", you can fix it by creating a new URP shader graph (if this is for the stencil object shader, set up sprite color & alpha outputs from _MainTex inside the shader graph editor, so that the resulting material can get the color of your main texture), click on "View Generated Shader" in the inspector, copy the generated codes from the shader graph to your .shade file, then make the edits in this video (add the stencil pass in the subshader before all other passes). As long as both of your shaders are URP, things should work properly.
Thank you so much Sebastian, you have no idea of how much useful has being your tutorials, do you know how to implement this in a 3d enviroment? do you think this would be expensive?(i say it for the raycasting)
Does this work on 2D too?
Thank you very much for this :3
Hi, this is really cool solution.. But I am still wondering, how to force viewMeshFilter to copy the terrain by its normal. If the terrain is not only flat surface. Or it would be nice, to detect height from ground during rendering the view angle..
С новым годом тебя! Привет из Сибири!
Is there anyway for the targets to have a bool inside them to check if they are being seen or not?
Hey, I know this is old, but I'm trying to figure out a way to create multiple levels of the field of view. For context: I'm trying to make a different level of field of view when holding a key to view objects at a low/higher level. Like, crouching behind a barricade, changing the field of view. Do you know how I could go about that?
Hey Sebastian, great tutorial! What would you recommend as the best way to draw a darkened version of the level instead of just black outside of the cone of vision?
@maow9240
3 жыл бұрын
have you figured this out? i got the black oustide but i don't want it to just be all black
@supercables251
2 жыл бұрын
@@maow9240 have 2 cameras on top of each other, one renders the game, the other renders the fov mask to a render texture. A post processing shader draws the game where the fov mask is white and the hidden color/texture where the fov is black. Good shader tutorials kzread.info/dron/EklP9iLcpExB8vp_fWQseg.html
Do you know use this variable "public List visibleTargets = new List ();" "VisibleTargets.Add" I wanted to convert but I do not know you know how one can convert to "Js" ???
Can you make a video on how to make compasses used in Skyrim or Fallout, with way point markers and such?
How do I make the shader compatible with a 2D Lit Scene?
Have you thought about doing a volumetric shader tutorial? for say..fog or clouds?
Man, thx you very much! Спасибо!
Does anyone know if you can change the colour of the Visualization FOV mesh?
for some reason my targets are not rendering I am using unity version 2022 I don't know if this is still viable with the later version. Everything up to this point was working fine and I had thought everything was fine until this point
Does this work with UI elements, like canvases?
How do I make a 2d version?, I tried to convert it but it didn't work
@rendioktaviardo3757
5 жыл бұрын
yeah, i search it too, waiting an answer
Can you make something like Mark's of the Ninja Fov, where everything not in field of view is blured?
this doesn't work on latest version of unity. could you please explain last part of the camera setting
I have an issue where if I add the material with the shader/object script, it brings the brightness/ contrast to my sprite very low, is there a way to fix this, do I need to add some code to the .shader file?
Is the field of view method and also this shader adaptable for 2D objects in Unity?
@SebastianLague
8 жыл бұрын
+Josh Nicholson Yes, though you'll have to add transparency to the stencil object shader if you want it to work well with sprites.
@SandDashGaming
8 жыл бұрын
+Sebastian Lague Hey, I have tried to make the "Object Mask" stencil transparent to work with sprites but the sprites don't seem affected by my changes to its transparency. I have looked on the unity forums and can not find anything to help, could you tell me what exactly needs to change in the stencil?
just a thought but could the mesh be created in the shader so it performs even better? i might just be uninformed of course.. : -)
Good times. Favorited, liked, subscribed, belled. >:D
@vxg2963
7 жыл бұрын
Question: Camera following and WASD movement with W being forward at all times (based on character's facing, while A and D strafe based on character's facing and D moves backward). I got the camera part by using: Camera.main.transform.position = new Vector3(rigidbody.position.x, 50, rigidbody.position.z); (is there any better way to do it?) Can you help me out with the movement?
hey sabastian did you know that a game thats called (among us) is using exactly that line of sight effect?
Can you make tutorials on how to make and edit 3d meshes in unity using script
Awwwwwww Awesome!!
Hi sebastian! I love your tutorials the helped me quite a lot. But now i have a little problem. I want the camera to follw the player. I cant get this working. everytime i my player turns arround like a freaking wheel... i have no idea how to fix this...
@jamestmp4071
6 жыл бұрын
Try the following: using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraTracking : MonoBehaviour { public Rigidbody Target; // Update is called once per frame void Update () { transform.position = new Vector3(Target.position.x, 10, Target.position.z); } } Just set the Target to the player rigidbody, or any other rigidbody you wish to track.
Having a hard time getting this to work for 2d game with sprites. Is it possible to adapt this to sprites/2d? Can't wrap my head around it at all.
@eggsscramblerzz4711
5 жыл бұрын
mesh != sprite
@snoober6690
4 жыл бұрын
I got something working in 2D The ` marks are not to be included and designates code between them: - I used the "unlit shader" instead of "surface shader". not sure if that part matters. Make the two shaders that Lague makes in the youtube tutorial. - Change the tags `"RenderType"="Transparent"` and `"Queue"="Transparent"` (and Transparent-100 for the mask). Below the tags add `Cull Off` line and `ZWrite Off` line. Do this for all 3 shaders total we will make. - Make a 3rd shader (I used unlit) and do the same changes as in the last step. Add a color property (check out Ronja's tutorial on properties www.ronja-tutorials.com/2018/03/22/properties.html). This involves a line in Properties block `_Color("Color", Color) = (0, 0, 0, 1)`, a line below pragma's but outside of structs/functions `fixed4 _Color;`, a line in the appdata struct `fixed4 color : COLOR;`, a line in v2f vert function `o.color = v.color;`, and a line in the frag function `col *= _Color;`. - Also add a blend to the fog shader. add below tags `Blend SrcAlpha OneMinusSrcAlpha`. - Add a stencil block to your fog shader, but it's going to be opposite of your object shader. So it's: Stencil { Ref 1 Comp NotEqual } - Make a material for each of the shaders (3 materials, one for mask shader, one for object shader, and one for fog shader). For the fog material, the color is currently white because of the (0, 0, 0, 1) part from last step. Change it to black and change the alpha via inspector window. The blend line will use this alpha so play around with this value. - Make a sprite that covers your level. Make sure everything on the same z-level (probabably 0). This will be your fog, so change the material to your fog material. - Change your view visualization cone material to your mask material, and change any objects you want to not render outside the cone to your object material. I hope I got everything I did in this comment. The fog is an optional difference I made, because I would like certain objects to just be darkened instead of not rendered at all, but you could leave that part out if you want it more like Lague's tutorial (and just make everything object shader so it won't be rendered when outside of view cone).
@phoder1
4 жыл бұрын
@@snoober6690 Any chance you can upload the files somewhere? The link for the guide you provided no longer exists.
@snoober6690
4 жыл бұрын
@@phoder1 here's the page from the wayback machine: web.archive.org/web/20200407085549/www.ronja-tutorials.com/2018/03/22/properties.html
@phoder1
4 жыл бұрын
@@snoober6690 Great thank you! I got it to work.
How would you go about making the viewmesh more of a cone to allow for a more 3D look
@keraunos94
7 жыл бұрын
Hey man it's been 1 year but did you make it ? I need a solution for this exact issue ! Thx
@hangyboi
5 ай бұрын
did you make it eventually? xd@@keraunos94
@keraunos94
5 ай бұрын
@@hangyboi hahaha waw 6 years ago. IIRC a dev friend helped me to make a 3d cone with many ray tracing (from origin to many points on a circle), from all hit points he created a dynamic mesh I guess, now about the mask/shader stuff I really don't remember sorry, not event sure we made it XD There should be more stuff now than 6 years ago though. Good luck^^
@hangyboi
5 ай бұрын
@@keraunos94 yeah, honestly for my game this tutorial was more than enough. Glad you are alive, just commented on you for fun :)
Have a way to fade in-ou the distant enemies?
n1, ty mate
My characters have a toonshader, how to blend two shaders?
Can i using your skrip for my project?
How can I add a transparency to it, if I'd prefer it not totally black?
@Blocks0
7 жыл бұрын
Hi. I'm looking for an answer to this also. Have you found a solution?
@TheStigChristensen
7 жыл бұрын
Nope, nothing so far :/ Shaders are hard man
@Blocks0
7 жыл бұрын
Maybe I figured it out. You build a mesh of the visualisation and give it a transparent white-ish material. Then, place another mesh over all of the map. Make it have a transparent dark material. The places that are not seen in the field of view are darker than the where there is a view mesh, since the mesh's white-ish material cancels out the darkness. It's a hacky way of doing it, and I'm sure there's a way better way, but it's what I've got so far.
@distrologic2925
7 жыл бұрын
you would probably need something else than a stencil shader, since a stancil shader is mostly only used to store "On/Off" vaules. a depth shader could store float values, which you could use as a transparency value. I have no idea if or how this works in unity however.
@neverever719
6 жыл бұрын
I'm currently trying to find a different solution, but right now I have this (I'm using a 2D scene, this might not work in 3D): 1) Duplicate the original object, darken the surface of the duplicated one, and move the original object above the duplicated one. 2) Apply the Stencil Object Shader to the original object. When the original object is hidden, the copy below it be be revealed, giving the illusion of transparency.
You are amazing
Can you make more blender tutorials I realy liked ones you made I would realy be happy if make some Thanks.
any one having issues with this not showing the enemys but its showing the view as blocking them. also the buildings are showing but are not getting any color
I copy the shader but nothing diseappear with the object mask I'm in unity 2017.3 is there something to change ?
@timotheemonney6547
6 жыл бұрын
I didn't set my camera as orthographic. That why it didn't work But it there a way to do the same with perspective camera?
Great tutorial, but the source code isn't up to date to the finished project in the video, there's some parts lacking in it like the scene doesn't have the stencil mask layer.
It just shows damn pink instead of being like a light.
can we do this for 2d ?? please reply
@reed9269
5 жыл бұрын
so can we ?
Since my camera is not entirely topdown but with a 45 degrees angle the shader is cutting through the objects, how would i solve this? imgur.com/a/jKGrb (screenshot of problem)
how to make a shader for this created mesh ?
I managed to setup the shaders on 2d textures, but I am having issues with the maskCutAwayDist, I don't know how to show all edges of the blocking textures, at most I manage 2. for example I use Vector3.up * maskCutawayDist + Vector3.right * maskCutawayDist and I get the texture edges shown on left and bottom parts (Vector3.forward * maskCutaway does not do anything for 2d). Has anyone tackled this?
@psiho92
6 жыл бұрын
Hmm, I think the issue is I do not rotate the player object :/
@psiho92
6 жыл бұрын
Ok, this is how you would go around this: Vector3 tempPoint = transform.InverseTransformPoint(viewPoints[i]); vertices[i + 1] = tempPoint + tempPoint.normalized * maskCutawayDist;
@simonaberg4392
6 жыл бұрын
Would you mind sharing how you setup the shaders for 2D?
YOU ARE THE ONE!!!! THANK YOU!!
Oh shit shaders
So it can be applicable on an RTS Fog of war system ? Am I wrong ?
@emascheg
8 жыл бұрын
+Castor Holyday Yep well be work but is going to need to more work.
@ShadowLow25
7 жыл бұрын
Tile based game ? haha switch to unreal engine everything is free and easy to make fog of war :p
So, how make shader for URP?
in my own project, whenever i put the stencil mask on my view visualisation material, the field of view disappears. Why is that? can anyone help?
@binyang8849
7 жыл бұрын
i have the same problem
@binyang8849
7 жыл бұрын
I JustNow Solved this Problem By Upgrading Unity
Can this work on a 2d scene?
Hello, I'm currently translating your Fov code to 2D, I have your part 1/2 and 2/2 working but I don't understand how to use the shader on 2D sprites. From what I can read in the comment we have to use transparency to make it work. But I don't see my level through the plane with the Shader. Can you help me a little (even if it's been 4 month you created this tuto) ;)
@shurikend
8 жыл бұрын
+shurikend Managed to resolve it by editing the sprite default shader instead of the one you used :)
@eysi94
8 жыл бұрын
+shurikend Can you share your code(the shader) with your fellow scrubs :)
@docmuerte
8 жыл бұрын
+shurikend Could you share your shader code? I can't get my head around shaders.
@shurikend
8 жыл бұрын
+DocMuerte this is the shader script for the sprites you want to hide : pastebin.com/NaiXch83 and this one is for the mesh to reveal the previous one: pastebin.com/E2Nu2ytb
@docmuerte
8 жыл бұрын
+shurikend Big thanks! I experimented until I got it working.
How can you apply this to a 2d game?
@EdgarGarcia-kt7bv
6 жыл бұрын
with black magic as rodolfo rubens said
Looks like for some reason that method doesn't work anymore 2019.1f. Thx for the tutorial anyway
YOU ARE A GOD AMONG MEN!
This sadly seem to have stopped working in the newest version of Unity :-/
@Shringyshrong
3 жыл бұрын
it definitely still works, have just got it all setup!
Hi! I want to achieve this visible Field Of View but in runtime... Any hints?
Tutorial showing how to use it on sprites in 2D would be great :)
@kamilblue
8 жыл бұрын
+Kamil Buczel doesn't matter, made it myself :p
@andreimuresanu1678
8 жыл бұрын
+Kamil Buczel How? I can't seem to do it.
@kamilblue
8 жыл бұрын
+Andrei Muresanu FieldofView class codeshare.io/y9JKN editor class: codeshare.io/O4NOn Code expires in 14 days :) Goodluck (don't mind polish comments :P)
@andreimuresanu1678
8 жыл бұрын
+Kamil Buczel Thank You Very Much :D!!! Yet is it supposed to cast shadow? Because that does not work. Thanks again :).
@eysi94
8 жыл бұрын
+Kamil Buczel I'm desperate!! Please share the code again.
Hello I liked your tutorial and I want to apply what I learned in a game that I am doing and maybe I will market it, it would affect me legal inquiry Please answer quickly
why does shader seem to be the most hardest thing to learn in unity?
@ufukseyithan
4 жыл бұрын
Ikr
This could work for an AI
This hurt my little brain
My mesh is totally irregular
500 (like) :D