Posts Depth Texture Renderer (Z-Buffer 렌더러)
Post
Cancel

Depth Texture Renderer (Z-Buffer 렌더러)

Note


  • 현재 메인 카메라의 뎁스 텍스쳐를 화면에 렌더링한다.

  • 각 쉐이더의 ZWrite 여부에 관계 없이 모두 렌더링하는 단점이 있다.

  • 렌더 큐 값이 2500(Skybox) 이하인 오브젝트들만 인식한다.


How to Use


  • DepthRenderer.cs 스크립트를 메인 카메라에 넣는다.

  • enabled를 체크/해제하여 화면에 렌더 텍스쳐를 보여줄지 여부를 결정할 수 있다.


Preview


  • 원래 화면

image

  • 뎁스 텍스쳐 렌더링

image


Download



Source Code


RenderDepth.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 Shader "Custom/RenderDepth"
 {
     Properties
     {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _DepthLevel ("Depth Level", Range(1, 3)) = 1
         _DepthMul ("Depth Mul", Range(-1, 1)) = 0
     }
     SubShader
     {
         Pass
         {
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             
             uniform sampler2D _MainTex;
             uniform sampler2D _CameraDepthTexture;
             uniform fixed _DepthLevel;
             uniform fixed _DepthMul;
             uniform half4 _MainTex_TexelSize;
 
             struct input
             {
                 float4 pos : POSITION;
                 half2 uv : TEXCOORD0;
             };
 
             struct output
             {
                 float4 pos : SV_POSITION;
                 half2 uv : TEXCOORD0;
             };
 
 
             output vert(input i)
             {
                 output o;
                 o.pos = UnityObjectToClipPos(i.pos);
                 o.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, i.uv);
                 // why do we need this? cause sometimes the image I get is flipped. see: http://docs.unity3d.com/Manual/SL-PlatformDifferences.html
                 #if UNITY_UV_STARTS_AT_TOP
                 if (_MainTex_TexelSize.y < 0)
                         o.uv.y = 1 - o.uv.y;
                 #endif
 
                 return o;
             }
             
             fixed4 frag(output o) : COLOR
             {
                 float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, o.uv));
                 depth = pow(Linear01Depth(depth), _DepthLevel) * _DepthMul;
                 return depth;
             }
             
             ENDCG
         }
     } 
 }


DepthRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using UnityEngine;

[ExecuteInEditMode]
public class DepthRenderer : MonoBehaviour
{
    [Range(0f, 3f)]
    public float depthLevel = 1.0f;

    [Range(1f, 50f)]
    public float depthMul = 20.0f;

    private Shader _shader;
    private Shader RdShader
        => _shader != null ? _shader : (_shader = Shader.Find("Custom/RenderDepth"));

    private Material _material;
    private Material RdMaterial
    {
        get
        {
            if (_material == null)
            {
                _material = new Material(RdShader);
                _material.hideFlags = HideFlags.HideAndDontSave;
            }
            return _material;
        }
    }

    private void OnEnable()
    {
        if (RdShader == null || !RdShader.isSupported)
        {
            enabled = false;
            print("Shader " + RdShader.name + " is not supported");
            return;
        }

        Camera.main.depthTextureMode = DepthTextureMode.None;
    }

    private void OnDisable()
    {
        if (_material != null)
            DestroyImmediate(_material);
    }

    private void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        if (RdShader != null)
        {
            RdMaterial.SetFloat("_DepthLevel", depthLevel);
            RdMaterial.SetFloat("_DepthMul", depthMul);
            Graphics.Blit(src, dest, RdMaterial);
        }
        else
        {
            Graphics.Blit(src, dest);
        }
    }
}


References


This post is licensed under CC BY 4.0 by the author.