3D lifelike HTML5 water wave animation with multiple perspectives _ html5 tutorial skills-

Source: Internet
Author: User
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

  1. Function Water (){
  2. Var vertexShader = '\
  3. Varying vec2 coord ;\
  4. Void main (){\
  5. Coord = gl_Vertex.xy * 0.5 + 0.5 ;\
  6. Gl_Position = vec4 (gl_Vertex.xyz, 1.0 );\
  7. }\
  8. ';
  9. This. plane = GL. Mesh. plane ();
  10. If (! GL. Texture. canUseFloatingPointTextures ()){
  11. Throw new Error ('this demo requires the OES_texture_float extension ');
  12. }
  13. Var filter = GL. Texture. canUseFloatingPointLinearFiltering ()? Gl. LINEAR: gl. NEAREST;
  14. This. textureA = new GL. Texture (256,256, {type: gl. FLOAT, filter: filter });
  15. This. textureB = new GL. Texture (256,256, {type: gl. FLOAT, filter: filter });
  16. This. dropShader = new GL. Shader (vertexShader ,'\
  17. Const float PI = 3.141592653589793 ;\
  18. Uniform sampler2D texture ;\
  19. Uniform vec2 center ;\
  20. Uniform float radius ;\
  21. Uniform float strength ;\
  22. Varying vec2 coord ;\
  23. Void main (){\
  24. /* Get vertex info */\
  25. Vec4 info = texture2D (texture, coord );\
  26. \
  27. /* Add the drop to the height */\
  28. Float drop = max (0.0, 1.0-length (center * 0.5 + 0.5-coord)/radius );\
  29. Drop = 0.5-cos (drop * PI) * 0.5 ;\
  30. Info. r + = drop * strength ;\
  31. \
  32. Gl_FragColor = info ;\
  33. }\
  34. ');
  35. This. updateShader = new GL. Shader (vertexShader ,'\
  36. Uniform sampler2D texture ;\
  37. Uniform vec2 delta ;\
  38. Varying vec2 coord ;\
  39. Void main (){\
  40. /* Get vertex info */\
  41. Vec4 info = texture2D (texture, coord );\
  42. \
  43. /* Calculate average neighbor height */\
  44. Vec2 dx = vec2 (delta. x, 0.0 );\
  45. Vec2 dy = vec2 (0.0, delta. y );\
  46. Float average = (\
  47. Texture2D (texture, coord-dx). r ++ \
  48. Texture2D (texture, coord-dy). r ++ \
  49. Texture2D (texture, coord + dx). r ++ \
  50. Texture2D (texture, coord + dy). r \
  51. ) * 0.25 ;\
  52. \
  53. /* Change the velocity to move toward the average */\
  54. Info. g + = (average-info. r) * 2.0 ;\
  55. \
  56. /* Attenuate the velocity a little so waves do not last forever */\
  57. Info. g * = 0.995 ;\
  58. \
  59. /* Move the vertex along the velocity */\
  60. Info. r + = info. g ;\
  61. \
  62. Gl_FragColor = info ;\
  63. }\
  64. ');
  65. This. normalShader = new GL. Shader (vertexShader ,'\
  66. Uniform sampler2D texture ;\
  67. Uniform vec2 delta ;\
  68. Varying vec2 coord ;\
  69. Void main (){\
  70. /* Get vertex info */\
  71. Vec4 info = texture2D (texture, coord );\
  72. \
  73. /* Update the normal */\
  74. Vec3 dx = vec3 (delta. x, texture2D (texture, vec2 (coord. x + delta. x, coord. y). r-info. r, 0.0 );\
  75. Vec3 dy = vec3 (0.0, texture2D (texture, vec2 (coord. x, coord. y + delta. y). r-info. r, delta. y );\
  76. Info. ba = normalize (cross (dy, dx). xz ;\
  77. \
  78. Gl_FragColor = info ;\
  79. }\
  80. ');
  81. This. sphereShader = new GL. Shader (vertexShader ,'\
  82. Uniform sampler2D texture ;\
  83. Uniform vec3 oldCenter ;\
  84. Uniform vec3 newCenter ;\
  85. Uniform float radius ;\
  86. Varying vec2 coord ;\
  87. \
  88. Float volumeInSphere (vec3 center ){\
  89. Vec3 toCenter = vec3 (coord. x * 2.0-1.0, 0.0, coord. y * 2.0-1.0)-center ;\
  90. Float t = length (toCenter)/radius ;\
  91. Float dy = exp (-pow (t * 1.5, 6.0 ));\
  92. Float ymin = min (0.0, center. y-dy );\
  93. Float ymax = min (max (0.0, center. y + dy), ymin + 2.0 * dy );\
  94. Return (ymax-ymin) * 0.1 ;\
  95. }\
  96. \
  97. Void main (){\
  98. /* Get vertex info */\
  99. Vec4 info = texture2D (texture, coord );\
  100. \
  101. /* Add the old volume */\
  102. Info. r + = volumeInSphere (oldCenter );\
  103. \
  104. /* Subtract the new volume */\
  105. Info. r-= volumeInSphere (newCenter );\
  106. \
  107. Gl_FragColor = info ;\
  108. }\
  109. ');
  110. }
  111. Water. prototype. addDrop = function (x, y, radius, strength ){
  112. Var this _ = this;
  113. This. textureB. drawTo (function (){
  114. This _. textureA. bind ();
  115. This _. dropShader. uniforms ({
  116. Center: [x, y],
  117. Radius: radius,
  118. Strength: strength
  119. }). Draw (this _. plane );
  120. });
  121. This. textureB. swapWith (this. textureA );
  122. };
  123. Water. prototype. moveSphere = function (oldCenter, newCenter, radius ){
  124. Var this _ = this;
  125. This. textureB. drawTo (function (){
  126. This _. textureA. bind ();
  127. This _. sphereShader. uniforms ({
  128. OldCenter: oldCenter,
  129. NewCenter: newCenter,
  130. Radius: radius
  131. }). Draw (this _. plane );
  132. });
  133. This. textureB. swapWith (this. textureA );
  134. };
  135. Water. prototype. stepSimulation = function (){
  136. Var this _ = this;
  137. This. textureB. drawTo (function (){
  138. This _. textureA. bind ();
  139. This _. updateShader. uniforms ({
  140. Delta: [1/this _. textureA. width, 1/this _. textureA. height]
  141. }). Draw (this _. plane );
  142. });
  143. This. textureB. swapWith (this. textureA );
  144. };
  145. Water. prototype. updateNormals = function (){
  146. Var this _ = this;
  147. This. textureB. drawTo (function (){
  148. This _. textureA. bind ();
  149. This _. normalShader. uniforms ({
  150. Delta: [1/this _. textureA. width, 1/this _. textureA. height]
  151. }). Draw (this _. plane );
  152. });
  153. This. textureB. swapWith (this. textureA );
  154. };

The above is all the content of this article, hoping to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.