webgl实现canvas可交互的可爱泡泡碰撞融合代码

代码语言:html

所属分类:其他

代码描述:webgl实现canvas可交互的可爱泡泡碰撞融合代码

代码标签: webgl canvas 交互 可爱 泡泡 碰撞 融合 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  
  
  
<style>
html, body {
    overflow: hidden;
    margin: 0;
    background-color: #f3f2f1;
}

canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.lil-gui {
    --width: 450px;
    max-width: 90%;
    --widget-height: 20px;
    font-size: 15px;
    --input-font-size: 15px;
    --padding: 10px;
    --spacing: 10px;
    --slider-knob-width: 5px;
    --background-color: rgba(5, 0, 15, .8);
    --widget-color: rgba(255, 255, 255, .3);
    --focus-color: rgba(255, 255, 255, .4);
    --hover-color: rgba(255, 255, 255, .5);

    --font-family: monospace;
}
</style>


  
</head>

<body>
  <canvas></canvas>

<script type="x-shader/x-fragment" id="vertShader">
    precision highp float;

    varying vec2 vUv;
    attribute vec2 a_position;

    void main () {
        vUv = .5 * (a_position + 1.);
        gl_Position = vec4(a_position, 0., 1.);
    }
</script>

<script type="x-shader/x-fragment" id="fragShaderSpeed">
    precision highp float;
    precision highp sampler2D;

    varying vec2 vUv;
    uniform sampler2D u_prev_frame_texture;
    uniform vec2 u_pointer_position;
    uniform float u_pointer_power;
    uniform vec2 u_delta_xy;
    uniform float u_ratio;
    uniform float u_speed_fade;


    #define TWO_PI 6.28318530718
    #define PI 3.14159265358979323846


    void main () {

        vec2 uv = vUv;

        vec2 pointer = u_pointer_position;
        pointer.x *= u_ratio;

        vec2 pointer_uv = uv;
        pointer_uv.x *= u_ratio;

        float pointer_dot = 1. - clamp(length(pointer_uv - pointer), 0., 1.);
        pointer_dot = pow(pointer_dot, 6.);
        pointer_dot *= u_pointer_power;

        vec3 back = texture2D(u_prev_frame_texture, uv).rgb;
        back *= u_speed_fade;
        back = mix(back, vec3(u_delta_xy, 0.), pointer_dot);

        gl_FragColor = vec4(back.xyz, 1.);
    }
</script>

