webgl实现图片玻璃破碎效果代码

代码语言:html

所属分类:其他

代码描述:webgl实现图片玻璃破碎效果代码,可通过右上角面板调节参数。

代码标签: webgl 图片 玻璃 破碎

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

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

<head>
  <meta charset="UTF-8">
  


  
  
<style>
body, html {
    margin: 0;
    padding: 0;
}

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

.tip {
    position: fixed;
    top: 90%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-family: monospace;
    user-select: none;
    pointer-events: none;
    background-color: cornsilk;
    color: red;
}

.lil-gui {
    --width: 400px;
    --widget-height: 20px;
    font-size: 15px;
    --input-font-size: 15px;
    --padding: 10px;
    --spacing: 10px;
    --slider-knob-width: 5px;
    --background-color: rgba(5, 0, 15, .9);
    --widget-color: rgba(255, 255, 255, .3);
    --focus-color: rgba(255, 255, 255, .4);
    --hover-color: rgba(255, 255, 255, .5);
    --font-family: monospace;
    z-index: 1;
}
</style>


  
  
</head>

<body translate="no">
  <input id="image-selector-input" style="visibility:hidden;" type="file">

<canvas></canvas>

<div class="tip">
    click to break, space to toggle
</div>


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

    varying vec2 vUv;
    attribute vec2 a_position;

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

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

    varying vec2 vUv;
    uniform sampler2D u_image_texture;
    uniform float u_edge_thickness;
    uniform float u_ratio;
    uniform vec2 u_pointer_position;
    uniform float u_img_ratio;
    uniform float u_click_randomizer;
    uniform float u_rotation;
    uniform float u_effect;
    uniform float u_effect_active;

    #define TWO_PI 6.28318530718
    #define PI 3.14159265358979323846


    float random(float x) {
        return fract(sin(x * 12.9898) * 43758.5453);
    }

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

    float noise(vec2 p) {
        vec2 ip = floor(p);
        vec2 u = fract(p);
        u = u*u*(3.0-2.0*u);

        float res = mix(
        mix(random2(ip), random2(ip+vec2(1.0, 0.0)), u.x),
        mix(random2(ip+vec2(0.0, 1.0)), random2(ip+vec2(1.0, 1.0)), u.x), u.y);
        return res*res;
    }

    float get_sector_shape(float d, float a, float angle, float edges) {
        float angle1 = PI;
        float angle2 = angle1 + angle;

        float edge1 = smoothstep(angle1 - edges / d, angle1 + edges / d, a);
        float edge2 = smoothstep(angle2 - edges / d, angle2 + edges / d, a);

        return edge1 * (1. - edge2);
    }


    float get_img_frame_alpha(vec2 uv, float img_frame_width) {
        float img_frame_alpha = smoothstep(0., img_frame_width, uv.x) * smoothstep(1., 1. - img_frame_width, uv.x);
        img_frame_alpha *= smoothstep(0., img_frame_width, uv.y) * smoothstep(1., 1. - img_frame_width, uv.y);
        return img_frame_alpha;
    }


    float get_simple_cracks(float a, float d, float n) {
        a *= (1. + sin(2. * a + PI + 2. * u_click_randomizer));
        float simple_cracks_number = 10.;
        float simple_cracks_angle_step = TWO_PI / simple_cracks_number;
        float simple_crack_angle = mod(a + n + u_click_randomizer, simple_cracks_angle_step);
        float cracks_shape = 4. * abs(simple_crack_angle - .5 * simple_cracks_angle_step);
        cracks_shape = mix(cracks_shape, 1., smoothstep(.9, 1., d));
        cracks_shape *= pow(d + .4 * u_click_randomizer * max(0., cos(2. * a + u_click_randomizer) * sin(1. * a)), 12.);
        cracks_shape = (1. + n) * (1. + sin(4. * a)) * step(.9, cracks_shape);
        return cracks_shape;
    }

    vec2 get_img_uv() {
        vec2 img_uv = vUv;
        img_uv -= .5;
        if (u_ratio > u_img_ratio) {
            img_uv.x = img_uv.x * u_ratio / u_img_ratio;
        } else {
            img_uv.y = img_uv.y * u_img_ratio / u_ratio;
        }
        float scale_factor = 1.4;
        img_uv *= scale_factor;
        img_uv += .5;
        img_uv.y = 1. - img_uv.y;

        return img_uv;
    }

    vec2 get_disturbed_uv(vec2 uv, float section_constant, float edge, vec2 direction, float border) {
        float img_distortion = u_effect * (section_constant - .5);
        vec2 discurbed_uv = uv;
        discurbed_uv += 2. * img_distortion;
        discurbed_uv.x -= mix(.03 * edge * direction.x, -.1 * edge, border);
        discurbed_uv.y -= mix(.03 * edge * direction.y, -.1 * edge, border);
        vec2 center = vec2(0.5, 0.5);
        discurbed_uv = discurbed_uv - center;
        float cosA = cos(4. * img_distortion);
        float sinA = sin(4. * img_distortion);
        float perspective = 1. + img_distortion * discurbed_uv.y;
        discurbed_uv = vec2(
        perspective * (cosA * discurbed_uv.x - sinA * discurbed_uv.y),
        perspective * (sinA * discurbed_uv.x + cosA * discurbed_uv.y)
        );
        discurbed_uv += center;
        return discurbed_uv;
    }


    void main() {
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0