多视角3D逼真HTML5水波动画
发布时间:2016-03-03 10:39:57 作者:佚名
我要评论

这篇文章主要介绍了多视角3D逼真HTML5水波动画,它的效果非常逼真,水池中的石头在水中沉浮,泛起了一层层水波。同时我们可以拖拽鼠标从不同的视角来浏览水池,3D效果非常不错,感兴趣的小伙伴们可以参考一下
这是一款基于HTML5的3D水波动画特效,它的效果非常逼真,我们可以按“G”键来让水池中的石头上下浮动,按“L”键添加灯光效果,设计相当完美。同时说明一下,这款3D水波动画是基于WebGL渲染技术的,大家可以了解一下WebGL。
HTML代码
XML/HTML Code复制内容到剪贴板
- <img id="tiles" src="tiles.jpg">
- <img id="xneg" src="xneg.jpg">
- <img id="xpos" src="xpos.jpg">
- <img id="ypos" src="ypos.jpg">
- <img id="zneg" src="zneg.jpg">
- <img id="zpos" src="zpos.jpg">
JavaScript代码
JavaScript Code复制内容到剪贴板
- 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() {\
- /* get vertex info */\
- vec4 info = texture2D(texture, coord);\
- \
- /* add the drop to the height */\
- 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() {\
- /* get vertex info */\
- vec4 info = texture2D(texture, coord);\
- \
- /* calculate average neighbor height */\
- 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;\
- \
- /* change the velocity to move toward the average */\
- info.g += (average - info.r) * 2.0;\
- \
- /* attenuate the velocity a little so waves do not last forever */\
- info.g *= 0.995;\
- \
- /* move the vertex along the velocity */\
- info.r += info.g;\
- \
- gl_FragColor = info;\
- }\
- ');
- this.normalShader = new GL.Shader(vertexShader, '\
- uniform sampler2D texture;\
- uniform vec2 delta;\
- varying vec2 coord;\
- void main() {\
- /* get vertex info */\
- vec4 info = texture2D(texture, coord);\
- \
- /* update the normal */\
- 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() {\
- /* get vertex info */\
- vec4 info = texture2D(texture, coord);\
- \
- /* add the old volume */\
- info.r += volumeInSphere(oldCenter);\
- \
- /* subtract the new volume */\
- 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);
- };
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关文章
- 这篇文章主要为大家详细介绍了HTML5 canvas实现雪花飘落特效,效果实现引人入胜,很逼真的动画效果,感兴趣的小伙伴们可以参考一下2016-03-08
- HTML5实现波浪滚动起伏动画特效源码是一款用HTML5的canvas给大家制作了一个波浪加载百分比,并滚动的动画效果。需要的朋友前来下载源码2016-03-08
- HTML5+SVG实现盒子爆炸动画特效源码是一款在用户点击页面中的一个弹跳的盒子的时候,盒子会爆炸为烟雾,然后会出现SVG Logo,整个效果非常连贯逼真,需要的朋友前来下载源2016-03-07
- HTML5+Canvas实现日期圆形时钟特效源码是一款可以全屏漂浮,带有日期星期显示的时钟代码,外观非常漂亮,需要的朋友前来下载源码2016-03-07
- 一款基于HTML5的3D水波动画特效,效果非常逼真2016-03-03
- 这篇文章主要介绍了HTML5的新特性,需要的朋友可以参考下的相关资料2016-03-03
- 这是一个款绚丽的HTML5 Canvas动画,它将模拟的是我们生活中烟花绽放的动画特效,效果非常逼真,下面我们来简单分析一下实现这款HTML5烟花特效的过程及代码,感兴趣的小伙2016-03-02
- Notification生成的消息不依附于某个页面,仅仅依附于浏览器。 下面通过本文给大家介绍HTML5实现Notification API桌面通知功能,需要的朋友参考下吧2016-03-02
- 这篇文章主要为大家介绍了一款非常棒的多视角3D可旋转的HTML5 Logo动画,可以无限的旋转,从而可以从多个视角来观察HTML5 Logo,感兴趣的小伙伴们可以参考一下2016-03-02
- 下面小编就为大家分享一篇html5+css3进度条倒计时动画特效代码【推荐】。希望对大家有所帮助。一起跟随小编过来看看吧2016-03-08
最新评论