头闻号

佛山市顺德区德龙涂料有限公司

特种涂料|金属漆|地坪|其他建筑涂料|防腐涂料|艺术涂料

首页 > 新闻中心 > 科技常识:逼真的HTML5 3D水波动画 可多视角浏览
科技常识:逼真的HTML5 3D水波动画 可多视角浏览
发布时间:2024-12-24 00:29:21        浏览次数:3        返回列表

今天小编跟大家讲解下有关逼真的HTML5 3D水波动画 可多视角浏览 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关逼真的HTML5 3D水波动画 可多视角浏览 的相关资料,希望小伙伴们看了有所帮助。

这是一款基于html5的3D水波动画特效,它的效果非常逼真,水池中的石头在水中沉浮,泛起了一层层水波。同时我们可以拖拽鼠标从不同的视角来浏览水池,3D效果非常不错。另外,我们可以按“G”键来让水池中的石头上下浮动,按“L”键添加灯光效果,设计相当完美。同时说明一下,这款3D水波动画是基于WebGL渲染技术的,大家可以了解一下WebGL。

html代码

<img id="xneg"src="http://www.aidi.net.cn/article/detial/5719/xneg.jpg"><img id="xpos"src="http://www.aidi.net.cn/article/detial/5719/xpos.jpg"><img id="ypos"src="http://www.aidi.net.cn/article/detial/5719/ypos.jpg"><img id="zneg"src="http://www.aidi.net.cn/article/detial/5719/zneg.jpg"><img id="zpos"src="http://www.aidi.net.cn/article/detial/5719/zpos.jpg">

Javascript代码

function Water() { var vertexShader = '\ varying vec2 coord;\ void main() {\ coord = gl_Vertex.xy * 0.5 + 0.5;\ gl_Position = vec4(gl_Vertex.xyz, 1.0);\ }\ '; this.plane = GL.Mesh.plane(); if (!GL.Texture.canUseFloatingPointTextures()) { throw new Error('This demo requires the OES_texture_float extension'); } var filter = GL.Texture.canUseFloatingPointLinearFiltering() ? gl.LINEAR : gl.NEAREST; this.textureA = new GL.Texture(256, 256, { type: gl.FLOAT, filter: filter }); this.textureB = new GL.Texture(256, 256, { type: gl.FLOAT, filter: filter }); this.dropShader = new GL.Shader(vertexShader, '\ const float PI = 3.141592653589793;\ uniform sampler2D texture;\ uniform vec2 center;\ uniform float radius;\ uniform float strength;\ varying vec2 coord;\ void main() {\ \ vec4 info = texture2D(texture, coord);\ \ \ float drop = max(0.0, 1.0 - length(center * 0.5 + 0.5 - coord) / radius);\ drop = 0.5 - cos(drop * PI) * 0.5;\ info.r += drop * strength;\ \ gl_FragColor = info;\ }\ '); this.updateShader = new GL.Shader(vertexShader, '\ uniform sampler2D texture;\ uniform vec2 delta;\ varying vec2 coord;\ void main() {\ \ vec4 info = texture2D(texture, coord);\ \ \ vec2 dx = vec2(delta.x, 0.0);\ vec2 dy = vec2(0.0, delta.y);\ float average = (\ texture2D(texture, coord - dx).r +\ texture2D(texture, coord - dy).r +\ texture2D(texture, coord + dx).r +\ texture2D(texture, coord + dy).r\ ) * 0.25;\ \ \ info.g += (average - info.r) * 2.0;\ \ \ info.g *= 0.995;\ \ \ info.r += info.g;\ \ gl_FragColor = info;\ }\ '); this.normalShader = new GL.Shader(vertexShader, '\ uniform sampler2D texture;\ uniform vec2 delta;\ varying vec2 coord;\ void main() {\ \ vec4 info = texture2D(texture, coord);\ \ \ vec3 dx = vec3(delta.x, texture2D(texture, vec2(coord.x + delta.x, coord.y)).r - info.r, 0.0);\ vec3 dy = vec3(0.0, texture2D(texture, vec2(coord.x, coord.y + delta.y)).r - info.r, delta.y);\ info.ba = normalize(cross(dy, dx)).xz;\ \ gl_FragColor = info;\ }\ '); this.sphereShader = new GL.Shader(vertexShader, '\ uniform sampler2D texture;\ uniform vec3 oldCenter;\ uniform vec3 newCenter;\ uniform float radius;\ varying vec2 coord;\ \ float volumeInSphere(vec3 center) {\ vec3 toCenter = vec3(coord.x * 2.0 - 1.0, 0.0, coord.y * 2.0 - 1.0) - center;\ float t = length(toCenter) / radius;\ float dy = exp(-pow(t * 1.5, 6.0));\ float ymin = min(0.0, center.y - dy);\ float ymax = min(max(0.0, center.y + dy), ymin + 2.0 * dy);\ return (ymax - ymin) * 0.1;\ }\ \ void main() {\ \ vec4 info = texture2D(texture, coord);\ \ \ info.r += volumeInSphere(oldCenter);\ \ \ info.r -= volumeInSphere(newCenter);\ \ gl_FragColor = info;\ }\ ');}Water.prototype.addDrop = function(x, y, radius, strength) { var this_ = this; this.textureB.drawTo(function() { this_.textureA.bind(); this_.dropShader.uniforms({ center: [x, y], radius: radius, strength: strength }).draw(this_.plane); }); this.textureB.swapWith(this.textureA);};Water.prototype.moveSphere = function(oldCenter, newCenter, radius) { var this_ = this; this.textureB.drawTo(function() { this_.textureA.bind(); this_.sphereShader.uniforms({ oldCenter: oldCenter, newCenter: newCenter, radius: radius }).draw(this_.plane); }); this.textureB.swapWith(this.textureA);};Water.prototype.stepSimulation = function() { var this_ = this; this.textureB.drawTo(function() { this_.textureA.bind(); this_.updateShader.uniforms({ delta: [1 / this_.textureA.width, 1 / this_.textureA.height] }).draw(this_.plane); }); this.textureB.swapWith(this.textureA);};Water.prototype.updateNormals = function() { var this_ = this; this.textureB.drawTo(function() { this_.textureA.bind(); this_.normalShader.uniforms({ delta: [1 / this_.textureA.width, 1 / this_.textureA.height] }).draw(this_.plane); }); this.textureB.swapWith(this.textureA);};

来源:爱蒂网