透明物体的渲染
2016-06-26 10:33
573 查看
前段时间涉及到一个功能的开发,游戏中当模型首次出现在场景中加载的时候,需要有渐变出现的效果。当时是对模型材质进行替换为透明材质,然后对透明材质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,之后使用深度检测方法(小于等于)剥离该部分 ; 重复上述步骤
补充下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
---------------------------------------------------------分割线--------------------------------------------------------------
透明物体渲染会有什么问题?
如下图的对比所示,右边的透明物体渲染可以看出有穿插的部分。因为对于复杂的模型,本身即有层次性,另外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
相关文章推荐
- 四季 —— 一种天文现象
- 百钱百鸡问题 枚举 与 不定方程解法
- link to cross the Great Wall to google
- java可视化编程 eclipse安装windowBuilder插件
- 备城门
- Akka初步介绍
- 人脸识别:特征脸(Eigenface)
- Android之Android Studio 快捷键整理分享
- 剑指offer37--找出数组中只出现1次的两个数
- 积木城堡
- 如何产生好的词向量
- 数据挖掘算法详解博客整理
- acm之贪心算法题目10
- unity人物场景切换到指定坐标
- 巫马子问道:
- 高德地图——地理围栏
- Flask: Ajax 设置Access-Control-Allow-Origin实现跨域访问;Ajax页面底部自动加载
- 解决SourceInsight不能解析标准C函数问题
- Ubuntu删除文件夹的命令
- 用ps制作淘宝产品主背景图