编辑
2023-11-21
学习
00
请注意,本文编写于 335 天前,最后修改于 335 天前,其中某些信息可能已经过时。

目录

功能: 修改指定像素颜色,实现如边框、头发的变色
演示:
拓展

功能: 修改指定像素颜色,实现如边框、头发的变色

用到的sprit2d图片为

texture.png

演示:

pixels.gif

js
shader_type canvas_item; // uniform 变量,表示边框颜色 uniform vec4 border_color : source_color; uniform float pxielsize = 2.0; uniform vec2 textureSize = vec2(24.0, 24.0); bool isThisPos(vec2 uv, vec2 targetPos) { if((uv.y * textureSize.y>= targetPos.y) && (uv.y * textureSize.y < (targetPos.y+1.0))) { if((uv.x * textureSize.x >= targetPos.x) && (uv.x * textureSize.x < (targetPos.x+1.0))) { return true; } } return false; } void fragment() { // 获取当前像素的UV坐标 float a = texture(TEXTURE, UV).a; if(a > 0.01) { for(int y = 0; y < int(textureSize.y); y++) { if(isThisPos(UV, vec2(0.0, float(y)))) { COLOR = border_color; } if(isThisPos(UV, vec2(1.0, float(y)))) { COLOR = border_color; } if(isThisPos(UV, vec2(22.0, float(y)))) { COLOR = border_color; } if(isThisPos(UV, vec2(23.0, float(y)))) { COLOR = border_color; } } for(int x = 0; x < int(textureSize.x); x++) { if(isThisPos(UV, vec2(float(x), 0.0))) { COLOR = border_color; } if(isThisPos(UV, vec2(float(x), 1.0))) { COLOR = border_color; } if(isThisPos(UV, vec2(float(x), 23.0))) { COLOR = border_color; } } if(isThisPos(UV, vec2(2.0, 2.0))) { COLOR = border_color; } if(isThisPos(UV, vec2(2.0, 22.0))) { COLOR = border_color; } if(isThisPos(UV, vec2(21.0, 2.0))) { COLOR = border_color; } if(isThisPos(UV, vec2(21.0, 22.0))) { COLOR = border_color; } } }

拓展

用此方法筛选点, 此时筛出来的点就是单独的图层,对此图层可做变换操作

pixels.gif

js
shader_type canvas_item; // uniform 变量,表示边框颜色 uniform vec4 border_color : source_color; uniform float pxielsize = 2.0; uniform vec2 textureSize = vec2(24.0, 24.0); // 波浪的频率、振幅和速度 uniform float wave_frequency : hint_range(0.1, 10.0) = 1.0; uniform float wave_amplitude : hint_range(0.1, 5.0) = 0.5; uniform float wave_speed : hint_range(0.1, 5.0) = 1.0; bool isThisPos(vec2 uv, vec2 targetPos) { if((uv.y * textureSize.y>= targetPos.y) && (uv.y * textureSize.y < (targetPos.y+1.0))) { if((uv.x * textureSize.x >= targetPos.x) && (uv.x * textureSize.x < (targetPos.x+1.0))) { return true; } } return false; } void fragment() { // 获取当前像素的UV坐标 // vec4 border_color = vec4(1.0*sin(PI*TIME),1.0*cos(PI*TIME),1.0*sin(PI*TIME)*cos(PI*TIME),1.0); bool isThisUV = false; float a = texture(TEXTURE, UV).a; if(a > 0.01) { for(int y = 0; y < int(textureSize.y); y++) { if(isThisPos(UV, vec2(0.0, float(y)))) { isThisUV = true; } if(isThisPos(UV, vec2(1.0, float(y)))) { isThisUV = true; } if(isThisPos(UV, vec2(22.0, float(y)))) { isThisUV = true; } if(isThisPos(UV, vec2(23.0, float(y)))) { isThisUV = true; } } for(int x = 0; x < int(textureSize.x); x++) { if(isThisPos(UV, vec2(float(x), 0.0))) { isThisUV = true; } if(isThisPos(UV, vec2(float(x), 1.0))) { isThisUV = true; } if(isThisPos(UV, vec2(float(x), 23.0))) { isThisUV = true; } } if(isThisPos(UV, vec2(2.0, 2.0))) { isThisUV = true; } if(isThisPos(UV, vec2(2.0, 22.0))) { isThisUV = true; } if(isThisPos(UV, vec2(21.0, 2.0))) { isThisUV = true; } if(isThisPos(UV, vec2(21.0, 22.0))) { isThisUV = true; } } if(isThisUV) { vec2 uv = UV; // 计算波浪效果 float wave = sin(uv.x * wave_frequency + TIME * wave_speed) * wave_amplitude; float wave2 = sin(uv.y * wave_frequency + TIME * wave_speed) * wave_amplitude; // 应用波浪效果到y坐标 uv.y += wave; uv.x += wave2; // 获取纹理颜色 vec4 current_color = texture(TEXTURE, uv); current_color.rgb = border_color.rgb; // 设置最终颜色 COLOR = current_color; } }

本文作者:beiklive

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!