【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;
}

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

相关推荐
小彭努力中3 天前
20. gui调试3-下拉菜单、单选框
前端·3d·webgl
还是大剑师兰特3 天前
webGL 综合教程100+【目录】
webgl·webgl教程·webgl 示例
xiangshangdemayi6 天前
WebGL系列教程八(GLSL着色器基础语法)
webgl·基础·shader·着色器·语法·glsl
wjs04066 天前
WebGL入门:将3D世界带入网页的魔法
javascript·3d·webgl·前端开发
xiangshangdemayi6 天前
WebGL系列教程六(纹理映射与立方体贴图)
webgl·贴图·uv·立方体·纹理坐标·纹理映射
refineiks9 天前
three.js使用3DTilesRendererJS加载3d tiles数据
前端·3d·图形渲染·webgl
Ian102511 天前
Three.js new THREE.TextureLoader()纹理贴图使用png图片显示为黑色
前端·javascript·webgl·three.js·贴图·三维
常城13 天前
解决TMP_InputField 在WebGL(抖音)上不能唤起虚拟键盘,不能使用手机内置输入法的问题
webgl
DSLMing15 天前
微信小程序webgl 显示图片
微信小程序·小程序·webgl
GISer_Jing15 天前
Cesium加载高速公路样式线图层和利用CSS撰写高速公路样式
前端·css·webgl