three+shader 使用自定着色器检测边缘,并且描边,使用自定着色器定义色块边缘。

准备

检测色块之间的颜色差,识别边缘。
素材
原始图:能分辨出色块的图

素材 =>检测添加边缘后在这里插入图片描述
开始
检测色块边缘的shader
资源地址:添加链接描述

// 顶
const vertexShader = `
        varying vec2 vUv;
        void main () {
            vUv = uv;
            vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
            gl_Position = projectionMatrix * mvPosition;
        }
        `;
// 片元
const fragmentShader = `
        uniform sampler2D u_texture;
        uniform float u_width;
        uniform float u_height;
        uniform float u_isBorder;
        uniform float u_opacity;
        uniform float u_border;
        uniform vec3 u_color;
        varying vec2 vUv;
        bool isBorder(vec4 wColor,vec4 hColor){
            return abs(wColor.r-hColor.r)<0.00001;
        }
        void main() {
            vec4 vTexture = texture2D(u_texture, vUv);
            gl_FragColor = vTexture;
            // 边缘检测 设置边框颜色
            if (u_isBorder == 1.0) {
                vec4 nerWDisColor = texture2D(u_texture, vec2(vUv.x + u_border / u_width, vUv.y));
                vec4 nerWDisColorDown = texture2D(u_texture, vec2(vUv.x - u_border / u_width, vUv.y));
                vec4 nerHDisColor = texture2D(u_texture, vec2(vUv.x, vUv.y + u_border / u_height));
                vec4 nerHDisColorDown = texture2D(u_texture, vec2(vUv.x, vUv.y - u_border / u_height));
                if (!isBorder(nerWDisColor, nerWDisColorDown) || !isBorder(nerHDisColor, nerHDisColorDown)) {
                    gl_FragColor = vec4(u_color, u_opacity);
                }
            }
        }
        `;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸡饶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值