Unity3D Shader:高亮

C#代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bloom : MonoBehaviour {
    //全屏泛光
    public Material mat;

    public int downscale;//分辨率

    public int sampleScale = 1;//高斯模糊半径

    public Color colorHold = Color.gray;//颜色阙值,提取高光点

    public Color bloomColor = Color.white;//高光颜色

    [Range(0.0f, 1.0f)]
    public float bloomFactor = 0.5f;//高光权重值

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        RenderTexture temp1 = RenderTexture.GetTemporary(source.width >> downscale, source.height >> downscale, 0, source.format);
        RenderTexture temp2 = RenderTexture.GetTemporary(source.width >> downscale, source.height >> downscale, 0, source.format);

        Graphics.Blit(source, temp1);

        mat.SetVector("_colorHold", colorHold);
        Graphics.Blit(temp1, temp2, mat, 0);

        mat.SetVector("_offset", new Vector4(0, sampleScale, 0, 0));
        Graphics.Blit(temp2, temp1, mat, 1);
        mat.SetVector("_offset", new Vector4(sampleScale, 0, 0, 0));
        Graphics.Blit(temp1, temp2, mat, 1);

        mat.SetTexture("_BlurTex", temp2);
        mat.SetVector("_bloomColor", bloomColor);
        mat.SetFloat("_bloomFactor", bloomFactor);
        Graphics.Blit(source, destination, mat, 2);

        RenderTexture.ReleaseTemporary(temp1);
        RenderTexture.ReleaseTemporary(temp2);
    }
}


Shader代码:

Shader "Unlit/Test"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurTex("BlurTex",2D)="white"{}
    }
    CGINCLUDE
    #include "UnityCG.cginc"
    
    struct v2f_colorHold
    {
        float4 pos:SV_POSITION;
        float2 uv:TEXCOORD0;
    };

    struct v2f_blur
    {
        float4 pos:SV_POSITION;
        float2 uv:TEXCOORD0;
        float4 uv01:TEXCOORD1;
        float4 uv23:TEXCOORD2;
        float4 uv45:TEXCOORD3;
    };

    struct v2f_bloom
    {
        float4 pos:SV_POSITION;
        float2 uv:TEXCOORD0;
    };

    sampler2D _MainTex;
    float4 _MainTex_TexelSize;
    sampler _BlurTex;
    float4 _BlurTex_TexelSize;
    float4 _offset;
    float4 _colorHold;
    float4 _bloomColor;
    float _bloomFactor;
    
    v2f_colorHold vert_colorHold(appdata_img v)
    {
        v2f_colorHold o;
        o.pos=UnityObjectToClipPos(v.vertex);
        o.uv=v.vertex.xy;
        return o;
    }
    fixed4 frag_colorHold(v2f_colorHold i):SV_Target
    {
        fixed4 col=tex2D(_MainTex,i.uv);

        return saturate(col-_colorHold);
    }
    
    v2f_blur vert_blur(appdata_img v)
    {
        v2f_blur o;
        _offset*=_MainTex_TexelSize.xyxy;
        o.pos=UnityObjectToClipPos(v.vertex);
        o.uv=v.vertex.xy;

        o.uv01=v.vertex.xyxy+_offset*float4(1,1,-1,-1);
        o.uv23=v.vertex.xyxy+_offset*float4(1,1,-1,-1)*2;
        o.uv45=v.vertex.xyxy+_offset*float4(1,1,-1,-1)*3;
        return o;
    }
    fixed4 frag_blur(v2f_blur i):SV_Target
    {
        fixed4 col=fixed4(0,0,0,0);
        col+=0.4*tex2D(_MainTex,i.uv);
        col+=0.15*tex2D(_MainTex,i.uv01.xy);
        col+=0.15*tex2D(_MainTex,i.uv01.zw);
        col+=0.10*tex2D(_MainTex,i.uv23.xy);
        col+=0.10*tex2D(_MainTex,i.uv23.zw);
        col+=0.05*tex2D(_MainTex,i.uv45.xy);
        col+=0.05*tex2D(_MainTex,i.uv45.zw);
        return col;
    }

    v2f_bloom vert_bloom(appdata_img v)
    {
        v2f_bloom o;
        o.pos=UnityObjectToClipPos(v.vertex);
        o.uv=v.vertex.xy;
        return o;
    }
    fixed4 frag_bloom(v2f_bloom i):SV_Target
    {
        fixed4 col=tex2D(_MainTex,i.uv);

        fixed4 blur=tex2D(_BlurTex,i.uv);

        fixed4 final=col+_bloomFactor*blur*_bloomColor;
        return final;
    }
    ENDCG
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        //提取高光点
        Pass
        {
            ZTest Off
            ZWrite Off
            Cull Off
            CGPROGRAM
            #pragma vertex vert_colorHold
            #pragma fragment frag_colorHold

            ENDCG
        }
        //高斯模糊
        Pass
        {
            ZTest Off
            ZWrite Off
            Cull Off
            CGPROGRAM
            #pragma vertex vert_blur
            #pragma fragment frag_blur

            ENDCG
        }
        //bloom混合
        Pass
        {
            ZTest Off
            ZWrite Off
            Cull Off
            CGPROGRAM
            #pragma vertex vert_bloom
            #pragma fragment frag_bloom

            ENDCG
        }
    }
}


原图:

开启泛光:

猜你喜欢

转载自blog.csdn.net/weixin_42452001/article/details/81233617