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
6
down vote

favorite
2












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





























    up vote
    6
    down vote

    favorite
    2












    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

























      up vote
      6
      down vote

      favorite
      2









      up vote
      6
      down vote

      favorite
      2






      2





      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















      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















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 25 at 12:00









      DMGregory♦

      53.4k1198151




      53.4k1198151










      asked Sep 25 at 8:10









      Demetre Saghliani

      312




      312




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          8
          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
            );



            );













             

            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
            8
            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
              8
              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
                8
                down vote










                up vote
                8
                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 Sep 25 at 12:13









                DMGregory♦

                53.4k1198151




                53.4k1198151



























                     

                    draft saved


                    draft discarded















































                     


                    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













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay