How can I implement a revealing light beam?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












In SEARCHLIGHT, you control a ray of light that illuminates incoming enemies that are otherwise invisible in the night sky. Here is an illustration:



Illustration



How would I go about doing this, preferably in Unity? I imagine this is less about lighting and more about shaders, but I'm not sure on details. The fact that I know very little about coding shaders makes it worse.



If it is about shaders, what are the specifics? Is there a stretched quad that moves with said shader applied to it, or perhaps is using the Line Renderer a better option?



As a beginner, may I try to achieve a similar effect, or should I leave it for when I'm better?



(P.S.: The game was made in Unity. Here is its itch.io page.)










share|improve this question









New contributor




Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    4
    down vote

    favorite












    In SEARCHLIGHT, you control a ray of light that illuminates incoming enemies that are otherwise invisible in the night sky. Here is an illustration:



    Illustration



    How would I go about doing this, preferably in Unity? I imagine this is less about lighting and more about shaders, but I'm not sure on details. The fact that I know very little about coding shaders makes it worse.



    If it is about shaders, what are the specifics? Is there a stretched quad that moves with said shader applied to it, or perhaps is using the Line Renderer a better option?



    As a beginner, may I try to achieve a similar effect, or should I leave it for when I'm better?



    (P.S.: The game was made in Unity. Here is its itch.io page.)










    share|improve this question









    New contributor




    Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      In SEARCHLIGHT, you control a ray of light that illuminates incoming enemies that are otherwise invisible in the night sky. Here is an illustration:



      Illustration



      How would I go about doing this, preferably in Unity? I imagine this is less about lighting and more about shaders, but I'm not sure on details. The fact that I know very little about coding shaders makes it worse.



      If it is about shaders, what are the specifics? Is there a stretched quad that moves with said shader applied to it, or perhaps is using the Line Renderer a better option?



      As a beginner, may I try to achieve a similar effect, or should I leave it for when I'm better?



      (P.S.: The game was made in Unity. Here is its itch.io page.)










      share|improve this question









      New contributor




      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      In SEARCHLIGHT, you control a ray of light that illuminates incoming enemies that are otherwise invisible in the night sky. Here is an illustration:



      Illustration



      How would I go about doing this, preferably in Unity? I imagine this is less about lighting and more about shaders, but I'm not sure on details. The fact that I know very little about coding shaders makes it worse.



      If it is about shaders, what are the specifics? Is there a stretched quad that moves with said shader applied to it, or perhaps is using the Line Renderer a better option?



      As a beginner, may I try to achieve a similar effect, or should I leave it for when I'm better?



      (P.S.: The game was made in Unity. Here is its itch.io page.)







      unity 2d shaders lighting






      share|improve this question









      New contributor




      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 6 hours ago









      DMGregory♦

      53k1198150




      53k1198150






      New contributor




      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 hours ago









      Demetre Saghliani

      212




      212




      New contributor




      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Demetre Saghliani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          5
          down vote













          One way to implement an effect like this is with the stencil buffer.



          This is an extra few bits the renderer keeps around for every pixel in a render target, that you can use for scratch notes about what you've drawn there or are allowed to draw later.



          You can create a new shader using one of Unity's built-in templates, and add some stencil operations to the top of the SubShader or Pass section:



          Stencil 
          Ref 1
          Comp always
          Pass replace



          This says "write a 1 into the stencil buffer, no matter what was there before" - put this on the shader/material that you use to render your light beam quad, and it will mark all the on-screen pixels it's touched.



          Then, for objects that should only show up inside the light beam, use:



          Stencil 
          Ref 1
          Comp equal



          This checks to see if the magic number 1 got written into this pixel previously (ie. is this pixel illuminated by the light beam?) - if not, the shader aborts rendering this pixel of the object, neatly clipping it to just the illuminated region of your screen.



          Make sure your objects that read the stencil information render later in the queues than the objects that write it, so the information is ready for them when they look for it.






          share|improve this answer




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "53"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            Demetre Saghliani is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f163978%2fhow-can-i-implement-a-revealing-light-beam%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            One way to implement an effect like this is with the stencil buffer.



            This is an extra few bits the renderer keeps around for every pixel in a render target, that you can use for scratch notes about what you've drawn there or are allowed to draw later.



            You can create a new shader using one of Unity's built-in templates, and add some stencil operations to the top of the SubShader or Pass section:



            Stencil 
            Ref 1
            Comp always
            Pass replace



            This says "write a 1 into the stencil buffer, no matter what was there before" - put this on the shader/material that you use to render your light beam quad, and it will mark all the on-screen pixels it's touched.



            Then, for objects that should only show up inside the light beam, use:



            Stencil 
            Ref 1
            Comp equal



            This checks to see if the magic number 1 got written into this pixel previously (ie. is this pixel illuminated by the light beam?) - if not, the shader aborts rendering this pixel of the object, neatly clipping it to just the illuminated region of your screen.



            Make sure your objects that read the stencil information render later in the queues than the objects that write it, so the information is ready for them when they look for it.






            share|improve this answer
























              up vote
              5
              down vote













              One way to implement an effect like this is with the stencil buffer.



              This is an extra few bits the renderer keeps around for every pixel in a render target, that you can use for scratch notes about what you've drawn there or are allowed to draw later.



              You can create a new shader using one of Unity's built-in templates, and add some stencil operations to the top of the SubShader or Pass section:



              Stencil 
              Ref 1
              Comp always
              Pass replace



              This says "write a 1 into the stencil buffer, no matter what was there before" - put this on the shader/material that you use to render your light beam quad, and it will mark all the on-screen pixels it's touched.



              Then, for objects that should only show up inside the light beam, use:



              Stencil 
              Ref 1
              Comp equal



              This checks to see if the magic number 1 got written into this pixel previously (ie. is this pixel illuminated by the light beam?) - if not, the shader aborts rendering this pixel of the object, neatly clipping it to just the illuminated region of your screen.



              Make sure your objects that read the stencil information render later in the queues than the objects that write it, so the information is ready for them when they look for it.






              share|improve this answer






















                up vote
                5
                down vote










                up vote
                5
                down vote









                One way to implement an effect like this is with the stencil buffer.



                This is an extra few bits the renderer keeps around for every pixel in a render target, that you can use for scratch notes about what you've drawn there or are allowed to draw later.



                You can create a new shader using one of Unity's built-in templates, and add some stencil operations to the top of the SubShader or Pass section:



                Stencil 
                Ref 1
                Comp always
                Pass replace



                This says "write a 1 into the stencil buffer, no matter what was there before" - put this on the shader/material that you use to render your light beam quad, and it will mark all the on-screen pixels it's touched.



                Then, for objects that should only show up inside the light beam, use:



                Stencil 
                Ref 1
                Comp equal



                This checks to see if the magic number 1 got written into this pixel previously (ie. is this pixel illuminated by the light beam?) - if not, the shader aborts rendering this pixel of the object, neatly clipping it to just the illuminated region of your screen.



                Make sure your objects that read the stencil information render later in the queues than the objects that write it, so the information is ready for them when they look for it.






                share|improve this answer












                One way to implement an effect like this is with the stencil buffer.



                This is an extra few bits the renderer keeps around for every pixel in a render target, that you can use for scratch notes about what you've drawn there or are allowed to draw later.



                You can create a new shader using one of Unity's built-in templates, and add some stencil operations to the top of the SubShader or Pass section:



                Stencil 
                Ref 1
                Comp always
                Pass replace



                This says "write a 1 into the stencil buffer, no matter what was there before" - put this on the shader/material that you use to render your light beam quad, and it will mark all the on-screen pixels it's touched.



                Then, for objects that should only show up inside the light beam, use:



                Stencil 
                Ref 1
                Comp equal



                This checks to see if the magic number 1 got written into this pixel previously (ie. is this pixel illuminated by the light beam?) - if not, the shader aborts rendering this pixel of the object, neatly clipping it to just the illuminated region of your screen.



                Make sure your objects that read the stencil information render later in the queues than the objects that write it, so the information is ready for them when they look for it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 6 hours ago









                DMGregory♦

                53k1198150




                53k1198150




















                    Demetre Saghliani is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    Demetre Saghliani is a new contributor. Be nice, and check out our Code of Conduct.












                    Demetre Saghliani is a new contributor. Be nice, and check out our Code of Conduct.











                    Demetre Saghliani is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f163978%2fhow-can-i-implement-a-revealing-light-beam%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery