본문 바로가기
Shader

Radial blur for Unity

by 대마왕J 2016. 11. 8.

Radialblur2.cs

RadialBlur2.shader



//shader 


Shader "Hidden/radial_blur" { 

Properties { 

    _MainTex ("Base (RGB)", 2D) = "white" {} 

    fSampleDist("fSampleDist", float) = 1

    fSampleStrength("fSampleStrength", float) = 1

}


SubShader 

    Pass 

    { 

        ZTest Always Cull Off ZWrite Off 

        Fog { Mode off }


CGPROGRAM 

// Upgrade NOTE: excluded shader from Xbox360 and OpenGL ES 2.0 because it uses unsized arrays 

#pragma exclude_renderers xbox360 gles 

#pragma exclude_renderers xbox360 

#pragma target 3.0 

#pragma vertex vert 

#pragma fragment frag 

#pragma fragmentoption ARB_precision_hint_fastest


#include "UnityCG.cginc"


uniform sampler2D _MainTex; 

uniform float fSampleDist; 

uniform float fSampleStrength; 

uniform float4 _MainTex_ST; 

uniform float4 _MainTex_TexelSize;

//static const float sam[11] = { -0.08,-0.05,-0.03,-0.02,-0.01,0,0.01,0.02,0.03,0.05,0.08 };


struct v2f { 

    float4 pos : POSITION; 

    float2 uv : TEXCOORD0; 

};


v2f vert (appdata_img v) 

    v2f o; 

    o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 

    o.uv = v.texcoord.xy; 

    return o; 

}


float4 frag (v2f i) : COLOR 

    float2 texCo = i.uv;

    float2 dir = 0.5 - texCo; 

    float dist = length(dir);

  

    dir /= dist; 

    float4 color = tex2D(_MainTex, texCo); 

    float4 sum = color;


    float samples[11] = { -0.08,-0.05,-0.03,-0.02,-0.01,0,0.01,0.02,0.03,0.05,0.08 };



//    float samples[10] = float[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08); 


    for (int i = 0; i < 10; ++i)  

    {  

        sum += tex2D(_MainTex, texCo + dir*samples[i]*fSampleDist);

//         sum += tex2D(_MainTex, texCo + dir*samples[i]*0.5);  

    }


    sum /= 11.0; 

    float t = saturate(dist * fSampleStrength); 

//     float t = saturate(dist * 1);

    return lerp(color, sum,t); 

//return float4 (1,0,0,1);

ENDCG


    } 

}


Fallback off


}






//cs



using System;

using UnityEngine;


namespace UnityStandardAssets.ImageEffects

{

    [ExecuteInEditMode]

    [RequireComponent (typeof(Camera))]

[AddComponentMenu ("Image Effects/Bloom and Glow/Radialblur2)")]

public class Radialblur2 : PostEffectsBase

    {


//        public enum Resolution

// {

//            Low = 0,

//            High = 1,

//        }


        public enum BlurType

{

            Standard = 0,

            Sgx = 1,

        }


        [Range(0.0f, 2.0f)]

public float fSampleDist = 1.0f;

        [Range(0.0f, 10.0f)]

public float fSampleStrength = 1.0f;


//        [Range(0.25f, 5.5f)]

//        public float blurSize = 1.0f;

//

//        Resolution resolution = Resolution.Low;

//        [Range(1, 4)]

//        public int blurIterations = 1;

//

//        public BlurType blurType= BlurType.Standard;


        public Shader fastBloomShader = null;

        private Material fastBloomMaterial = null;



        public override bool CheckResources ()

{

            CheckSupport (false);


            fastBloomMaterial = CheckShaderAndCreateMaterial (fastBloomShader, fastBloomMaterial);


            if (!isSupported)

                ReportAutoDisable ();

            return isSupported;

        }


        void OnDisable ()

{

            if (fastBloomMaterial)

                DestroyImmediate (fastBloomMaterial);

        }


        void OnRenderImage (RenderTexture source, RenderTexture destination)

{

            if (CheckResources() == false)

{

                Graphics.Blit (source, destination);

                return;

            }





//            fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod, 0.0f, threshold, intensity));

fastBloomMaterial.SetFloat ("fSampleDist", fSampleDist);

fastBloomMaterial.SetFloat ("fSampleStrength", fSampleStrength);

source.filterMode = FilterMode.Bilinear;




            // downsample

RenderTexture rt = RenderTexture.GetTemporary (source.width/2, source.height/2, 0, source.format);

            rt.filterMode = FilterMode.Bilinear;

Graphics.Blit (source, rt);

Graphics.Blit (rt, destination, fastBloomMaterial);



//            var passOffs= blurType == BlurType.Standard ? 0 : 2;

//

//            for(int i = 0; i < blurIterations; i++)

// {

//                fastBloomMaterial.SetVector ("_Parameter", new Vector4 (blurSize * widthMod + (i*1.0f), 0.0f, threshold, intensity));

//

//                // vertical blur

//                RenderTexture rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);

//                rt2.filterMode = FilterMode.Bilinear;

//                Graphics.Blit (rt, rt2, fastBloomMaterial, 2 + passOffs);

//                RenderTexture.ReleaseTemporary (rt);

//                rt = rt2;

//

//                // horizontal blur

//                rt2 = RenderTexture.GetTemporary (rtW, rtH, 0, source.format);

//                rt2.filterMode = FilterMode.Bilinear;

//                Graphics.Blit (rt, rt2, fastBloomMaterial, 3 + passOffs);

//                RenderTexture.ReleaseTemporary (rt);

//                rt = rt2;

//            }

//

//            fastBloomMaterial.SetTexture ("_Bloom", rt);

//

//            Graphics.Blit (source, destination, fastBloomMaterial, 0);


            RenderTexture.ReleaseTemporary (rt);

        }

    }

}




반응형

'Shader ' 카테고리의 다른 글

_LightColor0 란 무엇인가요?  (0) 2017.04.13
lerp 공식  (2) 2017.03.26
Rotation UV  (0) 2016.10.23
레이마칭 테스트  (0) 2016.08.01
Volumetric Rendering: Surface Shading  (0) 2016.07.28

댓글