webgl实现图片玻璃破碎效果代码
代码语言:html
所属分类:其他
代码描述: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() { vec2 uv = vUv; uv.y = 1. - uv.y; uv.x *= u_ratio; vec2 pointer = u_pointer_position; vec2 pointer_direction = normalize(u_pointer_position - vec2(vUv.x, 1. - vUv.y)); pointer.x *= u_ratio; pointer = pointer - uv; float pointer_angle = atan(pointer.y, pointer.x); float pointer_distance = length(pointer); float pointer_distance_normalized = (1. - clamp(pointer_distance, 0., 1.)); vec3 color = vec3(0.); vec2 img_uv = get_img_uv(); float sector_constant = 0.; float sector_start_angle = 0.; float is_sector_edge = 0.; float is_grid_edge = 0.; float is_central_edge = 0.; float angle_noise = .3 * noise(3. * img_uv); for (int i = 0; i < 12; i++) { float sector_seed = float(i) + u_click_randomizer + 2.; float angle_normalised = mod((pointer_angle - sector_start_angle) / TWO_PI, 1.); angle_normalised += .1 * angle_noise; float angle = angle_normalised * TWO_PI; float sector_size = (.01 + 2. * random2(vec2(float(i) + u_click_randomizer, u_pointer_position.x))); sector_size = min(sector_size, TWO_PI - sector_start_angle); float thickness = u_edge_thickness * (.2 + random(3. * sector_seed)); thickness += angle_noise * .03 * pow(pointer_distance_normalized, 80.); float shape = get_sector_shape(pointer_distance, angle, sector_size, thickness); is_sector_edge = max(is_sector_edge, smoothstep(.6, 1., shape)); sector_constant = mix(sector_constant, random(sector_seed), smoothstep(.2, .8, shape)); vec2 grid_uv = 2. * (.8 + .5 * pointer_distance_normalized) * img_uv; float grid_noise = noise(grid_uv + sector_seed); float grid_thickness = (.4 + .4 * random(10. * sector_seed)) * u_edge_thickness; float grid_shape = shape * smoothstep(.27, .27 + grid_thickness, grid_noise); is_grid_edge += (smoothstep(.1, .5, grid_shape) * smoothstep(.9, .6, grid_shape)); sector_constant = mix(sector_constant, random(sector_seed + 100.), smoothstep(.2, .8, grid_shape)); vec2 central_grid_uv = img_uv * (3. + 3. * pow(pointer_distance_normalized, 10.)); float central_grid_noise = noise(central_grid_uv + sector_seed); float central_grid_thickness = (1. + .5 * random(-2. + sector_seed)) * u_edge_thickness; float central_grid_shape = step(.7, shape) * smoothstep(.27, .27 + central_grid_thickness, central_.........完整代码请登录后点击上方下载按钮下载查看
网友评论0