原理:对于所有透明的像素点,遍历该像素点周围的所有像素点,当有任意一个像素点非透明时,就将该像素点置为描边颜色。
PS.在网上读到一位前辈写的方法是“遍历所有不透明的像素点四周,当有透明像素点时,将该像素点设置为描边颜色”(思路相反),这样的做法会有些缺憾,结尾会放出比较图。
local vert = [[
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_PMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
]]
local frag = [[
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform vec2 my_size; // 纹理大小,纹理坐标范围为0-1
void main(void)
{
vec2 unit = 1.0 / my_size.xy; // 单位坐标
float step = 30.0; // 30度
float width = 2.5; // 描边宽度
float a_limit = 0.4; // 透明度限制,低于该值即视为透明
vec4 border = vec4(1.0,1.0,1.0,1.0);// 边框颜色
vec4 color1 = texture2D(CC_Texture0, v_texCoord);
gl_FragColor = color1;
if(color1.a >= a_limit)
{
return; // 非透明像素点不做处理
}
// 遍历四周所有像素点
for(float i = 0.0; i < 360.0; i += step)
{
// 当前角度的偏移坐标
vec2 offset = ve

本文介绍了Cocos2d-lua中使用shader进行描边的原理,通过对比两种不同的遍历方法,探讨了如何提高描边效率。文章讨论了一种时间复杂度更优的策略,即先找到非透明像素点,再判断相邻非透明像素,以优化描边效果。同时,提出了寻找透明像素点周围最近非透明像素点以改善描边质量的方法。
最低0.47元/天 解锁文章
638

被折叠的 条评论
为什么被折叠?



