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

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

相关推荐
Highcharts.js10 小时前
Highcharts 渲染方式解析:SVG 默认渲染与 Boost 模块的 WebGL 加速,以及它与其他可视化库的不同
svg·webgl·可视化·canvas·highcharts·渲染方式
UWA4 天前
GPM 2.0 WebGL 监测能力扩容|覆盖 Unity/Cocos/LayaAir 多引擎微信 & 抖音小游戏
unity·微信·性能优化·小游戏·webgl
像我这样帅的人丶你还4 天前
🚀苹果的液态玻璃咋做?🚀
前端·webgl·three.js
用户83134859306985 天前
Cesium实现动态流动火烧云晚霞效果(可用slider调整火烧云浓度)
vue.js·webgl·cesium
IT爱学堂5 天前
Three.js可视化企业实战WEBGL课,2023年全新WEB 3D THREEJS技术
前端·javascript·webgl
智商偏低6 天前
attribute 与 uniform 本质区别(WebGL)
webgl
进哥AI研习社6 天前
WebGL 与视频背景——首屏视觉层的性能博弈
性能优化·webgl·gpu·canvas·shader·帧率·视频背景
IT小白杨13 天前
链上任务与海外社媒多账号:环境隔离工具技术横评
经验分享·webgl·chrome devtools·安全架构·指纹浏览器
AI_AGENT_DEV_AI19 天前
数字孪生项目的开发
webgl
supermapsupport21 天前
SuperMap GIS 基础产品 FAQ 集锦(20260807)
webgl·supermap·idesktopx