经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
基于Unity实现2D边缘检测
来源:jb51  时间:2022/4/11 18:16:20  对本文有异议

一、ShaderLab

1.Alpha值边缘检测

根据图片的Alpha值边缘判定,向内扩一段距离做边缘,颜色设置未描边颜色;

片元着色阶段,向上下左右四个方向做检测,有一个点的透明度为0,判定为边缘;

  1. Shader "2DOutline"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _LineWidth("Width",Range(0,0.4)) = 1.0
  7. _LineColor("LineColor",color) = (1,1,1,1)
  8. _Intensity("Intensity",Range(1,10)) = 1.0
  9. }
  10.  
  11. SubShader
  12. {
  13. Tags { "RenderType" = "Opaque" "Queue" = "Transparent"}
  14. Blend SrcAlpha OneMinusSrcAlpha
  15. Pass
  16. {
  17. CGPROGRAM
  18. #pragma vertex vert
  19. #pragma fragment frag
  20.  
  21. #include "UnityCG.cginc"
  22.  
  23. struct appdata
  24. {
  25. float4 vertex : POSITION;
  26. float2 uv : TEXCOORD0;
  27. };
  28.  
  29. struct v2f
  30. {
  31. float2 uv : TEXCOORD0;
  32. float4 vertex : SV_POSITION;
  33. };
  34.  
  35. sampler2D _MainTex;
  36. float4 _MainTex_ST;
  37. fixed _LineWidth;
  38. float4 _LineColor;
  39. fixed _Intensity;
  40.  
  41. v2f vert(appdata v)
  42. {
  43. v2f o;
  44. o.vertex = UnityObjectToClipPos(v.vertex);
  45. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  46. return o;
  47. }
  48.  
  49. fixed4 frag(v2f i) : SV_Target
  50. {
  51. fixed4 col = tex2D(_MainTex, i.uv);
  52. // 采样周围4个点
  53. float2 up_uv = i.uv + float2(0, 1) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
  54. float2 down_uv = i.uv + float2(0,-1) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
  55. float2 left_uv = i.uv + float2(-1,0) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
  56. float2 right_uv = i.uv + float2(1,0) * _LineWidth * 1 / 10 * _MainTex_ST.xy;
  57. // 如果有一个点透明度为0 说明是边缘
  58. float w = tex2D(_MainTex,up_uv).a * tex2D(_MainTex,down_uv).a * tex2D(_MainTex,left_uv).a * tex2D(_MainTex,right_uv).a;
  59.  
  60. if (w == 0) {
  61. col.rgb = lerp(_LineColor * _Intensity, col.rgb, w);
  62. }
  63.  
  64. return col;
  65. }
  66. ENDCG
  67. }
  68. }
  69. }

如果图片内容恰好铺满整张图,没有alpha值,方法不适用;下图底部边缘消失了;

2.卷积边缘检测

在屏幕后处理阶段,使用卷积做边缘检测;

卷积:根据像素周围八个方向的像素的计算出新的像素值;

边缘检测卷积算子,都包含水平和竖直两个方向的卷积核;

梯度公式:G = sqrt(Gx*Gx + Gy*Gy);

考虑性能问题,使用:G = |Gx|+|Gy|;

顶点着色器计算卷积纹理采样坐标,减少计算量(片元数量更多);

片元着色阶段Sobel卷积计算,插值获得片元像素颜色;

Sobel计算结果和梯度Gradient比较,大于梯度和EdgeColor做插值;

屏幕后效调用OnRenderImage接口;

  1. Shader "EdgeDetection"
  2. {
  3. Properties{
  4. _MainTex("Base (RGB)", 2D) = "white" {}
  5. _EdgeColor("Edge Color", Color) = (0, 0, 0, 1)
  6. //卷积梯度
  7. _Gradient("Gradient",float) =0.0
  8. }
  9. SubShader{
  10. Pass
  11. {
  12. ZTest Always Cull Off ZWrite Off
  13.  
  14. CGPROGRAM
  15.  
  16. #include "UnityCG.cginc"
  17.  
  18. #pragma vertex vert
  19. #pragma fragment frag
  20.  
  21. sampler2D _MainTex;
  22. uniform half4 _MainTex_TexelSize;
  23. //fixed _EdgeOnly;
  24. fixed4 _EdgeColor;
  25. //fixed4 _BackgroundColor;
  26. fixed _Gradient;
  27.  
  28. struct v2f {
  29. float4 pos : SV_POSITION;
  30. half2 uv[9] : TEXCOORD0;
  31. };
  32.  
  33. v2f vert(appdata_img v) {
  34. v2f o;
  35. o.pos = UnityObjectToClipPos(v.vertex);
  36.  
  37. half2 uv = v.texcoord;
  38.  
  39. o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1);
  40. o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -1);
  41. o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1);
  42. o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 0);
  43. o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0);
  44. o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1, 0);
  45. o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1);
  46. o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1);
  47. o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1);
  48.  
  49. return o;
  50. }
  51.  
  52. fixed luminance(fixed4 color) {
  53. return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
  54. }
  55.  
  56. half Sobel(v2f i) {
  57. const half Gx[9] = { -1, 0, 1,
  58. -2, 0, 2,
  59. -1, 0, 1};
  60. const half Gy[9] = { -1, -2, -1,
  61. 0, 0, 0,
  62. 1, 2, 1};
  63.  
  64. half texColor;
  65. half edgeX = 0;
  66. half edgeY = 0;
  67. for (int it = 0; it < 9; it++) {
  68. texColor = luminance(tex2D(_MainTex, i.uv[it]));
  69. edgeX += texColor * Gx[it];
  70. edgeY += texColor * Gy[it];
  71. }
  72.  
  73. half edge = 1 - abs(edgeX) - abs(edgeY);
  74.  
  75. return edge;
  76. }
  77.  
  78. fixed4 frag(v2f i) : SV_Target {
  79. half edge = Sobel(i);
  80.  
  81. fixed4 col = tex2D(_MainTex, i.uv[4]);
  82.  
  83. if(edge> _Gradient)
  84. col = lerp(_EdgeColor, tex2D(_MainTex, i.uv[4]), edge);
  85. return col;
  86. }
  87.  
  88. ENDCG
  89. }
  90. }
  91. FallBack Off
  92. }

二、ShaderGraph

抓取图片缓冲,上下左右四个方位平移,乘以描边颜色;

四张图合并,减去原图范围的像素,只剩边缘;

最后将原图和边缘合并(可插值使边缘柔和);

升级项目到URP,修改projectsetting-graphic-pielinesettings;

导入ShaderGraph包,开始拖拖拽拽,真的香,效果好,速度快,思路清晰;

到此这篇关于基于Unity实现2D边缘检测的文章就介绍到这了,更多相关Unity边缘检测内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号