zoukankan      html  css  js  c++  java
  • Unity shader学习之屏幕后期处理效果之运动模糊

    运动模糊,代码如下:

     1 using UnityEngine;
     2 
     3 public class MotionBlurRenderer : PostEffectRenderer
     4 {
     5     [Range(0.1f, 0.9f)]
     6     [SerializeField]
     7     float m_blurAmount = 0.1f;
     8 
     9     RenderTexture m_accumulationTexture;
    10 
    11     void OnDisable()
    12     {
    13         DestroyImmediate(m_accumulationTexture);
    14     }
    15 
    16     protected override void OnRenderImage(RenderTexture src, RenderTexture dest)
    17     {
    18         if (!m_accumulationTexture || m_accumulationTexture.width != src.width || m_accumulationTexture.height != src.height)
    19         {
    20             DestroyImmediate(m_accumulationTexture);
    21             m_accumulationTexture = new RenderTexture(src.width, src.height, 0);
    22             //m_accumulationTexture.hideFlags = HideFlags.HideAndDontSave;
    23             Graphics.Blit(src, m_accumulationTexture);
    24         }
    25 
    26         //m_accumulationTexture.MarkRestoreExpected();
    27 
    28         Mat.SetFloat("_BlurAmount", m_blurAmount);
    29 
    30         Graphics.Blit(src, m_accumulationTexture, Mat);
    31         Graphics.Blit(m_accumulationTexture, dest);
    32 
    33         base.OnRenderImage(src, dest);
    34     }
    35 
    36     protected override string ShaderName
    37     {
    38         get { return "Custom/Study/Motion Shader"; }
    39     }
    40 }
    MotionBlurRenderer

    对应shader如下:

     1 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
     2 
     3 Shader "Custom/Study/Motion Shader"
     4 {
     5     Properties
     6     {
     7         _MainTex ("Main Texture", 2D) = "white" {}
     8         _BlurAmount ("Blur Amount", Range(0,1)) = 0.5
     9     }
    10 
    11     SubShader
    12     {
    13         ZWrite Off
    14         ZTest Always
    15         Cull Off
    16 
    17         Pass
    18         {
    19             Blend SrcAlpha OneMinusSrcAlpha
    20 
    21             CGPROGRAM
    22             #pragma vertex vert
    23             #pragma fragment frag
    24 
    25             sampler2D _MainTex;
    26             fixed _BlurAmount;
    27 
    28             struct appdata
    29             {
    30                 float4 vertex : POSITION;
    31                 float2 uv : TEXCOORD0;
    32             };
    33 
    34             struct v2f
    35             {
    36                 float4 pos : SV_POSITION;
    37                 float2 uv : TEXCOORD0;
    38             };
    39 
    40             v2f vert(appdata v)
    41             {
    42                 v2f o;
    43                 o.pos = UnityObjectToClipPos(v.vertex);
    44                 o.uv = v.uv;
    45                 return o;
    46             }
    47 
    48             fixed4 frag(v2f i) : SV_TARGET
    49             {
    50                 fixed4 tex = tex2D(_MainTex, i.uv);
    51                 return fixed4(tex.rgb, _BlurAmount);
    52             }
    53 
    54             ENDCG
    55         }
    56     }
    57 
    58     Fallback Off
    59 }
    Custom/Study/Motion Shader

    效果如下:

     

  • 相关阅读:
    PPP点对点协议
    Wireshark包过滤
    asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
    sql学习笔记(三)—— 联表查询
    Sql学习笔记(二)—— 条件查询
    bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序
    汕头市队赛 SRM19 字符题
    bzoj 2037: [Sdoi2008]Sue的小球——dp
    bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形——极角排序
    【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
  • 原文地址:https://www.cnblogs.com/jietian331/p/7278481.html
Copyright © 2011-2022 走看看