webgl_ribbons.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/ShaderExtras.js"></script>
  32. <script type="text/javascript" src="js/postprocessing/EffectComposer.js"></script>
  33. <script type="text/javascript" src="js/postprocessing/RenderPass.js"></script>
  34. <script type="text/javascript" src="js/postprocessing/ShaderPass.js"></script>
  35. <script type="text/javascript" src="js/postprocessing/BloomPass.js"></script>
  36. <script type="text/javascript" src="js/Detector.js"></script>
  37. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  38. <script type="text/javascript" src="js/Stats.js"></script>
  39. <div id="info">
  40. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl ribbons example
  41. </div>
  42. <script type="text/javascript">
  43. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  44. var container, stats;
  45. var camera, scene, renderer, ribbon, geometry, geometry2, materials = [], ribbons = [],
  46. parameters, i, i2, h, color, x, y, z, z2, s, n, n2, nribbons, grid;
  47. var mouseX = 0, mouseY = 0;
  48. var windowHalfX = window.innerWidth / 2;
  49. var windowHalfY = window.innerHeight / 2;
  50. var postprocessing = { enabled : true };
  51. var composer;
  52. init();
  53. animate();
  54. function init() {
  55. container = document.createElement( 'div' );
  56. document.body.appendChild( container );
  57. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  58. camera.position.z = 1200;
  59. scene = new THREE.Scene();
  60. scene.fog = new THREE.FogExp2( 0x000000, 0.0016 );
  61. geometry = new THREE.Geometry();
  62. geometry2 = new THREE.Geometry();
  63. n = 1000;
  64. n2 = 2 * n;
  65. for ( i = -n; i < n; i++ ) {
  66. i2 = i + n;
  67. x = i * 1.175;
  68. y = ( i2 % 2 ) * 5;
  69. if ( i2 % 2 ) {
  70. z = 10 * Math.sin( i2 * 0.3 ) * Math.cos( i2 * 0.1 );
  71. }
  72. vector = new THREE.Vector3( x, y, z );
  73. geometry.vertices.push( new THREE.Vertex( vector ) );
  74. vector = new THREE.Vector3( x, y, z );
  75. geometry2.vertices.push( new THREE.Vertex( vector ) );
  76. h = i2 % 2 ? 1 : 0.15;
  77. if( i2 % 4 <= 2 ) h -= 0.15;
  78. color = new THREE.Color( 0xffffff );
  79. color.setHSV( 0.1 , 0, h );
  80. geometry.colors.push( color );
  81. geometry2.colors.push( color );
  82. }
  83. var tmpRot = new THREE.Matrix4();
  84. tmpRot.setRotationAxis( new THREE.Vector3( 1, 0, 0 ), 1.57 );
  85. xgrid = 34;
  86. ygrid = 15;
  87. nribbons = xgrid * ygrid;
  88. c = 0;
  89. for ( i = 0; i < xgrid; i ++ )
  90. for ( j = 0; j < ygrid; j ++ ) {
  91. materials[ c ] = new THREE.MeshBasicMaterial( { color: 0xffffff, vertexColors: true } );
  92. ribbon = new THREE.Ribbon( i % 2 ? geometry : geometry2, materials[ c ] );
  93. ribbon.rotation.x = 0;
  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. // manually create local matrix
  104. ribbon.matrix.setPosition( ribbon.position );
  105. ribbon.matrixRotationWorld.setRotationFromEuler( ribbon.rotation );
  106. ribbon.matrix.n11 = ribbon.matrixRotationWorld.n11;
  107. ribbon.matrix.n12 = ribbon.matrixRotationWorld.n12;
  108. ribbon.matrix.n13 = ribbon.matrixRotationWorld.n13;
  109. ribbon.matrix.n21 = ribbon.matrixRotationWorld.n21;
  110. ribbon.matrix.n22 = ribbon.matrixRotationWorld.n22;
  111. ribbon.matrix.n23 = ribbon.matrixRotationWorld.n23;
  112. ribbon.matrix.n31 = ribbon.matrixRotationWorld.n31;
  113. ribbon.matrix.n32 = ribbon.matrixRotationWorld.n32;
  114. ribbon.matrix.n33 = ribbon.matrixRotationWorld.n33;
  115. ribbon.matrix.multiplySelf( tmpRot );
  116. ribbon.matrix.scale( ribbon.scale );
  117. ribbon.boundRadiusScale = Math.max( ribbon.scale.x, Math.max( ribbon.scale.y, ribbon.scale.z ) );
  118. ribbons.push( ribbon );
  119. scene.addObject( ribbon );
  120. c ++;
  121. }
  122. scene.matrixAutoUpdate = false;
  123. //
  124. renderer = new THREE.WebGLRenderer( { antialias: false } );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. renderer.autoClear = false;
  127. renderer.setClearColor( scene.fog.color, 1 );
  128. container.appendChild( renderer.domElement );
  129. //
  130. stats = new Stats();
  131. stats.domElement.style.position = 'absolute';
  132. stats.domElement.style.top = '0px';
  133. container.appendChild( stats.domElement );
  134. //
  135. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  136. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  137. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  138. //
  139. var renderModel = new THREE.RenderPass( scene, camera );
  140. var effectBloom = new THREE.BloomPass( 1.0 );
  141. var effectScreen = new THREE.ShaderPass( THREE.ShaderExtras[ "screen" ] );
  142. effectScreen.renderToScreen = true;
  143. composer = new THREE.EffectComposer( renderer );
  144. composer.addPass( renderModel );
  145. composer.addPass( effectBloom );
  146. composer.addPass( effectScreen );
  147. }
  148. function onDocumentMouseMove( event ) {
  149. mouseX = event.clientX - windowHalfX;
  150. mouseY = event.clientY - windowHalfY;
  151. }
  152. function onDocumentTouchStart( event ) {
  153. if ( event.touches.length == 1 ) {
  154. event.preventDefault();
  155. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  156. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  157. }
  158. }
  159. function onDocumentTouchMove( event ) {
  160. if ( event.touches.length == 1 ) {
  161. event.preventDefault();
  162. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  163. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  164. }
  165. }
  166. //
  167. function animate() {
  168. requestAnimationFrame( animate );
  169. render();
  170. stats.update();
  171. }
  172. function render() {
  173. var time = new Date().getTime() * 0.00005;
  174. camera.position.x += ( mouseX - camera.position.x ) * 0.036;
  175. camera.position.y += ( - (mouseY) - camera.position.y ) * 0.036;
  176. for ( i = -n; i < n; i ++ ) {
  177. i2 = i + n;
  178. z = 10 * Math.sin( i2 * 0.1 + time*30 );
  179. z2 = 20 * Math.cos( Math.sin( i2 * 0.1 + time * 20 ) );
  180. geometry.vertices[ i2 ].position.z = z;
  181. geometry2.vertices[ i2 ].position.z = z2;
  182. }
  183. geometry.__dirtyVertices = true;
  184. geometry2.__dirtyVertices = true;
  185. for( i = 0; i < nribbons; i++ ) {
  186. h = ( 360 * ( i / nribbons + time ) % 360 ) / 360;
  187. materials[ i ].color.setHSV( h, 0.5 + 0.5 * ( i % 20 / 20 ), 1 );
  188. }
  189. renderer.clear();
  190. composer.render( 0.1 );
  191. }
  192. </script>
  193. </body>
  194. </html>