Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Multi-view 3D realistic HTML5 Water Wave Animation

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

How to make multi-view 3D realistic HTML5 water wave animation, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

This is a HTML5-based 3D water wave animation special effect, its effect is very realistic, we can press the "G" key to let the stones in the pool float up and down, press the "L" key to add lighting effects, the design is quite perfect. At the same time, this 3D water wave animation is based on WebGL rendering technology, you can learn about WebGL.

HTML code

XML/HTML Code copies content to the clipboard

JavaScript code

JavaScript Code copies content to the clipboard

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,0.0, coord.y * 2.01.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);} This is the answer to the question on how to make multi-view 3D realistic HTML5 water wave animation. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 240

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report