Unity 后期特效用到的一些方法:OnRenderImage Blit

1.OnRenderImage (RenderTexture source, RenderTexture destination)

当这个函数被调用时,所有的渲染已经完成,渲染结果以参数source传入到函数中。它允许你对最终的显示图像进行处理。所以说是后期特效。

处理的最终结果会输入到destination中。如果相机上添加多个后处理脚本则会依次执行,而且前一个的destination会用作下一个的source。

2.Graphics.Blit

public static void Blit(Texture sourceRenderTexture destMaterial mat, int pass = -1);

有三个重载方法,这个方法的参数是最全的。

如果mat不为空,那么source被设置为mat中的shader的_MainTex,因此必须在shader开头的Properties块中定义_MainTex属性.

该方法使用mat中的shader绘制一个全屏的quad

Note that if you want to use depth or stencil buffer that is part of the source (Render)texture, you'll have to do equivalent of Blit functionality manually - i.e.Graphics.SetRenderTarget with destination color buffer and source depth buffer, setup orthographic projection (GL.LoadOrtho), setup material pass (Material.SetPass) and draw a quad (GL.Begin).

由上面这段话可以看出:Blit的等价方法是依次使用Graphics.SetRenderTargetGL.LoadOrthoMaterial.SetPass

GL.Begin(GL.QUADS);(GL.QUADS);

所以说,最后实际渲染的是一个以mat为材质,以source为_MainTex的全屏大小的quad,渲染该quad的摄像机为正交的。渲染的结果输出到destination,destination就是rendertarget.

网上某人写的,大致应该就是这样吧,没研究

 static public void Blit(RenderBuffer COLOR,RenderBuffer DEPTH, Material MRTMat,int pass)
        {
                Graphics.SetRenderTarget(COLOR, DEPTH);
                RenderQuad (MRTMat, pass);
        }

static public void RenderQuad( Material MRTMat,int pass)
        {

                GL.PushMatrix();
                GL.LoadOrtho();
                MRTMat.SetPass(pass);
                GL.Begin(GL.QUADS);
                GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 1.0f, 0.1f);//设置顶点,这样可以全屏,(0,1)代表屏幕左上角
                GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(1.0f, 1.0f, 0.1f);  //右上角              
                GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(1.0f, 0.0f, 0.1f);  //右下角              
                GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, 0.0f, 0.1f);  //左下角              
                GL.End();
                GL.PopMatrix();
}

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

3.

Texture size

{TextureName}_TexelSize - a float4 property contains texture size information:

  • x contains 1.0/width
  • y contains 1.0/height
  • z contains width
  • w contains height

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值