功能描述:
使用uv位置偏移实现像素条形错位的故障艺术效果
效果:
参数:
//错位强度
strength: { value: 0.02, editor: { slide : true, range : [0,0.1], step: 0.01 } }
//错位横条的宽度(密集程度)
density: { value: 0.01, editor: { slide : true, range : [0.01,1], step: 0.01 } }
核心思路:
根据uv按水平方向进行随机偏移。
//随机数函数,可用其他实现等效替换
float random(float coord) {
// range [0, 1)
float scaledValue = coord / density;
float roundedValue = floor(scaledValue);
float approxValue = roundedValue * density;
return fract(sin(approxValue * 12.9898) * 43758.5453);
}
//可调范围的随机数函数,可用其他实现等效替换
float randomRange(float coord, float minVal, float maxVal) {
float rand = random(coord);
return minVal + rand * (maxVal - minVal);
}
vec4 frag () {
vec2 uv_offset = v_uv; // Offset for the UV coordinates
// random horizontal glitch line
uv_offset.x += randomRange(v_uv.y, -strength, strength); // Move the UV coordinates over time
vec4 col = mainColor * texture(mainTexture, uv_offset);
return CCFragOutput(col);
}
扩展:可以与其他故障效果结合搭配使用。