shader学习笔记——Looking Through A Filter 通过滤波器观察
来源:互联网 发布:医疗软件注册 编辑:程序博客网 时间:2024/04/28 20:10
渲染目标render target
所谓渲染目标,就是指GPU可以把画面绘制到的目标,我们可以把它理解为GPU的画布。
当场景不是被渲染在正常的屏幕后备缓冲,而是被渲染在一个临时的纹理。这个临时的纹理经过过滤特效处理后,然后传到后备缓冲,最后画到屏幕上。绘制这样的一个可以实现某些功能的临时纹理被称为渲染目标。
实质上,渲染目标是一个连续的内存区域,这样的内存区域可以同时的存在多个,也就是多个渲染目标。一般来说,渲染目标被输出到屏幕上,这样我们就能看到画好的画面了。但是有时为了实现一些特效,某些渲染结果并不直接画到屏幕上,而是再返给GPU做进一步的特效处理,而且渲染目标中也不一定是画好的画面的颜色信息。
要想把纹理目标的内容渲染到屏幕上,就必须把纹理目标投影到屏幕上。
纹理坐标
想把纹理覆盖全部。必须确保矩形的每个角的坐标是以下:(0,1)(1,0)(1,1)(0,0)
要想屏幕上的每一个像素都对应纹理的像素,必须要注意:
计算纹理坐标 // Texture coordinates are setup so that the full texture // is mapped completely onto the screen Out.texCoord.x = 0.5 * (1 + Pos.x); Out.texCoord.y = 0.5 * (1 - Pos.y); Combining the standard scale and offset with the texture correction offset yields the following code: // Texture coordinates are setup so that the full texture // is mapped completely onto the screen Out.texCoord.x = 0.5 * (1 + Pos.x - viewport_inv_width); Out.texCoord.y = 0.5 * (1 - Pos.y - viewport_inv_height); 光亮度(intensity)和灰阶(grayscale) 求光亮度最直观简单的方法是intensity = (green + blue + red)/3 但是这是有缺陷的,人眼所接收的颜色不是均等,根据研究表明,人眼对颜色的感知得出的光度公式为 intensity = 0.299 * red + 0.587 * Green + 0.114 * blue (注:在《shader for game programmers and Artists》的blue乘以的是0.184) Pixel shader代码 sampler Texture0; float4 ps_main( float4 inDiffuse: COLOR0, float2 inTxr1: TEXCOORD0 ) : COLOR0 { // Output constant color: float4 col = tex2D(Texture0,inTxr1); float Intensity; Intensity = 0.299 * col.r + 0.587 * col.g + 0.114 * col.b; //等同于上面 Intensity = dot(col,float4(0.299,0.587,0.114,0)); // Return the intensity as a uniform RGB color return float4(Intensity.xxx,col.a); } 注:用点乘dot一般效率会比直接用公式要高。 模糊(blur)和运动模糊 模糊纹理: 简单的模糊效果 取一个像素的四周的像素加起来求平均值:
因为模糊效果需要多次采样纹理,所以,最好定义一个常数向量数组: const float4 samples[4] = -1.0, 0, 0, 0.25, 1.0, 0, 0, 0.25, 0, -1.0, 0, 0.25, 0, 1.0 , 0, 0.25 pass blur的pixel shader代码: float viewport_inv_width; float viewport_inv_height; sampler Texture0; const float4 samples[4] = { -1.0, 0, 0, 0.25, 1.0, 0, 0, 0.25, 0, -1.0, 0, 0.25, 0, 1.0 , 0, 0.25 }; float4 ps_main(float2 texCoord: TEXCOORD) : COLOR { float4 col = float4(0,0,0,0); for(int i = 0; i < 4; i++) { col+=samples[i].w * tex2D(Texture0,texCoord + float2(samples[i].x * viewport_inv_width, samples[i].x * viewport_inv_height)); } return col; } 边缘检测(Edge detection) 在上面的模糊效果改下数组: float viewport_inv_width; float viewport_inv_height; sampler Texture0; const float4 samples[6] = { -1.0, 1.0, 0, 1.0, 0.0, 1.0, 0, 2.0, 1.0, 1.0, 0, 1.0, -1.0, -1.0, 0, -1.0, 0.0, -1.0, 0, -2.0, 1.0, -1.0, 0, -1.0 }; float4 ps_main(float2 texCoord: TEXCOORD) : COLOR { float4 col = float4(0,0,0,0); for(int i = 0; i < 6; i++) { col+=samples[i].w * tex2D(Texture0,texCoord + float2(samples[i].x * viewport_inv_width, samples[i].y * viewport_inv_height)); } return col; } 锐化(sharp color) 在上面的模糊效果改下数组: float viewport_inv_width; float viewport_inv_height; sampler Texture0; const float4 samples[5] = { 0.0, 0.0, 0, 11.0/3.0, 0.0, 1.0, 0, -2.0/3.0, 0.0, -1.0, 0, -2.0/3.0, -1.0, 0.0, 0, -2.0/3.0, 1.0, 0.0, 0, -2.0/3.0 }; float4 ps_main(float2 texCoord: TEXCOORD0) : COLOR { float4 col = float4(0,0,0,0); // Sample and output the averaged colors for(int i=0;i<5;i++) col += samples[i].w*tex2D(Texture0,texCoord+ float2(samples[i].x*viewport_inv_width, samples[i].y*viewport_inv_height)); return col; }
- shader学习笔记——Looking Through A Filter 通过滤波器观察
- Shader 学习笔记 ---Looking Through a Filter 过滤器
- Shaders for Game Programmers and Artists(2) - Looking Through a Filter
- 图像处理滤波器(一)——均值滤波器(Mean Filter)
- 图像处理滤波器(二)——中值滤波器(Median Filter)
- 图像处理滤波器(六)——频率滤波器(Frequency Filter)
- Shader学习笔记,通过GLSL实现
- Shader编程学习笔记(五)—— 固定管线shader (Fixed function shader)_1
- Shader编程学习笔记(六)—— 固定管线shader (Fixed function shader)_2
- Opencv学习笔记——自建滤波器cvFilter2D
- OpenCV学习笔记——图像处理之滤波器ImgProc
- 【unity3d学习笔记】Shader-通过Shader实现2D游戏中无限滚动的背景Shader
- 自适应滤波器学习笔记
- 图像处理滤波器(三)——高斯平滑滤波器(Gaussian Smoothing Filter)
- 图像处理滤波器(四)——最值滤波器(Conservative Smoothing Filter)
- 图像处理滤波器(五)——斑点滤波器(Speckle Removal Filter)
- Filter/滤波器
- web安全测试之基本观察学习笔记——使用WebScarab观察实时的POST数据
- 手机实名制在国外是如何推广的
- 我
- 学习配置samba服务
- 酒桌上的那些礼仪规矩~
- 8-30至9-4
- shader学习笔记——Looking Through A Filter 通过滤波器观察
- 创建新节点,删除节点,替换节点,特定节点前插入节点,特定节点后插入节点
- 进程控制
- ADF中Tab页切换事件
- bitset用法 —— 修改一个字节的任意bit位
- libssh c++
- 高端人才必看,生意人必读
- shader学习笔记——运动模糊(motion blur)
- jsp页面中的session、cookies、隐藏域、url重写四种会话及跟踪方式