边缘检测的原理是利用一些边缘检测算子对图像进行卷积操作。

转载请注明出处:http://www.cnblogs.com/jietian331/p/7232707.html

例如:

代码如下:

 using UnityEngine;

 public class EdgeDetectRenderer : PostEffectRenderer
{
[SerializeField]
Color m_edgeColor; protected override void OnRenderImage(RenderTexture src, RenderTexture dest)
{
base.Mat.SetColor("_EdgeColor", m_edgeColor);
base.OnRenderImage(src, dest);
} protected override string ShaderName
{
get { return "Custom/Edge Detect"; }
}
}

EdgeDetectRenderer

shader如下:

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

 Shader "Custom/Edge Detect"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_EdgeColor("Edge Color", Color) = (,,,)
} SubShader
{
Pass
{
Cull Off
ZWrite Off
ZTest Always CGPROGRAM
#pragma vertex vert
#pragma fragment frag sampler2D _MainTex;
float4 _MainTex_TexelSize;
uniform fixed4 _EdgeColor; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float4 pos : SV_POSITION;
half2 uv[] : TEXCOORD0;
}; v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(-, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(-, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, );
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(-, -);
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, -);
o.uv[] = v.uv + _MainTex_TexelSize.xy * half2(, -);
return o;
} float luminance(float3 color)
{
return dot(fixed3(0.2125, 0.7154, 0.0721), color);
} half sober(half2 uvs[])
{
half gx[] = { -, , ,
-, , ,
-, , };
half gy[] = { -, -, -,
, , ,
, , }; float edgeX = ;
float edgeY = ; for(int i = ; i < ; i++)
{
fixed3 c = tex2D(_MainTex, uvs[i]).rgb;
float l = luminance(c);
edgeX += l * gx[i];
edgeY += l * gy[i];
} return abs(edgeX) + abs(edgeY);
} fixed4 frag(v2f i) : SV_TARGET
{
half edge = sober(i.uv);
fixed4 tex = tex2D(_MainTex, i.uv[]);
return lerp(tex, _EdgeColor, edge);
} ENDCG
}
} Fallback Off
}

效果如下:

 

最新文章

  1. Linux 使用Crontab设置定时调用Shell文件
  2. eclipse为方法添加注释的快捷键是什么
  3. 简易的GCC图形界面GCCUI
  4. window.opener用法
  5. Part 56 Generics in C#
  6. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-003-示例项目用到的类及配置文件
  7. linux杂谈(十七):iscsi存储分离技术
  8. PHP登录程序
  9. [UOJ207]共价大爷游长沙
  10. Windows下SVN命令行工具使用详解
  11. 基础知识:IDE集成开发环境(pycharm)、基本数据类型、用户的交互、运算符
  12. 补充:pyhton 2 和3中的beyts类型
  13. sqlserver2017 重装过程中出现“无法找到数据库引擎启动句柄”错误的解决办法
  14. PHP localhost和127.0.0.1 的区别
  15. Note of The Linux Command Line
  16. python AjaxSpider 代码演示
  17. 【html5】HTML5中canvas怎样画虚线
  18. Unity3d 屏幕截图。并保存。iOS
  19. 十大要避免的Ext JS开发方法
  20. EventBus 3.0源码解析

热门文章

  1. OCR技术(光学字符识别)
  2. 【编译原理】c++实现自上而下语法分析器
  3. 介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
  4. LeetCode 575 Distribute Candies 解题报告
  5. es6 学习四 数组的学习
  6. 第二章 构建布局良好的windows程序
  7. Laravel展示产品-CRUD之show
  8. dedecms批量修改文章为待审核稿件怎么操作
  9. 20165236 2017-2018-2 《Java程序设计》第八周学习总结
  10. vuex操作