유니티 엔진 (Unity Engine)

[Unity] 간단한 가우시안 블러(GaussianBlur) 셰이더 코드

원소랑 2024. 3. 14. 19:55
728x90

텍스처에 가우시안 블러 처리를 하면 요런 결과물이 생성됨.

아래는 머티리얼 속성. Blur Radius 를 조절해서 Blur 강도를 조절할 수 있다.

간단한 가우시안 블러 셰이더 코드

Shader "Custom/GaussianBlur"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurRadius ("Blur Radius", Range(0.0, 10.0)) = 1.0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            
            sampler2D _MainTex;
            float _BlurRadius;
            float _BlurTexelSize;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
            
            // Function to calculate Gaussian weight
            float Gaussian(float x, float deviation)
            {
                return exp(-(x * x) / (2 * deviation * deviation)) / (deviation * sqrt(2 * UNITY_PI));
            }

            float4 frag(v2f i) : SV_Target
            {
                // Compute the texel size for blurring
                float2 texelSize = 1.0 / _ScreenParams.xy;

                // Gaussian blur calculation
                float4 color = float4(0, 0, 0, 0);
                float totalWeight = 0.0;

                // Horizontal blur pass
                for (int x = -5; x <= 5; x++)
                {
                    float2 offset = float2(x, 0) * texelSize * _BlurRadius;
                    color += tex2D(_MainTex, i.uv + offset) * Gaussian(x, _BlurRadius);
                    totalWeight += Gaussian(x, _BlurRadius);
                }

                // Normalize color
                color /= totalWeight;

                return color;
            }
            ENDCG
        }
    }
}

 

 


게임 개발에 필수적인 내용을 담는 명서들을 소개합니다.

 

<유니티 교과서 개정6판>(유니티 최신 버전)
https://link.coupang.com/a/be3P0t

 

유니티 교과서 개정6판

COUPANG

www.coupang.com

 

<대마왕의 유니티 URP 셰이더 그래프 스타트업>

https://link.coupang.com/a/bs8qyC

 

대마왕의 유니티 URP 셰이더 그래프 스타트업

COUPANG

www.coupang.com


<리얼-타임 렌더링(REAL-TIME RENDERING) 4/e>
https://link.coupang.com/a/8VWas

 

리얼-타임 렌더링 4/e

COUPANG

www.coupang.com

 

<이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것>
https://link.coupang.com/a/9BqLd

 

이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것

COUPANG

www.coupang.com

 

유니티 에셋 스토어 링크
https://assetstore.unity.com?aid=1011lvz7h

 

에셋스토어

여러분의 작업에 필요한 베스트 에셋을 찾아보세요. 유니티 에셋스토어가 2D, 3D 모델, SDK, 템플릿, 툴 등 여러분의 콘텐츠 제작에 날개를 달아줄 다양한 에셋을 제공합니다.

assetstore.unity.com

(링크를 통해 도서/에셋 구입시 일정액의 수수료를 지급받습니다.)


 

728x90
반응형