


Multi-perspective 3D realistic HTML5 water wave animation _html5 tutorial skills
This is a 3D water wave animation special effect based on HTML5. Its effect is very realistic. We can press the "G" key to make the stones in the pool float up and down, and press the "L" key to add lighting effects. The design is quite perfect. At the same time, please note that this 3D water wave animation is based on WebGL rendering technology. You can learn about WebGL.
Online preview Source code download
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 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);
- };
以上就是本文的全部内容,希望对大家的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