<script type="x-shader/x-fragment" id="fragShaderOutput">
    precision highp float;
    precision highp sampler2D;

    varying vec2 vUv;
    uniform sampler2D u_image_texture;
    uniform sampler2D u_speed_texture;
    uniform float u_ratio;
    uniform float u_img_ratio;
    uniform float u_time;
    uniform float u_pointer_multiplier;

    #define TWO_PI 6.28318530718
    #define PI 3.14159265358979323846

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

    float get_dot_shape (vec2 dist, float radius) {
        return 1. - smoothstep(0., radius, dot(dist, dist) * 4.);
    }

    float get_point_shape (vec2 dist, float p) {
        float v = pow(1. - clamp(0., 1., length(dist)), 1.);
        v = smoothstep(0., 1., v);
        v = pow(v, 2.);
        return v;
    }

    void main () {

        float t = .001 * u_time;

        vec2 offset = texture2D(u_speed_texture, vUv).xy;
        offset.x = - offset.x;
        offset *= u_pointer_multiplier;

        float offset_pow = 9.;
        float offset_mult = 1.4;
        offset.x += offset_mult * pow(vUv.x, offset_pow);
        offset.x -= offset_mult * pow(1. - vUv.x, offset_pow);
        offset.y += offset_mult * pow(vUv.y, offset_pow);
        offset.y -= offset_mult * pow(1. - vUv.y, offset_pow);

        vec2 uv = vUv - .5;
        uv.x *= u_ratio;

        float f1, f2, f3, f4;
        float f1_eyes, f2_eyes, f3_eyes, f4_eyes;
        float f1_pupils, f2_pupils, f3_pupils, f4_pupils;

        {
            vec2 f_uv = uv + .9 * offset;
            vec2 f1_traj = .2 * vec2(.8 * sin(.5 * t), .2 + 2.5 * cos(.3 * t));
            vec2 f1_eye_size = vec2(.015, .001);
            float f1_eye_x = .1;
            float f1_eye_y = .1 + .1 * f1_traj.y;

            float f = get_point_shape(f_uv + f1_traj, 5.);
            f1_eyes = get_dot_shape(f_uv - vec2(f1_eye_x, f1_eye_y) + f1_traj, f1_eye_size[0]);
            f1_eyes += get_dot_shape(f_uv - vec2(-f1_eye_x, f1_eye_y) + f1_traj, f1_eye_size[0]);

            f1_pupils = get_dot_shape(f_uv - vec2(f1_eye_x, f1_eye_y - .02) + f1_traj, f1_eye_size[1]);
            f1_pupils += get_dot_shape(f_uv - vec2(-f1_eye_x + .005, f1_eye_y - .03) + f1_traj, f1_eye_size[1]);

            f1 = f;
        }

        {
            vec2 f_uv = uv + 1.3 * offset;
            vec2 f2_traj = .5 * vec2(1.7 * sin(-.5 * t), sin(.8 * t));
            vec2 f1_eye_size = vec2(.01, .001);
            float f1_eye_x = .1;
            float f1_eye_y = .05 + .3 * f2_traj.y;

            float f = get_point_shape(f_uv + f2_traj, 3.);
            f2_eyes = get_dot_shape((f_uv - vec2(f1_eye_x, f1_eye_y) + f2_traj) * vec2(.9, .8), f1_eye_size[0]);
            f2_eyes += get_dot_shape((f_uv - vec2(-f1_eye_x, f1_eye_y) + f2_traj) * vec2(1.2, 1.), f1_eye_size[0]);

            f2_pupils = get_dot_shape(f_uv - vec2(f1_eye_x + .01 * sin(2. * t), f1_eye_y - .014) + f2_traj, f1_eye_size[1]);
            f2_pupils += get_dot_shape(f_uv - vec2(-f1_eye_x + .01 * sin(2. * t) + .005, f1_eye_y - .011) + f2_traj, f1_eye_size[1]);

            f2 = f;
        }

        {
            vec2 f_uv = uv + 1.2 * offset;
            vec2 f3_traj = .45 * vec2(.5 * cos(-.3 * t), cos(-.8 * t));
            vec2 f1_eye_size = vec2(.005, .001);
            float f1_eye_x = .1;
            float f1_eye_y = .1 + .5 * f3_traj.y;

            float f = get_point_shape(f_uv + f3_traj, 3.);
            f3_eyes += get_dot_shape(f_uv - vec2(f1_eye_x, f1_eye_y) + f3_traj, f1_eye_size[0]);
            f3_eyes += get_dot_shape(f_uv - vec2(-f1_eye_x, f1_eye_y) + f3_traj, f1_eye_size[0]);

            f3_pupils = get_dot_shape(f_uv - vec2(f1_eye_x, f1_eye_y - .014) + f3_traj, f1_eye_size[1]);
            f3_pupils += get_dot_shape(f_uv - vec2(-f1_eye_x + .005, f1_eye_y - .011) + f3_traj, f1_eye_size[1]);

            f3 = f;
        }

        f1 -= f2;
        f3 -= f2;
        f2 -= f1;
        f2 -= f3;
        f1 -= f3;
        f3 -= f1;
        f3 -= f1;


        f1_eyes *= smoothstep(.1, .9, f1);
        f1_pupils *= smoothstep(.1, .9, f1);

        f2_eyes *= smoothstep(.3, .9, f2);
        f2_pupils *= smoothstep(.3, .9, f2);

        f3_eyes *= smoothstep(.3, .9, f3);
        f3_pupils *= smoothstep(.3, .9, f3);

        f1 = step(.3, f1);
        f2 = step(.3, f2);
        f3 = step(.3, f3);
        f1_eyes = step(.2, f1_eyes);
        f2_eyes = step(.2, f2_eyes);
        f3_eyes = step(.2, f3_eyes);
        f1_pupils = step(.2, f1_pupils);
        f2_pupils = step(.2, f2_pupils);
        f3_pupils = step(.2, f3_pupils);

        vec3 color1 = vec3(0.8549, 0.7059, 0.1961);
        vec3 color2 = vec3(0.7569, 0.2863, 0.1765);
        vec3 color3 = vec3(0.2627, 0.4627, 0.6706);

        float opacity = 0.;
        opacity += f1;
        opacity -= f1_eyes;
        opacity += f1_pupils;

        opacity += .9 * f2;
        opacity *= (1. - f2_eyes);
        opacity += f2_pupils;

        opacity += .95 * f3;
        opacity *= (1. - f3_eyes);
        opacity += f3_pupils;

        vec3 color = vec3(0.);
        color = f1 * color1;
        color -= f1_eyes;
        color = mix(color, color2, f2);
        color *= (1. - f2_eyes);
        color = mix(color, color3, f3);
        color = mix(color, vec3(0.), f3_pupils);

        float noise = random(uv + sin(t));
        color.rgb += noise * .1;

        gl_FragColor = vec4(color, opacity);
    }
</script>
  
      <script type="module">
import GUI from "//repo.bfw.wiki/bfwrepo/js/lil-gui.esm.js";

const canvasEl = document.getElementsByTagName("canvas")[0];
const devicePixelRatio = Math.min(window.devicePixelRatio, 2);

const params = {
  pointerMultiplier: .05,
  pointerPower: .35,
  deltaThreshold: .1,
  pointerFadeSpeed: .75,
  speedTextureFadeSpeed: .95.........完整代码请登录后点击上方下载按钮下载查看

网友评论0