【Shader】屏幕后处理-利用卷积算子计算边缘检测
2018-02-05 16:06
423 查看
Shader "Edge Detection" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _EdgeOnly ("Edge Only", Float) = 1.0 _EdgeColor ("Edge Color", Color) = (0, 0, 0, 1) _BackgroundColor ("Background Color", Color) = (1, 1, 1, 1) } SubShader { Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #include "UnityCG.cginc" #pragma vertex vert #pragma fragment fragSobel sampler2D _MainTex; uniform half4 _MainTex_TexelSize; fixed _EdgeOnly; fixed4 _EdgeColor; fixed4 _BackgroundColor; struct v2f { float4 pos : SV_POSITION; half2 uv[9] : TEXCOORD0; }; v2f vert(appdata_img v) { v2f o; // 顶点投影坐标 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); // uv坐标 half2 uv = v.texcoord; // 纹素值*Sobel算子 o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1); o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -2); o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1); o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-2, 0); o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0); o.uv[5] = uv + _MainTex_TexelSize.xy * half2(2, 0); o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1); o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1); o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1); return o; } // 固定参数,求亮度 fixed luminance(fixed4 color) { return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b; } half Sobel(v2f i) { const half Gy[9] = {-1, 0, 1, -2, 0, 2, -1, 0, 1}; const half Gx[9] = {-1, -2, -1, 0, 0, 0, 1, 2, 1}; half texColor; half edgeX = 0; half edgeY = 0; for (int it = 0; it < 9; it++) { texColor = luminance(tex2D(_MainTex, i.uv[it])); edgeX += texColor * Gx[it]; edgeY += texColor * Gy[it]; } half edge = 1 - abs(edgeX) - abs(edgeY); return edge; } fixed4 fragSobel(v2f i) : SV_Target { // edge值越小,表示越可能是边缘点 half edge = Sobel(i); // i.uv[4]是0,0偏移,等同于UV坐标 // 边缘+彩色 fixed4 withEdgeColor = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge); // 边缘 fixed4 onlyEdgeColor = lerp(_EdgeColor, _BackgroundColor, edge); // 边缘彩色混合 return lerp(withEdgeColor, onlyEdgeColor, _EdgeOnly); } ENDCG } } FallBack Off }
相关文章推荐
- Unity shader学习之屏幕后期处理效果之边缘检测
- opencv 图像处理 形态学操作 腐蚀 膨胀 开闭运算 阈值二值化 图像卷积 图像金字塔 Sobel算子 Laplacian 算子 candy边缘检测 霍夫变换 直方图
- UnityShader屏幕后处理-边缘检测,经典的插值lerp
- 图像处理算法4——Sobel 边缘检测算子
- 图像处理--------应用卷积– 轧花与边缘检测
- OpenCV之imgproc 模块. 图像处理(2)实现自己的线性滤波器 给图像添加边界 Sobel 导数 Laplace 算子 Canny 边缘检测
- 图像处理常用边缘检测算子总结
- 图像处理常用边缘检测算子总结
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- Android Jni OpenCv 利用Cany算子做边缘检测
- 图像处理常用边缘检测算子总结(转)
- 利用Canny边缘检测算子进行边缘检测的原理及OpenCV代码实现
- 数字图像处理——边缘检测算子…
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- 【OpenCV图像处理入门学习教程四】基于LoG算子的图像边缘检测
- 图像处理常用边缘检测算子
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- 图像处理-线性滤波-1 基础(相关算子、卷积算子、边缘效应)
- 图像处理之应用卷积– 轧花与边缘检测