多視角3D逼真HTML5水波動畫
發(fā)布時間:2016-03-03 10:39:57 作者:佚名
我要評論

這篇文章主要介紹了多視角3D逼真HTML5水波動畫,它的效果非常逼真,水池中的石頭在水中沉浮,泛起了一層層水波。同時我們可以拖拽鼠標(biāo)從不同的視角來瀏覽水池,3D效果非常不錯,感興趣的小伙伴們可以參考一下
這是一款基于HTML5的3D水波動畫特效,它的效果非常逼真,我們可以按“G”鍵來讓水池中的石頭上下浮動,按“L”鍵添加燈光效果,設(shè)計相當(dāng)完美。同時說明一下,這款3D水波動畫是基于WebGL渲染技術(shù)的,大家可以了解一下WebGL。
HTML代碼
XML/HTML Code復(fù)制內(nèi)容到剪貼板
- <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復(fù)制內(nèi)容到剪貼板
- 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);
- };
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
- 這篇文章主要為大家詳細(xì)介紹了HTML5 canvas實現(xiàn)雪花飄落特效,效果實現(xiàn)引人入勝,很逼真的動畫效果,感興趣的小伙伴們可以參考一下2016-03-08
- HTML5實現(xiàn)波浪滾動起伏動畫特效源碼是一款用HTML5的canvas給大家制作了一個波浪加載百分比,并滾動的動畫效果。需要的朋友前來下載源碼2016-03-08
- HTML5+SVG實現(xiàn)盒子爆炸動畫特效源碼是一款在用戶點擊頁面中的一個彈跳的盒子的時候,盒子會爆炸為煙霧,然后會出現(xiàn)SVG Logo,整個效果非常連貫逼真,需要的朋友前來下載源2016-03-07
HTML5+Canvas實現(xiàn)日期圓形時鐘特效源碼
HTML5+Canvas實現(xiàn)日期圓形時鐘特效源碼是一款可以全屏漂浮,帶有日期星期顯示的時鐘代碼,外觀非常漂亮,需要的朋友前來下載源碼2016-03-07- 一款基于HTML5的3D水波動畫特效,效果非常逼真2016-03-03
- 這篇文章主要介紹了HTML5的新特性,需要的朋友可以參考下的相關(guān)資料2016-03-03
- 這是一個款絢麗的HTML5 Canvas動畫,它將模擬的是我們生活中煙花綻放的動畫特效,效果非常逼真,下面我們來簡單分析一下實現(xiàn)這款HTML5煙花特效的過程及代碼,感興趣的小伙2016-03-02
HTML5實現(xiàn)Notification API桌面通知功能
Notification生成的消息不依附于某個頁面,僅僅依附于瀏覽器。 下面通過本文給大家介紹HTML5實現(xiàn)Notification API桌面通知功能,需要的朋友參考下吧2016-03-02- 這篇文章主要為大家介紹了一款非常棒的多視角3D可旋轉(zhuǎn)的HTML5 Logo動畫,可以無限的旋轉(zhuǎn),從而可以從多個視角來觀察HTML5 Logo,感興趣的小伙伴們可以參考一下2016-03-02
html5+css3進(jìn)度條倒計時動畫特效代碼【推薦】
下面小編就為大家分享一篇html5+css3進(jìn)度條倒計時動畫特效代碼【推薦】。希望對大家有所幫助。一起跟隨小編過來看看吧2016-03-08