webgl_ribbons.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/Three.js"></script>
  31. <script type="text/javascript" src="js/Detector.js"></script>
  32. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  33. <script type="text/javascript" src="js/Stats.js"></script>
  34. <div id="info">
  35. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl ribbons example
  36. </div>
  37. <script type="text/javascript">
  38. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  39. var container, stats;
  40. var camera, scene, renderer, ribbon, geometry, geometry2, materials = [], ribbons = [],
  41. parameters, i, i2, h, color, x, y, z, z2, s, n, n2, nribbons, grid;
  42. var mouseX = 0, mouseY = 0;
  43. var windowHalfX = window.innerWidth / 2;
  44. var windowHalfY = window.innerHeight / 2;
  45. var postprocessing = { enabled : true };
  46. init();
  47. animate();
  48. function init() {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  52. camera.position.z = 1200;
  53. scene = new THREE.Scene();
  54. scene.fog = new THREE.FogExp2( 0x000000, 0.0016 );
  55. renderer = new THREE.WebGLRenderer();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. container.appendChild( renderer.domElement );
  58. renderer.setClearColor( scene.fog.color, 1 );
  59. geometry = new THREE.Geometry();
  60. geometry2 = new THREE.Geometry();
  61. n = 1000;
  62. n2 = 2 * n;
  63. for ( i = -n; i < n; i++ ) {
  64. i2 = i + n;
  65. x = i * 1.175;
  66. y = ( i2 % 2 ) * 5;
  67. if ( i2 % 2 ) {
  68. z = 10 * Math.sin( i2 * 0.3 ) * Math.cos( i2 * 0.1 );
  69. }
  70. vector = new THREE.Vector3( x, y, z );
  71. geometry.vertices.push( new THREE.Vertex( vector ) );
  72. vector = new THREE.Vector3( x, y, z );
  73. geometry2.vertices.push( new THREE.Vertex( vector ) );
  74. h = i2%2 ? 1 : 0.15;
  75. if( i2%4 <= 2 ) h -= 0.15;
  76. color = new THREE.Color( 0xffffff );
  77. color.setHSV( 0.1 , 0, h );
  78. geometry.colors.push( color );
  79. geometry2.colors.push( color );
  80. }
  81. var material_base = new THREE.MeshBasicMaterial( { color:0xffffff, vertex_colors:true } );
  82. renderer.initMaterial( material_base, scene.lights, scene.fog );
  83. xgrid = 34;
  84. ygrid = 15;
  85. nribbons = xgrid * ygrid;
  86. c = 0;
  87. for ( i = 0; i < xgrid; i++ )
  88. for ( j = 0; j < ygrid; j++ ) {
  89. materials[c] = new THREE.MeshBasicMaterial( { color:0xffffff, vertex_colors:true } );
  90. materials[c].program = material_base.program;
  91. materials[c].uniforms = Uniforms.clone( THREE.ShaderLib[ 'basic' ].uniforms );
  92. ribbon = new THREE.Ribbon( i % 2 ? geometry : geometry2, materials[c] );
  93. ribbon.rotation.x = 1.57;
  94. ribbon.rotation.y = 1.57;
  95. ribbon.rotation.z = -3.14;
  96. x = 40 * ( i - xgrid/2 );
  97. y = 40 * ( j - ygrid/2 );
  98. z = 0;
  99. ribbon.position.set( x, y, z );
  100. materials[c].color.setHSV( i/xgrid, 0.3 + 0.7*j/ygrid, 1 );
  101. ribbon.doubleSided = true;
  102. ribbon.matrixAutoUpdate = false;
  103. ribbon.updateMatrix();
  104. ribbons.push( ribbon );
  105. scene.addObject( ribbon );
  106. c++;
  107. }
  108. scene.matrixAutoUpdate = false;
  109. initPostprocessing();
  110. renderer.autoClear = false;
  111. stats = new Stats();
  112. stats.domElement.style.position = 'absolute';
  113. stats.domElement.style.top = '0px';
  114. container.appendChild( stats.domElement );
  115. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  116. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  117. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  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. //
  173. function animate() {
  174. requestAnimationFrame( animate );
  175. render();
  176. stats.update();
  177. }
  178. function render() {
  179. var time = new Date().getTime() * 0.00005;
  180. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  181. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  182. for ( i = -n; i < n; i++ ) {
  183. i2 = i + n;
  184. z = 10 * Math.sin( i2 * 0.1 + time*30 );
  185. z2 = 20 * Math.cos( Math.sin( i2 * 0.1 + time * 20 ) );
  186. geometry.vertices[i2].position.z = z;
  187. geometry2.vertices[i2].position.z = z2;
  188. }
  189. geometry.__dirtyVertices = true;
  190. geometry2.__dirtyVertices = true;
  191. for( i = 0; i < nribbons; i++ ) {
  192. h = ( 360 * ( i/nribbons + time ) % 360 ) / 360;
  193. materials[i].color.setHSV( h, 0.5+0.5*(i%20/20), 1 );
  194. }
  195. if ( postprocessing.enabled ) {
  196. renderer.clear();
  197. // Render scene into texture
  198. renderer.render( scene, camera, postprocessing.rtTexture1 );
  199. // Render quad with blured scene into texture (convolution pass 1)
  200. postprocessing.quad.materials = [ postprocessing.materialConvolution ];
  201. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  202. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blurx;
  203. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture2 );
  204. // Render quad with blured scene into texture (convolution pass 2)
  205. postprocessing.materialConvolution.uniforms.tDiffuse.texture = postprocessing.rtTexture2;
  206. postprocessing.materialConvolution.uniforms.uImageIncrement.value = postprocessing.blury;
  207. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture3 );
  208. // Render original scene with superimposed blur to texture
  209. postprocessing.quad.materials = [ postprocessing.materialScreen ];
  210. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture3;
  211. postprocessing.materialScreen.uniforms.opacity.value = 1.2;
  212. renderer.render( postprocessing.scene, postprocessing.camera, postprocessing.rtTexture1, false );
  213. // Render to screen
  214. postprocessing.materialScreen.uniforms.tDiffuse.texture = postprocessing.rtTexture1;
  215. renderer.render( postprocessing.scene, postprocessing.camera );
  216. } else {
  217. renderer.clear();
  218. renderer.render( scene, camera );
  219. }
  220. }
  221. </script>
  222. </body>
  223. </html>