This article mainly introduces 3D lifelike HTML5 water wave animation with multiple perspectives. The effects of the animation are extremely lifelike. The stones in the pool are flushed in the water, and water waves are formed layer by layer. At the same time, we can drag the mouse to view the pool from different perspectives. The 3D effect is very good. If you are interested, you can refer to it as a HTML5-based 3D water wave animation special effect, the 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 the light effect, the design is quite perfect. At the same time, this 3D water wave animation is based on WebGL rendering technology. You can take a look at WebGL.
Online Preview Source code download
HTML code
Copy XML/HTML Code to clipboard
-
-
-
-
-
-
-
JavaScript code
Copy the content to the clipboard using 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 );
- };
The above is all the content of this article, hoping to help you learn.