카테고리 없음

URP 외곡하기

소나무꼴 2022. 7. 22. 18:04

아래 처럼 따로 URP 셋팅을 해준 상태에서 

 

LayerMask 는 외곡되는 오브젝트의 Layer 을 포함되면 안된다.

 

Shader "Unlit/Temp"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Refraction ("Refraction", float) = 1
    }
    SubShader
    {
        Tags { 
            "RenderPipeline" = "UniversalPipeline"
            //"RenderType"="Opaque" 
            "RenderType"="Transparent"
            // "Queue"="Geometry+0"
            "Queue"="Transparent"
        }

        Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha

        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 baseUV : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 screenPos : TEXCOORD1;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);

            UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
                UNITY_DEFINE_INSTANCED_PROP(float4, _MainTex_ST)
                UNITY_DEFINE_INSTANCED_PROP(float4, _BaseColor)
                float _Refraction;
            UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)

            float3 UnpackNormalmapRGorAG(float4 packednormal)
            {
                // This do the trick
                packednormal.x *= packednormal.w;

                float3 normal;
                normal.xy = packednormal.xy * 2 - 1;
                normal.z = sqrt(1 - saturate(dot(normal.xy, normal.xy)));
                return normal;
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = TransformObjectToHClip(v.vertex);

                float4 baseST = UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, _MainTex_ST);
                o.uv = v.baseUV * baseST.xy + baseST.zw;
                o.screenPos = ComputeScreenPos(o.vertex);
                return o;
            }

            half4 frag(v2f input) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(input);
                float4 baseMapRGBA = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
                float3 NormalColor = UnpackNormalmapRGorAG(baseMapRGBA) * _Refraction;
                
                float4 color = float4(1,1,1,1);

                color.rgb = SampleSceneColor(input.screenPos.xy + (NormalColor.xy));

                return color;
            }
            ENDHLSL
        }
    }
}