当前位置:   article > 正文

【OpenGL】Shader实例分析(九)- AngryBots中的主角受伤特效

【OpenGL】Shader实例分析(九)- AngryBots中的主角受伤特效

转发请保持地址:http://blog.csdn.net/stalendp/article/details/40859441

AngryBots是Unity官方的一个非常棒的例子,很有研究价值。以前研究的时候,由于其内容丰富,一时间不知道从哪入手写文章分析。这一段时间研究shader技术比较多一些,就从shader的这一方面开始吧。首先分析其中的一个屏幕特效:当主角受到攻击时会出现的全屏效果(postScreenEffect),效果如下:

  

其实这是一种的Bloom效果,相关文件有:MobileBloom.js 和 MobileBloom.shader;关于如何查看这两个文件,请参考下图:


JS代码分析

MobileBloom.js部分代码如下:

  1. function OnRenderImage (source : RenderTexture, destination : RenderTexture) {
  2. #if UNITY_EDITOR
  3. FindShaders ();
  4. CheckSupport ();
  5. CreateMaterials ();
  6. #endif
  7. agonyTint = Mathf.Clamp01 (agonyTint - Time.deltaTime * 2.75f);
  8. var tempRtLowA : RenderTexture = RenderTexture.GetTemporary (source.width / 4, source.height / 4, rtFormat);
  9. var tempRtLowB : RenderTexture = RenderTexture.GetTemporary (source.width / 4, source.height / 4, rtFormat);
  10. // prepare data
  11. apply.SetColor ("_ColorMix", colorMix);
  12. apply.SetVector ("_Parameter", Vector4 (colorMixBlend * 0.25f, 0.0f, 0.0f, 1.0f - intensity - agonyTint));
  13. // downsample & blur
  14. Graphics.Blit (source, tempRtLowA, apply, agonyTint < 0.5f ? 1 : 5);
  15. Graphics.Blit (tempRtLowA, tempRtLowB, apply, 2);
  16. Graphics.Blit (tempRtLowB, tempRtLowA, apply, 3);
  17. // apply
  18. apply.SetTexture ("_Bloom", tempRtLowA);
  19. Graphics.Blit (source, destination, apply, QualityManager.quality > Quality.Medium ? 4 : 0);
  20. RenderTexture.ReleaseTemporary (tempRtLowA);
  21. RenderTexture.ReleaseTemporary (tempRtLowB);
  22. }

知识点准备

1)OnRenderImage函数

这是一个回调函数,是MonoBehaviour的生命周期的一部分,每一帧都会被调用;当这个函数被调用时,所有的3d渲染已经完成,渲染结果以参数source传入到函数中,后期效果的实现就是对source的处理,并把结果整合到destination中。这个函数所在的脚本一般绑定在Camera上。此函数只有在Unity Pro版本中才能够使用。

2)Graphics.Blit函数

  1. static void Blit(Texture source, RenderTexture dest);
  2. static void Blit(Texture source, RenderTexture dest, Material mat, int pass = -1);
  3. static void Blit(Texture source, Material mat, int pass = -1);

这个函数就像过转化器一样,source图片经过它的处理变成了dest图片,其中材质对象mat负责算法实施(更准确的说法:算法是绑定到该mat上的shader来实现的。shader可以有多个pass,可以通过pass参数指定特定的shader,-1表示执行这个shader上所有的pass)

3)RenderTexture.GetTemporary函数

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/636982
推荐阅读
相关标签
  

闽ICP备14008679号