webgl_ribbons.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - ribbons</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/RequestAnimationFrame.js"></script>
  32. <script type="text/javascript" src="js/Stats.js"></script>
  33. <div id="info">
  34. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl ribbons example
  35. </div>
  36. <script type="text/javascript">
  37. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  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 = { enabled : true };
  45. init();
  46. animate();
  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. }
  118. function onDocumentMouseMove( event ) {
  119. mouseX = event.clientX - windowHalfX;
  120. mouseY = event.clientY - windowHalfY;
  121. }
  122. function onDocumentTouchStart( event ) {
  123. if ( event.touches.length == 1 ) {
  124. event.preventDefault();
  125. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  126. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  127. }
  128. }
  129. function onDocumentTouchMove( event ) {
  130. if ( event.touches.length == 1 ) {
  131. event.preventDefault();
  132. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  133. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  134. }
  135. }
  136. function initPostprocessing() {
  137. postprocessing.scene = new THREE.Scene();
  138. postprocessing.camera = new THREE.Camera();
  139. postprocessing.camera.projectionMatrix = THREE.Matrix4.makeOrtho( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -10000, 10000 );
  140. postprocessing.camera.position.z = 100;
  141. var pars = { min_filter: THREE.LinearFilter, mag_filter: THREE.LinearFilter };
  142. postprocessing.rtTexture1 = new THREE.RenderTarget( window.innerWidth, window.innerHeight, pars );
  143. postprocessing.rtTexture2 = new THREE.RenderTarget( 512, 512, pars );
  144. postprocessing.rtTexture3 = new THREE.RenderTarget( 512, 512, pars );
  145. var screen_shader = ShaderUtils.lib["screen"];
  146. var screen_uniforms = Uniforms.clone( screen_shader.uniforms );
  147. screen_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  148. screen_uniforms["opacity"].value = 1.0;
  149. postprocessing.materialScreen = new THREE.MeshShaderMaterial( {
  150. uniforms: screen_uniforms,
  151. vertex_shader: screen_shader.vertex_shader,
  152. fragment_shader: screen_shader.fragment_shader,
  153. blending: THREE.AdditiveBlending
  154. } );
  155. var convolution_shader = ShaderUtils.lib["convolution"];
  156. var convolution_uniforms = Uniforms.clone( convolution_shader.uniforms );
  157. postprocessing.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  158. postprocessing.blury = new THREE.Vector2( 0.0, 0.001953125 );
  159. convolution_uniforms["tDiffuse"].texture = postprocessing.rtTexture1;
  160. convolution_uniforms["uImageIncrement"].value = postprocessing.blurx;
  161. convolution_uniforms["cKernel"].value = ShaderUtils.buildKernel( 4.0 );
  162. postprocessing.materialConvolution = new THREE.MeshShaderMaterial( {
  163. uniforms: convolution_uniforms,
  164. vertex_shader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertex_shader,
  165. fragment_shader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragment_shader
  166. } );
  167. postprocessing.quad = new THREE.Mesh( new Plane( window.innerWidth, window.innerHeight ), postprocessing.materialConvolution );
  168. postprocessing.quad.position.z = -500;
  169. postprocessing.scene.addObject( postprocessing.quad );
  170. }
  171. //
  172. function animate() {
  173. requestAnimationFrame( animate );
  174. render();
  175. stats.update();
  176. }
  177. function render() {
  178. var time = new Date().getTime() * 0.00005;
  179. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  180. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  181. for ( i = -n; i < n; i++ ) {
  182. i2 = i + n;
  183. z = 10 * Math.sin( i2 * 0.1 + time*30 );
  184. z2 = 20 * Math.cos( Math.sin( i2 * 0.1 + time * 20 ) );
  185. geometry.vertices[i2].position.z = z;
  186. geometry2.vertices[i2].position.z = z2;
  187. }
  188. geometry.__dirtyVertices = true;
  189. geometry2.__dirtyVertices = true;
  190. for( i = 0; i < nribbons; i++ ) {
  191. h = ( 360 * ( i/nribbons + time ) % 360 ) / 360;
  192. materials[i].color.setHSV( h, 0.5+0.5*(i%20/20), 1 );
  193. }
  194. if ( postprocessing.enabled ) {
  195. renderer.clear();
  196. // Render scene into texture
  197. renderer.render( scene, camera, postprocessing.rtTexture1 );
  198. // Render quad with blured scene into texture (convolution pass 1)
  199. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  200. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  201. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  202. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2 );
  203. // Render quad with blured scene into texture (convolution pass 2)
  204. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  205. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  206. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3 );
  207. // Render original scene with superimposed blur to texture
  208. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  209. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  210. postprocessing.materialScreen.uniforms.opacity.value = 1.2;
  211. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  212. // Render to screen
  213. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  214. renderer.render( postprocessing.scene, postprocessing.camera );
  215. } else {
  216. renderer.clear();
  217. renderer.render( scene, camera );
  218. }
  219. }
  220. </script>
  221. </body>
  222. </html>