three实现三维迷幻水母游动动画效果代码
代码语言:html
所属分类:三维
代码描述:three实现三维可旋转的迷幻水母游动动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> html, body { margin: 0; overflow: hidden; } #canvas { display: block; width: 100vw; height: 100vh; } </style> </head> <body> <canvas id="canvas" ></canvas> <script type="x-shader" id="shader_vertex"> #define PI 3.1415926535897932384626433832795 float TAU = PI * 2.0; uniform float u_time; uniform float u_width; uniform float u_bump_frequency; uniform float u_bump_scale; varying vec3 v_pos; varying vec3 v_orig_pos; varying vec3 v_view_pos; varying float v_bumps; const float bump_iterations = 7.0; void main() { vec4 model_position = modelMatrix * vec4(position, 1.0); vec3 pos = position; pos.xz *= u_width - (0.05 + smoothstep(0.0, -0.3, position.y) * -0.3) * u_width * 0.3; pos.xz *= 1.0 - sin((pos.y + u_time * 0.2 * TAU) * 2.0) * 0.2; pos.y *= 1.0 - sin((pos.y + (u_time + 0.3) * 0.2 * TAU) * 2.0) * .6; float bumps = 0.0; for (float i = 1.0; i < bump_iterations; i += 1.0) { float bump_scalar = (pos.y * 0.5 - 1.0) * u_bump_frequency * pow(i, 1.4); float bump_noise = snoise4(vec4( pos.x * bump_scalar, pos.y * bump_scalar + u_time * 0.1, pos.z * bump_scalar, u_time * 0.1 * pow(i, 1.0) )); bump_noise = abs(bump_noise); bumps += bump_noise * u_bump_scale / i * sin(((pos.y + u_time * 0.2 * TAU) - PI * .5) * 2.0); } pos *= 1.0 + bumps; pos.xz *= 1.0 - smoothstep(0.2, 0.1, position.y) * 0.35; model_position.xyz = pos; vec4 view_position = viewMatrix * model_position; vec4 projected_position = projectionMatrix * view_position; gl_Position = projected_position; v_pos = vec3(model_position.xyz); v_view_pos = vec3(view_position.xyz); v_bumps = bumps; v_orig_pos = position; } </script> <script type="x-shader" id="shader_fragment"> uniform float u_time; varying vec3 v_pos; varying vec3 v_view_pos; varying vec3 v_orig_pos; varying float v_bumps; void main() { vec3 color_dark = vec3(0, 0, 0); vec3 color_light = vec3(240, 230, 243); vec3 color = mix(color_dark, color_light, v_bumps * 0.002); color = mix(color, color_light, (1.5 + v_pos.y * 0.5) * 0.0008); float noise = snoise4(vec4( v_pos.x + v_orig_pos.y * 32.0, v_orig_pos.y * v_pos.y * 0.1, v_pos.z + v_orig_pos.y * 32.0, u_time )); noise = noise * 0.25 * smoothstep(0.1, 0.2, v_orig_pos.y); color -= noise; color.r *= 1.0 + distance(v_pos.xyz, vec3(0.0,v_orig_pos.y,0.0)) * 0.5; color.b += noise * v_pos.y; color.r *= 1.3; color.g *= v_pos.y + 0.5; color.b *= v_pos.y + 0.5; color.b += (1.0 - color.r) * 0.4; color *= .9; gl_FragColor = vec4(color, 1.0); } </script> <script type="x-shader" id="shader_simplexNoise4D"> // // Description : Array and textureless GLSL 2D/3D/4D simplex // noise functions. // Author : Ian McEwan, Ashima Arts. // Maintainer : ijm // Lastmod : 20110822 (ijm) // License : Copyright (C) 2011 Ashima Arts. All rights reserved. // Distributed under the MIT License. See LICENSE file. // https://github.com/ashima/webgl-noise // vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } float mod289(float x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); } float permute(float x) { return mod289(((x*34.0)+1.0)*x); } vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; } float taylorInvSqrt(float r) { return 1.79284291400159 - 0.85373472095314 * r; } vec4 grad4(float j, vec4 ip) { const vec4 ones = vec4(1.0, 1.0, 1.0, -1.0); vec4 p,s; p.xyz = floor( fract (vec3(j) * ip.xyz) * 7.0) * ip.z - 1.0; p.w = 1.5 - dot(abs(p.xyz), ones.xyz); s = vec4(lessThan(p, vec4(0.0))); p.xyz = p.xyz + (s.xyz*2.0 - 1.0) * s.www; return p; } // (sqrt(5) - 1)/4 = F4, used once below #define F4 0.309016994374947451 float snoise4(vec4 v) { const vec4 C = vec4( 0.138196601125011, // (5 - sqrt(5)).........完整代码请登录后点击上方下载按钮下载查看
网友评论0