webgl_ribbons.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - ribbons - webgl</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background-color: #000000;
  9. margin: 0px;
  10. overflow: hidden;
  11. font-family:Monospace;
  12. font-size:13px;
  13. text-align:center;
  14. font-weight: bold;
  15. text-align:center;
  16. }
  17. a {
  18. color:#0078ff;
  19. }
  20. #info {
  21. color:#fff;
  22. position: absolute;
  23. top: 0px; width: 100%;
  24. padding: 5px;
  25. z-index:100;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  31. <script type="text/javascript" src="js/Stats.js"></script>
  32. <div id="info">
  33. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl ribbons example
  34. </div>
  35. <script type="text/javascript">
  36. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  37. window.onload = init;
  38. var container, stats;
  39. var camera, scene, renderer, ribbon, geometry, geometry2, materials = [], ribbons = [],
  40. parameters, i, i2, h, color, x, y, z, z2, s, n, n2, nribbons, grid;
  41. var mouseX = 0, mouseY = 0;
  42. var windowHalfX = window.innerWidth / 2;
  43. var windowHalfY = window.innerHeight / 2;
  44. var postprocessing = {
  45. enabled : true
  46. };
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  51. camera.position.z = 1200;
  52. scene = new THREE.Scene();
  53. scene.fog = new THREE.FogExp2( 0x000000, 0.0016 );
  54. renderer = new THREE.WebGLRenderer();
  55. renderer.setSize( window.innerWidth, window.innerHeight );
  56. container.appendChild( renderer.domElement );
  57. renderer.setClearColor( scene.fog.color, 1 );
  58. geometry = new THREE.Geometry();
  59. geometry2 = new THREE.Geometry();
  60. n = 1000;
  61. n2 = 2 * n;
  62. for ( i = -n; i < n; i++ ) {
  63. i2 = i + n;
  64. x = i * 1.175;
  65. y = ( i2 % 2 ) * 5;
  66. if ( i2 % 2 ) {
  67. z = 10 * Math.sin( i2 * 0.3 ) * Math.cos( i2 * 0.1 );
  68. }
  69. vector = new THREE.Vector3( x, y, z );
  70. geometry.vertices.push( new THREE.Vertex( vector ) );
  71. vector = new THREE.Vector3( x, y, z );
  72. geometry2.vertices.push( new THREE.Vertex( vector ) );
  73. h = i2%2 ? 1 : 0.15;
  74. if( i2%4 <= 2 ) h -= 0.15;
  75. color = new THREE.Color( 0xffffff );
  76. color.setHSV( 0.1 , 0, h );
  77. geometry.colors.push( color );
  78. geometry2.colors.push( color );
  79. }
  80. var material_base = new THREE.MeshBasicMaterial( { color:0xffffff, vertex_colors:true } );
  81. renderer.initMaterial( material_base, scene.lights, scene.fog );
  82. xgrid = 34;
  83. ygrid = 15;
  84. nribbons = xgrid * ygrid;
  85. c = 0;
  86. for ( i = 0; i < xgrid; i++ )
  87. for ( j = 0; j < ygrid; j++ ) {
  88. materials[c] = new THREE.MeshBasicMaterial( { color:0xffffff, vertex_colors:true } );
  89. materials[c].program = material_base.program;
  90. materials[c].uniforms = Uniforms.clone( THREE.ShaderLib[ 'basic' ].uniforms );
  91. ribbon = new THREE.Ribbon( i % 2 ? geometry : geometry2, materials[c] );
  92. ribbon.rotation.x = 1.57;
  93. ribbon.rotation.y = 1.57;
  94. ribbon.rotation.z = -3.14;
  95. x = 40 * ( i - xgrid/2 );
  96. y = 40 * ( j - ygrid/2 );
  97. z = 0;
  98. ribbon.position.set( x, y, z );
  99. materials[c].color.setHSV( i/xgrid, 0.3 + 0.7*j/ygrid, 1 );
  100. ribbon.doubleSided = true;
  101. ribbon.matrixAutoUpdate = false;
  102. ribbon.updateMatrix();
  103. ribbons.push( ribbon );
  104. scene.addObject( ribbon );
  105. c++;
  106. }
  107. scene.matrixAutoUpdate = false;
  108. initPostprocessing();
  109. renderer.autoClear = false;
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. container.appendChild( stats.domElement );
  114. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  115. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  116. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  117. loop();
  118. }
  119. function onDocumentMouseMove( event ) {
  120. mouseX = event.clientX - windowHalfX;
  121. mouseY = event.clientY - windowHalfY;
  122. }
  123. function onDocumentTouchStart( event ) {
  124. if ( event.touches.length == 1 ) {
  125. event.preventDefault();
  126. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  127. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  128. }
  129. }
  130. function onDocumentTouchMove( event ) {
  131. if ( event.touches.length == 1 ) {
  132. event.preventDefault();
  133. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  134. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  135. }
  136. }
  137. function initPostprocessing() {
  138. postprocessing.scene = new THREE.Scene();
  139. postprocessing.camera = new THREE.Camera();
  140. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  141. postprocessing.camera.position.z = 100;
  142. var pars = { min_filter: THREE.LinearFilter, mag_filter: THREE.LinearFilter };
  143. postprocessing.rtTexture1 = new THREE.RenderTarget( window.innerWidth, window.innerHeight, pars );
  144. postprocessing.rtTexture2 = new THREE.RenderTarget( 512, 512, pars );
  145. postprocessing.rtTexture3 = new THREE.RenderTarget( 512, 512, pars );
  146. var screen_shader = ShaderUtils.lib["screen"];
  147. var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
  148. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  149. screen_uniforms["opacity"].value = 1.0;
  150. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  151. uniforms: screen_uniforms,
  152. vertex_shader: screen_shader.vertex_shader,
  153. fragment_shader: screen_shader.fragment_shader,
  154. blending: THREE.AdditiveBlending
  155. } );
  156. var convolution_shader = ShaderUtils.lib["convolution"];
  157. var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
  158. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  159. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  160. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  161. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  162. convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
  163. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  164. uniforms: convolution_uniforms,
  165. vertex_shader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertex_shader,
  166. fragment_shader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragment_shader
  167. } );
  168. postprocessing.quad = new THREE.Mesh( new Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  169. postprocessing.quad.position.z = -500;
  170. postprocessing.scene.addObject( postprocessing.quad );
  171. }
  172. function loop() {
  173. requestAnimationFrame( loop, renderer.domElement );
  174. var time = new Date().getTime() * 0.00005;
  175. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  176. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  177. for ( i = -n; i < n; i++ ) {
  178. i2 = i + n;
  179. z = 10 * Math.sin( i2 * 0.1 + time*30 );
  180. z2 = 20 * Math.cos( Math.sin( i2 * 0.1 + time * 20 ) );
  181. geometry.vertices[i2].position.z = z;
  182. geometry2.vertices[i2].position.z = z2;
  183. }
  184. geometry.__dirtyVertices = true;
  185. geometry2.__dirtyVertices = true;
  186. for( i = 0; i < nribbons; i++ ) {
  187. h = ( 360 * ( i/nribbons + time ) % 360 ) / 360;
  188. materials[i].color.setHSV( h, 0.5+0.5*(i%20/20), 1 );
  189. }
  190. if ( postprocessing.enabled ) {
  191. renderer.clear();
  192. // Render scene into texture
  193. renderer.render( scene, camera, postprocessing.rtTexture1 );
  194. // Render quad with blured scene into texture (convolution pass 1)
  195. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  196. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  197. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  198. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2 );
  199. // Render quad with blured scene into texture (convolution pass 2)
  200. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  201. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  202. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3 );
  203. // Render original scene with superimposed blur to texture
  204. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  205. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  206. postprocessing.materialScreen.uniforms.opacity.value = 1.2;
  207. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  208. // Render to screen
  209. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  210. renderer.render( postprocessing.scene, postprocessing.camera );
  211. } else {
  212. renderer.clear();
  213. renderer.render( scene, camera );
  214. }
  215. stats.update();
  216. }
  217. </script>
  218. </body>
  219. </html>