본문 바로가기
Shader

깊이값 가지고 놀기

by 대마왕J 2018. 6. 1.

할 때 마다 잊어버려서 백업용. 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
Shader "ltest"
{
    SubShader
    {
    Tags{ "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf Lambert 
        struct Input
        {
            float4 screenPos;//스크린 포지션을 받읍시다
        };
        sampler2D _CameraDepthTexture;
        
        void surf( Input i , inout SurfaceOutput o ){
            float4 ScreenPos = float4( i.screenPos.xyz , i.screenPos.w + 0.00000000001 ); //0으로 나누는 사태만 방지하기 위해 작은 값을 넣습니다. 
            float4 screenPosNorm = ScreenPos / ScreenPos.w; // 퍼스펙티브 제거
            //screenPosNorm.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? screenPosNorm.z : screenPosNorm.z * 0.5 + 0.5;
            //near클립 값이 0보다 크면 냅두고 작으면 하프 램버트 처리
            float screenDepth= LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture,UNITY_PROJ_COORD(ScreenPos)))); // 깊이 텍스쳐를 스크린 좌표계를 이용해서 깊이값을 뽑아내고 그걸 리니어로 변환
            o.Emission  = 1- ( screenDepth - LinearEyeDepth( screenPosNorm.z ) );
            }
        ENDCG
    }
}
cs
Shader "ltest"
{
    SubShader
    {
    Tags{ "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf Lambert 
        struct Input
        {
            float4 screenPos;//스크린 포지션을 받읍시다
        };
        sampler2D _CameraDepthTexture;
        
        void surf( Input i , inout SurfaceOutput o ){
            float4 ScreenPos = float4( i.screenPos.xyz , i.screenPos.w + 0.00000000001 ); 
//0으로 나누는 사태만 방지하기 위해 작은 값을 넣습니다.
            float4 screenPosNorm = ScreenPos / ScreenPos.w; // 퍼스펙티브 제거
            //screenPosNorm.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? screenPosNorm.z : 
screenPosNorm.z * 0.5 + 0.5;
            //near클립 값이 0보다 크면 냅두고 작으면 하프 램버트 처리
            float screenDepth= LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture,
UNITY_PROJ_COORD(ScreenPos)))); 
// 깊이 텍스쳐를 스크린 좌표계를 이용해서 깊이값을 뽑아내고 그걸 리니어로 변환
            o.Emission  = 1- ( screenDepth - LinearEyeDepth( screenPosNorm.z ) );
            }
        ENDCG
    }
}

 

 

반응형

댓글