透明物体的渲染

前段时间涉及到一个功能的开发,游戏中当模型首次出现在场景中加载的时候,需要有渐变出现的效果。当时是对模型材质进行替换为透明材质,然后对透明材质alpha做曲线变换。这个过程看似比较顺利,但是对于怎么渲染透明物体确是没有深入的研究。以后开发中需要多深入的了解过程中的一些隐藏的部分,这样能力才会有更多的提高。

---------------------------------------------------------分割线--------------------------------------------------------------

透明物体渲染会有什么问题?

如下图的对比所示,右边的透明物体渲染可以看出有穿插的部分。因为对于复杂的模型,本身即有层次性,另外GPU中可能对顶点或片段有某些操作引起透明片段的渲染顺序不正确。


解决问题的方法

首先看看shadow mapping的渲染,从中得到启发

shadow mapping: 

Pass one: 从光的方向对场景渲染,保存此时的深度buffer

Pass two: 使用投影纹理把阴影图片投射到场景中,此时用到刚才的深度信息和距离r 比较,确定是否在阴影当中

depth peeling:

上述shadow mapping 方法得到的是靠近光源一层的深度,继续拓展下去,depth peeling 使用更多级的深度信息,n个pass可以得到n层的深度信息,如下所示,4个pass 之后得到4层的深度信息


更清晰的表示如下,先从最左边看,得到一个深度buffer,之后使用深度检测方法(小于等于)剥离该部分 ; 重复上述步骤


for(i=0; i< num_passes; i++)
{
     clear color buffer
     A = i%2
     B = (i+1)%2
     depth unit 0:
        if(i == 0)
           disable depth test
        else
            enable depth test
        bind buffer A
        disable depth writes
        set depth func to GREATER
     depth unit 1:
        bind buffer B
        clear depth buffer
        enable depth writes
        enable depth test
        set depth func to LESS
     render scene
     save color buffer RGBA as layeri
}


补充下OpenGL 中glsl 代码:


/*

* check if the point p(in clip-space )is behind the lst depth value renderd

* if so, the color is unchaged, if not, the color is set to the background color

* and  gl_FragDepth is adjusted

*/

void depthPeel(inout vec4 color, in vec4 p)

{

   vec4 clipCoord = (p/p.w + 1.0)/2.0;

   vec2 uv = clipCoord.xy;


   float currDepth = gl_FragCoord.z - 1e-4;

   flaot  lastDepth = texexFetch(depthPeelMap, ivec2(uv * textureSize(depthPeelMap,0)),0).r;

  

   // step is a campare function, return 0 or 1(bigger)

   float discardFrag = step(currDepth, lastDepth);

   // mix = color* (1- discardFrag)+ Back * discardFrag

   color = mix(color, BACKGROUND_COLOR, discardFrag);

   gl_FragDepth = mix(gl_FragCoord.z, 1.0, discardFrag);

}


Refference:

eng.utah.edu/~cs5610/handouts/order_independent_transparency.pdf


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不负初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值