zhiqim_webgl_three.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
  3. *
  4. * 指定登记&发行网站: https://www.zhiqim.com/ 欢迎加盟知启蒙,[编程有你,知启蒙一路随行]。
  5. *
  6. * 本文采用《知启蒙许可证》,除非符合许可证,否则不可使该文件!
  7. * 1、您可以免费使用、修改、合并、出版发行和分发,再授权软件、软件副本及衍生软件;
  8. * 2、您用于商业用途时,必须在原作者指定的登记网站进行实名登记;
  9. * 3、您在使用、修改、合并、出版发行和分发时,必须包含版权声明、许可声明,及保留原作者的著作权、商标和专利等知识产权;
  10. * 4、您在互联网、移动互联网等大众网络下发行和分发再授权软件、软件副本及衍生软件时,必须在原作者指定的发行网站进行发行和分发;
  11. * 5、您可以在以下链接获取一个完整的许可证副本。
  12. *
  13. * 许可证链接:http://zhiqim.org/licenses/LICENSE.htm
  14. *
  15. * 除非法律需要或书面同意,软件由原始码方式提供,无任何明示或暗示的保证和条件。详见完整许可证的权限和限制。
  16. */
  17. +(function(Z)
  18. {
  19. //BEGIN
  20. /**
  21. * WebGL+three.js 3D动画,当前有波浪和光线球两种
  22. */
  23. Z.WebGLThree = Z.Class.newInstance();
  24. Z.WebGLThree.prototype =
  25. {
  26. defaults:
  27. {
  28. //常量
  29. SEPARATION: 125,
  30. AMOUNTX: 35,
  31. AMOUNTY: 35,
  32. //传入参数
  33. threePath: null,
  34. elem: null,
  35. //内部对象
  36. camera: null,
  37. scene: null,
  38. renderer: null,
  39. particles_ware: [],
  40. particles_globe: [],
  41. //运行时
  42. count: 0,
  43. mouseX: 0,
  44. mouseY: 0,
  45. windowHalfX: window.innerWidth / 2,
  46. windowHalfY: window.innerHeight / 2,
  47. rotation_speed: .002,
  48. timeout: null
  49. },
  50. execute: function()
  51. {
  52. if (Z.B.mobile || Z.B.msieVer <= 9)
  53. {//移动端和IE9以下不支持
  54. return;
  55. }
  56. if (this.threePath == null || this.elem == null)
  57. {//两个参数必须,未传不处理
  58. return;
  59. }
  60. //先加载three.js,再初始化init
  61. this.$elem = Z.$elem(this.elem, "Z.WebGLThree");
  62. Z.loads(this.threePath, Z.bind(this.initWebGL, this));
  63. },
  64. initWebGL: function()
  65. {
  66. var animationType = Math.floor(2 * Math.random());
  67. if (animationType == 0)
  68. {//波浪
  69. this.initWave();
  70. this.animateWave();
  71. }
  72. else
  73. {//光球
  74. this.initGlobe();
  75. this.animateGlobe();
  76. }
  77. //增加事件处理
  78. Z(document).mousemove(this.onDocumentMouseMove, this)
  79. .on("touchstart", this.onDocumentTouchStart, this)
  80. .on("touchmove", this.onDocumentTouchMove, this);
  81. Z(window).resize(this.onWindowResize, this);
  82. },
  83. /******************************************************************************************/
  84. //光球
  85. /******************************************************************************************/
  86. initGlobe: function initGlobe()
  87. {
  88. this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1e4),
  89. this.camera.position.z = 500,
  90. this.scene = new THREE.Scene;
  91. for (var t = 2 * Math.PI, i = function (e) {e.beginPath(), e.arc(0, 0, 25, 0, t, true), e.fill()}, t = 2 * Math.PI, n = 0; 500 > n; n++)
  92. {
  93. var o = new THREE.SpriteCanvasMaterial({color: 16777215,
  94. transparent: true,
  95. program: function (e) {e.beginPath(), e.arc(0, 0, .5, 0, t, true), e.fill()}});
  96. var particle = new THREE.Sprite(o);
  97. particle.position.x = 2 * Math.random() - 1;
  98. particle.position.y = 2 * Math.random() - 1;
  99. particle.position.z = 2 * Math.random() - 1;
  100. particle.position.normalize();
  101. particle.position.multiplyScalar(10 * Math.random() + 450);
  102. particle.scale.multiplyScalar(4 + 2 * Math.random());
  103. particle.material.opacity = .1;
  104. this.scene.add(particle);
  105. this.particles_globe.push(particle);
  106. }
  107. for (var n = 0; 500 > n; n++)
  108. {
  109. var s = new THREE.Geometry;
  110. var r = new THREE.Vector3(2 * Math.random() - 1, 2 * Math.random() - 1, 2 * Math.random() - 1);
  111. r.normalize();
  112. r.multiplyScalar(450);
  113. s.vertices.push(r);
  114. var a = r.clone();
  115. a.multiplyScalar(.3 * Math.random() + 1), s.vertices.push(a);
  116. var l = new THREE.Line(s, new THREE.LineBasicMaterial({color: 16777215, opacity: .3}));
  117. this.scene.add(l)
  118. }
  119. this.renderer = new THREE.CanvasRenderer({alpha: true});
  120. this.renderer.setClearColor(0, 0);
  121. this.renderer.setSize(window.innerWidth, window.innerHeight);
  122. this.$elem.append(this.renderer.domElement)
  123. },
  124. animateGlobe: function()
  125. {
  126. requestAnimationFrame(Z.bind(this.animateGlobe, this));
  127. this.renderGlobe();
  128. },
  129. renderGlobe: function()
  130. {
  131. var e = Z("body:hover"), t = this.camera.position.x, i = this.camera.position.y, n = this.camera.position.z;
  132. if (e.length != 0 && this.timeout != null)
  133. {
  134. this.camera.position.x += .05 * (this.mouseX - this.camera.position.x);
  135. }
  136. else
  137. {
  138. this.camera.position.x = t * Math.cos(this.rotation_speed) - n * Math.sin(this.rotation_speed);
  139. this.camera.position.z = n * Math.cos(this.rotation_speed) + t * Math.sin(this.rotation_speed);
  140. }
  141. this.camera.position.y += .05 * (-this.mouseY + 200 - this.camera.position.y);
  142. this.camera.lookAt(this.scene.position);
  143. Z(document).mousemove(function () {
  144. this.timeout !== null && clearTimeout(this.timeout), this.timeout = setTimeout(function () {this.timeout = null}, 600)
  145. });
  146. for (var o = 0; o < this.particles_globe.length; o++)
  147. {
  148. var particle = this.particles_globe[o++];
  149. temp = 50 * Math.sin(.3 * (o + this.count)) + .5 * Math.sin(.5 * (o + this.count));
  150. opacity = Math.abs(temp) / 50 + .1;
  151. opacity > 1 && (opacity = 1);
  152. particle.material.opacity = opacity;
  153. }
  154. this.renderer.render(this.scene, this.camera);
  155. this.count += .1;
  156. },
  157. /******************************************************************************************/
  158. //波浪
  159. /******************************************************************************************/
  160. initWave: function()
  161. {
  162. this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1e4);
  163. this.camera.position.z = 1e3;
  164. this.camera.position.y = 100;
  165. this.camera.position.y = 1e3;
  166. this.scene = new THREE.Scene;
  167. for (var t = 2 * Math.PI, i = 0, n = 0; this.AMOUNTX > n; n++)for (var o = 0; this.AMOUNTY > o; o++)
  168. {
  169. var s = new THREE.SpriteCanvasMaterial({color: 16777215,
  170. transparent: true,
  171. program: function (e) {e.beginPath(), e.arc(0, 0, .5, 0, t, true), e.fill()}});
  172. var particle = this.particles_ware[i++] = new THREE.Sprite(s);
  173. particle.position.x = n * this.SEPARATION - this.AMOUNTX * this.SEPARATION / 2;
  174. particle.position.z = o * this.SEPARATION - this.AMOUNTY * this.SEPARATION / 2;
  175. this.scene.add(particle), particle.material.opacity = .4;
  176. }
  177. this.renderer = new THREE.CanvasRenderer({alpha: true});
  178. this.renderer.setClearColor(0, 0);
  179. this.renderer.setSize(window.innerWidth, window.innerHeight);
  180. this.$elem.append(this.renderer.domElement);
  181. },
  182. animateWave: function()
  183. {
  184. requestAnimationFrame(Z.bind(this.animateWave, this));
  185. this.renderWave();
  186. },
  187. renderWave: function()
  188. {
  189. this.camera.position.x += .01 * (this.mouseX - this.camera.position.x),
  190. this.camera.position.y += .005 * (this.mouseY - this.camera.position.y),
  191. this.camera.lookAt(this.scene.position);
  192. for (var e = 0, t = 0; this.AMOUNTX > t; t++)
  193. {
  194. for (var i = 0; this.AMOUNTY > i; i++)
  195. {
  196. var particle = this.particles_ware[e++];
  197. particle.position.y = 50 * Math.sin(.3 * (t + this.count)) + 50 * Math.sin(.5 * (i + this.count));
  198. particle.scale.x = particle.scale.y = 4 * (Math.sin(.3 * (t + this.count)) + 1) + 4 * (Math.sin(.5 * (i + this.count)) + 1);
  199. opacity = Math.abs(particle.position.y) / 100;
  200. opacity < .5 && (opacity = .5);
  201. opacity > 1 && (opacity = 1);
  202. particle.material.opacity = opacity;
  203. }
  204. }
  205. this.renderer.render(this.scene, this.camera);
  206. this.count += .03
  207. },
  208. /******************************************************************************************/
  209. //事件处理
  210. /******************************************************************************************/
  211. onWindowResize: function()
  212. {//缩放
  213. this.windowHalfX = window.innerWidth / 2,
  214. this.windowHalfY = window.innerHeight / 2,
  215. this.camera.aspect = window.innerWidth / window.innerHeight,
  216. this.camera.updateProjectionMatrix(),
  217. this.renderer.setSize(window.innerWidth, window.innerHeight);
  218. },
  219. onDocumentMouseMove: function(e)
  220. {//鼠标移动
  221. this.mouseX = e.clientX - this.windowHalfX, this.mouseY = e.clientY + 150;
  222. },
  223. onDocumentTouchStart: function(e)
  224. {//iOS触摸开始
  225. 1 === e.touches.length && (e.preventDefault(), this.mouseX = e.touches[0].pageX - this.windowHalfX, this.mouseY = -e.touches[0].pageY);
  226. },
  227. onDocumentTouchMove: function(e)
  228. {//iOS触摸移动
  229. 1 === e.touches.length && (e.preventDefault(), this.mouseX = e.touches[0].pageX - this.windowHalfX, this.mouseY = -e.touches[0].pageY);
  230. }
  231. };
  232. //END
  233. })(zhiqim);