做一款基于子弹时间的小游戏(视效篇一)

注:学习实践阶段,参考了unity shader 入门精要一书。

模糊效果

在进入慢速度状态时希望在视觉上呈现模糊效果,类似那种摄像机高速模糊,有残影的效果。
试了两种方案,都是基于后处理。一种采用不断叠加上一帧渲染纹理的方式,一种利用深度纹理重建世界坐标计算像素速度的方式进行渲染。但第二种方法的效果在该游戏上的效果并不适合,抖动太过剧烈。如图
在这里插入图片描述
而第一种实现的效果比较理想,如图(截图原因可能并不明显,仔细看能看到拖尾效果):
在这里插入图片描述
而且第一种方法实现更加简单,即利用一张渲染纹理来保存上一帧的处理结果,每一次处理通过将上一帧保存的渲染纹理和当前的渲染纹理进行混合叠加。

步骤

  • 定义一个高斯模糊系数:
[Range(0.0f, 0.9f)]
ublic float blurAmount = 0.5f;
  • 定义一个渲染纹理来保存前一帧混合纹理、
private RenderTexture accumulationTexture;
void OnDisable() 
{
   
   DestroyImmediate(accumulationTexture);//销毁
}
  • 实现OnRenderImage函数:
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
   
    if(material != null)
    {
   
        if (accumulationTexture == null || accumulationTexture.width != source.width || accumulationTexture.height != source.height) 
        {
   
            DestroyImmediate(accumulationTexture);
            accumulationTexture = new RenderTexture(source.width, source.height, 0);
            accumulationTexture.hideFlags = HideFlags.HideAndDontSave;
            Graphics.Blit(source, accumulationTexture);
        }
        //避免沒有清空 RenderTexture 時 Unity 跳出警告
        accumulationTexture.MarkRestoreExpected();
        material.SetFloat("_BlurAmount", 1.0f - blurAmount);
        Graphics.Blit (source, accumulationTexture, material,0);
        Graphics.Blit (accumulationTexture, destination
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值