Unity3D/Shader

unity shader input screenPos

일등하이 2021. 11. 25. 18:02
반응형

스크린 UV

Shader "Custom/TestRefraction"
{
    Properties
    {
        
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
     
        //https://docs.unity3d.com/kr/530/Manual/SL-GrabPass.html
        //GrabPass는 특별한 패스 타입으로, 그려질 때 화면의 내용을 텍스처 내에 잡아둡니다. 
        //Grab the screen behind the object into _GrabTexture
        GrabPass { }    

        CGPROGRAM
        #pragma surface surf _NoLight noambient

        sampler2D _GrabTexture; // GrabPass에서 캡쳐한 화면을 텍스쳐로 사용가능 
        struct Input
        {
            //https://docs.unity3d.com/kr/530/Manual/SL-SurfaceShaders.html
            //반사 효과의 스크린스페이스 위치 또는 스크린스페이스 효과를 포함합니다.(현재 화면의 UV)
            float4 screenPos; 
            float4 color:COLOR;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            o.Emission = IN.screenPos.rgb;
        }

        float4 Lighting_NoLight(SurfaceOutput o, float3 lightDir, float atten)
        {
            return float4(0, 0, 0, 1);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

카메라 거리에 따라 달라짐 
앞으로 이동하면 이렇게 
뒤로 이동하면 이렇게 

Shader "Custom/TestRefraction"
{
    Properties
    {
        
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
     
        //https://docs.unity3d.com/kr/530/Manual/SL-GrabPass.html
        //GrabPass는 특별한 패스 타입으로, 그려질 때 화면의 내용을 텍스처 내에 잡아둡니다. 
        //Grab the screen behind the object into _GrabTexture
        GrabPass { }    

        CGPROGRAM
        #pragma surface surf _NoLight noambient

        sampler2D _GrabTexture; // GrabPass에서 캡쳐한 화면을 텍스쳐로 사용가능 
        struct Input
        {
            //https://docs.unity3d.com/kr/530/Manual/SL-SurfaceShaders.html
            //반사 효과의 스크린스페이스 위치 또는 스크린스페이스 효과를 포함합니다.(현재 화면의 UV)
            float4 screenPos; 
            float4 color:COLOR;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            //카메라 거리 영향을 제거 해줌 
            float3 screenUV = IN.screenPos.rgb / IN.screenPos.a;
            o.Emission = float3(screenUV.xy, 0);
        }

        float4 Lighting_NoLight(SurfaceOutput o, float3 lightDir, float atten)
        {
            return float4(0, 0, 0, 1);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

또는 

float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
o.Emission = float3(screenUV.xy, 0);
카메라 거리에 따라 달라지는 경우 

 

카메라 거리에 따라 달라지지 않게 한경우 

 

Shader "Custom/TestRefraction"
{
    Properties
    {
        _MainTex ("Main Texture", 2D) = "white" {}
        _RefractionStrength ("Refraction Strength", Range(0, 0.1)) = 0.05
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }
        zwrite off  //쿼드는 다른 일반 오브젝트들보다 나중에 그려져야 배경을 캡쳐 할수 있음으로 zwrite를 off해줌
     
        //https://docs.unity3d.com/kr/530/Manual/SL-GrabPass.html
        //GrabPass는 특별한 패스 타입으로, 그려질 때 화면의 내용을 텍스처 내에 잡아둡니다. 
        //Grab the screen behind the object into _GrabTexture
        GrabPass { }    

        CGPROGRAM
        #pragma surface surf _NoLight noambient alpha:fade

        sampler2D _MainTex;
        float _RefractionStrength;
        sampler2D _GrabTexture; // GrabPass에서 캡쳐한 화면을 텍스쳐로 사용가능 

        struct Input
        {
            //https://docs.unity3d.com/kr/530/Manual/SL-SurfaceShaders.html
            //반사 효과의 스크린스페이스 위치 또는 스크린스페이스 효과를 포함합니다.(현재 화면의 UV)
            float4 screenPos; 
            float4 color:COLOR;
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o)
        {
            //카메라 거리 영향을 제거 해줌 
            float4 c = tex2D(_MainTex, IN.uv_MainTex);
            float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
            float4 grab = tex2D(_GrabTexture, screenUV.xy + c.r * _RefractionStrength);
            //o.Emission = float3(screenUV.xy, 0);
            o.Emission = grab.rgb;

            //o.Emission = float3(IN.screenPos.xy, 0);
        }

        float4 Lighting_NoLight(SurfaceOutput o, float3 lightDir, float atten)
        {
            return float4(0, 0, 0, 1);
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

 

참고

https://darkcatgame.tistory.com/80

반응형