【shader】模糊与区域清晰效果

【shader】模糊与区域清晰效果

背景------ 群里大佬发个有意思的视频 刚好有思路写了demo实现一下
shader基础入门的话可以看看这两位大佬的文章

Three.js 进阶之旅:Shader着色器基础图案-旷野之息神庙铁球 🔮 - 掘金 (juejin.cn)

手把手带你入门 Three.js Shader 系列(一) - 掘金 (juejin.cn)

使用的vscode的glsl-canvas插件展示

实现思路拆分视频中功能

背景 + y轴偏移 +鼠标区域不偏移

接下来就是一步步实现

背景的实现示例(用图片就好,有需要也拿过去吧)

ini 复制代码
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform float u_time;

float random(vec2 st){
  return fract(sin(dot(st.xy,
  vec2(12.9898,78.233)))
  *43758.5453123);
}

vec3 palette1(float t,vec3 a,vec3 b,vec3 c,vec3 d){
  return a+b*cos(6.28318*(c*t+d)); 
}
vec3 palette(float t){
  vec3 a=vec3(.5,.5,.5);
  vec3 b=vec3(.5,.5,.5);
  vec3 c=vec3(1.,1.,1.);
  vec3 d=vec3(.263,.416,.557);
  return a+b*cos(6.28318*(c*t+d));
}
void main(){
  vec2 uv=gl_FragCoord.xy/u_resolution.xy;

  uv=uv*2.-1.;
  float d=length(uv*2.);
  vec3 col=palette(d*.5);
  d=sin(d*8.)/8.;
  d=abs(d);
  d=.02/d;
  col*=d;
  gl_FragColor=vec4(col,1.);
}

y轴偏移示例

偏移前

偏移后

ini 复制代码
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main(){
    vec2 st=gl_FragCoord.xy/u_resolution.xy;
    st.y = st.y+sin(st.x*100.+u_time)/50.;// 控制偏移
    float strength = mod((st.y) * 10.0, 1.0);
    gl_FragColor = vec4(vec3(strength), 1.0);
}

鼠标缓冲区的实现

ini 复制代码
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main(){
    vec2 st=gl_FragCoord.xy/u_resolution.xy;
    vec2 uv = (st - vec2(0.5));

    vec2 mouse = vec2(u_mouse.x / u_resolution.x,u_mouse.y / u_resolution.y);
    mouse = mouse -vec2(0.5);
    float l = distance(uv, mouse);
    vec3 color = vec3(1.);
    float strength = step(0.2,l);
    if(strength > 0.){
        color = vec3(strength); 
    }else{  
        color = vec3(1.,0.,0);
        
    }
    gl_FragColor = vec4(color, 1.0);
}

加点噪声

arduino 复制代码
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform float u_time;


float random (vec2 st) {
      return fract(sin(dot(st.xy,
                           vec2(12.9898,78.233)))
                   * 43758.5453123);
  }
void main(){
    vec2 uv=gl_FragCoord.xy/u_resolution.xy;

    gl_FragColor=vec4(vec3(random(uv)),1.);
}

混合起来

ini 复制代码
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
float random(vec2 st){
    return fract(sin(dot(st.xy,
        vec2(12.9898,78.233)))
      *43758.5453123);
}
    

vec3 palette(float t){
    vec3 a=vec3(.5,.5,.5);
    vec3 b=vec3(.5,.5,.5);
    vec3 c=vec3(1.,1.,1.);
    vec3 d=vec3(.263,.416,.557);
    return a+b*cos(6.28318*(c*t+d));
}
void main(){
    vec2 uv=gl_FragCoord.xy/u_resolution.xy;

    vec2 st = (uv - vec2(0.5));
    vec2 mouse = vec2(u_mouse.x / u_resolution.x,u_mouse.y / u_resolution.y);
    mouse = mouse -vec2(0.5);
    float l = distance(st, mouse);
    float lstep = step(0.2,l);
    vec3 col = vec3(0.);
    float mixdata = 0.;
    if(lstep > 0.){
        uv.y = uv.y+sin(uv.x*200.+u_time*10.)/50.;
        mixdata =0.1;
    }else{  
        mixdata =0.0;
    }
    uv=uv*2.-1.;
        float d=length(uv*2.);
        col=palette(d*.5);
        d=sin(d*8.)/8.;
        d=abs(d);
        d=.02/d;
        col*=d;
    vec4 texture1 = vec4(col,1.);
    vec4 texture2 = vec4(vec3(random(uv))/2.,1.);
    vec4 texture3 = mix(texture1,texture2,mixdata);
    gl_FragColor=texture3;
}

文章写的不多 欢迎大家多提点建议

相关推荐
贵州数擎科技有限公司17 小时前
使用 Three.js 实现流光特效
前端·webgl
GAMESLI-GIS20 小时前
【WebGL】fbo双pass案例
前端·javascript·webgl
图导物联3 天前
基于WebGIS技术的校园地图导航系统架构与核心功能设计
系统架构·智慧校园·gis·webgl·地图导航·电子地图·校园地图导航
匹马夕阳3 天前
(十七)WebGL中 图像处理的初识
图像处理·人工智能·webgl
2201_759646564 天前
three.js 通用 shaderToy着色器使用,切换
开发语言·前端·javascript·ecmascript·webgl·着色器
优雅永不过时·11 天前
原生Three.js 和 Cesium.js 案例 。 智慧城市 数字孪生常用功能列表
前端·javascript·低代码·编辑器·智慧城市·webgl·three.js
travelclover12 天前
在ArcGIS JS API中使用WebGL实现波纹扩散特效
javascript·arcgis·webgl
iloveas201421 天前
three.js+WebGL踩坑经验合集(6.2):负缩放,负定矩阵和行列式的关系(3D版本)
3d·矩阵·webgl
iloveas201423 天前
three.js+WebGL踩坑经验合集(6.1):负缩放,负定矩阵和行列式的关系(2D版本)
线性代数·矩阵·webgl
iloveas20141 个月前
three.js+WebGL踩坑经验合集(4.2):为什么不在可视范围内的3D点投影到2D的结果这么不可靠
3d·webgl