Shader-边缘检测

这篇博客介绍了如何在Unity中实现Sobel Edge Detection算法,通过Shader检测图像边缘。内容包括Shader的编写,涉及到纹理采样、像素操作等技术。


Shader "CX/Edge Detection" /* The edge detection algorithm that is implemented in this shader is named "Sobel Edge Detection" */
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "Queue"="Transparent" }
        Cull Back

        GrabPass { }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };    

            struct v2f
            {
                float4 position : POSITION;
                float4 screenPos : TEXCOORD0;
            };

            fixed4 _Color;
            sampler2D _GrabTexture : register(s0);

            v2f vert(appdata input)
            {
                v2f output;

                output.position = UnityObjectToClipPos(input.vertex);
                output.screenPos = output.position;

                return output;
            }

            half4 pixel;
            half2 uv;
            fixed onePixelW, onePixelH;
            half4 frag(v2f input) : SV_Target
            {    
                uv = input.screenPos.xy / input.screenPos.w;
                uv.x = (uv.x + 1) * .5;
                uv.y = 1.0 - (uv.y + 1) * .5;

                onePixelW = 1.0 / _ScreenParams.x;
                onePixelH = 1.0 / _ScreenParams.y;

                pixel = 0;
                pixel = abs(
                        tex2D(_GrabTexture, half2(uv.x - onePixelW, uv.y)) - 
                        tex2D(_GrabTexture, half2(uv.x + onePixelW, uv.y)) + 
                        tex2D(_GrabTexture, half2(uv.x, uv.y + onePixelH)) -
                        tex2D(_GrabTexture, half2(uv.x, uv.y - onePixelH))
                    );

                return pixel * _Color;
            }    
            ENDCG
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值