Write to the depth buffer in a Unity 3D unlit shader?










0















I admit I'm a bit lost when it comes to shaders. Below is my unlit shader. What can I add to this to write to the depth buffer and depth texture? Also, am I doing anything wrong or unnecessary in this shader?



Shader "Unlit/Environment Texture" 
Properties
_MainTex ("Base (RGB)", 2D) = "white"
_MainTexBias ("Mip Bias (-1 to 1)", float) = -1


SubShader
Tags "RenderType"="Opaque"

Lighting Off
Fog Mode off

Pass
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata_t
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
;

struct v2f
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
;

sampler2D _MainTex;
float4 _MainTex_ST;
half _MainTexBias;

v2f vert (appdata_t v)

v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;


fixed4 frag (v2f i) : SV_Target

fixed4 col = tex2Dbias(_MainTex, half4(i.texcoord.x,i.texcoord.y,0.0,_MainTexBias));
return col;

ENDCG












share|improve this question




























    0















    I admit I'm a bit lost when it comes to shaders. Below is my unlit shader. What can I add to this to write to the depth buffer and depth texture? Also, am I doing anything wrong or unnecessary in this shader?



    Shader "Unlit/Environment Texture" 
    Properties
    _MainTex ("Base (RGB)", 2D) = "white"
    _MainTexBias ("Mip Bias (-1 to 1)", float) = -1


    SubShader
    Tags "RenderType"="Opaque"

    Lighting Off
    Fog Mode off

    Pass
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t
    float4 vertex : POSITION;
    float2 texcoord : TEXCOORD0;
    ;

    struct v2f
    float4 vertex : SV_POSITION;
    half2 texcoord : TEXCOORD0;
    ;

    sampler2D _MainTex;
    float4 _MainTex_ST;
    half _MainTexBias;

    v2f vert (appdata_t v)

    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    return o;


    fixed4 frag (v2f i) : SV_Target

    fixed4 col = tex2Dbias(_MainTex, half4(i.texcoord.x,i.texcoord.y,0.0,_MainTexBias));
    return col;

    ENDCG












    share|improve this question


























      0












      0








      0








      I admit I'm a bit lost when it comes to shaders. Below is my unlit shader. What can I add to this to write to the depth buffer and depth texture? Also, am I doing anything wrong or unnecessary in this shader?



      Shader "Unlit/Environment Texture" 
      Properties
      _MainTex ("Base (RGB)", 2D) = "white"
      _MainTexBias ("Mip Bias (-1 to 1)", float) = -1


      SubShader
      Tags "RenderType"="Opaque"

      Lighting Off
      Fog Mode off

      Pass
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag

      #include "UnityCG.cginc"

      struct appdata_t
      float4 vertex : POSITION;
      float2 texcoord : TEXCOORD0;
      ;

      struct v2f
      float4 vertex : SV_POSITION;
      half2 texcoord : TEXCOORD0;
      ;

      sampler2D _MainTex;
      float4 _MainTex_ST;
      half _MainTexBias;

      v2f vert (appdata_t v)

      v2f o;
      o.vertex = UnityObjectToClipPos(v.vertex);
      o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
      return o;


      fixed4 frag (v2f i) : SV_Target

      fixed4 col = tex2Dbias(_MainTex, half4(i.texcoord.x,i.texcoord.y,0.0,_MainTexBias));
      return col;

      ENDCG












      share|improve this question
















      I admit I'm a bit lost when it comes to shaders. Below is my unlit shader. What can I add to this to write to the depth buffer and depth texture? Also, am I doing anything wrong or unnecessary in this shader?



      Shader "Unlit/Environment Texture" 
      Properties
      _MainTex ("Base (RGB)", 2D) = "white"
      _MainTexBias ("Mip Bias (-1 to 1)", float) = -1


      SubShader
      Tags "RenderType"="Opaque"

      Lighting Off
      Fog Mode off

      Pass
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag

      #include "UnityCG.cginc"

      struct appdata_t
      float4 vertex : POSITION;
      float2 texcoord : TEXCOORD0;
      ;

      struct v2f
      float4 vertex : SV_POSITION;
      half2 texcoord : TEXCOORD0;
      ;

      sampler2D _MainTex;
      float4 _MainTex_ST;
      half _MainTexBias;

      v2f vert (appdata_t v)

      v2f o;
      o.vertex = UnityObjectToClipPos(v.vertex);
      o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
      return o;


      fixed4 frag (v2f i) : SV_Target

      fixed4 col = tex2Dbias(_MainTex, half4(i.texcoord.x,i.texcoord.y,0.0,_MainTexBias));
      return col;

      ENDCG









      unity3d shader






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 4:54







      jestro

















      asked Nov 14 '18 at 4:23









      jestrojestro

      1,30921734




      1,30921734






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Uhm... this should already write to the depth buffer? You can add "Queue"="Opaque" to the tags to enable front-to-back sorting, though i think it should default to this anyways. Ztest On is also implicit. How does it look for you in Unity?






          share|improve this answer






















            Your Answer






            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: "1"
            ;
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293165%2fwrite-to-the-depth-buffer-in-a-unity-3d-unlit-shader%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Uhm... this should already write to the depth buffer? You can add "Queue"="Opaque" to the tags to enable front-to-back sorting, though i think it should default to this anyways. Ztest On is also implicit. How does it look for you in Unity?






            share|improve this answer



























              0














              Uhm... this should already write to the depth buffer? You can add "Queue"="Opaque" to the tags to enable front-to-back sorting, though i think it should default to this anyways. Ztest On is also implicit. How does it look for you in Unity?






              share|improve this answer

























                0












                0








                0







                Uhm... this should already write to the depth buffer? You can add "Queue"="Opaque" to the tags to enable front-to-back sorting, though i think it should default to this anyways. Ztest On is also implicit. How does it look for you in Unity?






                share|improve this answer













                Uhm... this should already write to the depth buffer? You can add "Queue"="Opaque" to the tags to enable front-to-back sorting, though i think it should default to this anyways. Ztest On is also implicit. How does it look for you in Unity?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 17 at 10:45









                Kalle HalvarssonKalle Halvarsson

                1586




                1586



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293165%2fwrite-to-the-depth-buffer-in-a-unity-3d-unlit-shader%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    27

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    Category:Rhetoric