webgl_lines_cubes.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - lines - cubes - 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. }
  12. a {
  13. color:#0078ff;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 10px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family: Monospace;
  21. font-size: 13px;
  22. text-align: center;
  23. z-index:100;
  24. }
  25. a {
  26. color: orange;
  27. text-decoration: none;
  28. }
  29. a:hover {
  30. color: #0080ff;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - lines WebGL demo
  37. [<a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert curve</a> thanks to <a href="http://www.openprocessing.org/visuals/?visualID=15599">Thomas Diewald</a>]
  38. </div>
  39. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  40. <script type="text/javascript" src="js/Stats.js"></script>
  41. <script type="text/javascript">
  42. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  43. var mouseX = 0, mouseY = 0,
  44. windowHalfX = window.innerWidth / 2,
  45. windowHalfY = window.innerHeight / 2,
  46. camera, scene, renderer,
  47. stats;
  48. init();
  49. setInterval( loop, 1000 / 60 );
  50. function init() {
  51. var i, material, container;
  52. container = document.createElement('div');
  53. document.body.appendChild(container);
  54. camera = new THREE.Camera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  55. camera.position.z = 700;
  56. scene = new THREE.Scene();
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setSize( window.innerWidth, window.innerHeight );
  59. container.appendChild( renderer.domElement );
  60. var geometry = new THREE.Geometry(),
  61. points = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 4, 0, 1, 2, 3, 4, 5, 6, 7 );
  62. for ( i = 0; i < points.length; i ++ ) {
  63. geometry.vertices.push( new THREE.Vertex( points[i] ) );
  64. }
  65. // lines
  66. var line, p, scale = 0.3, d = 125, c1 = 0x553300, c2 = 0x555555, c3 = 0x992800, g1 = geometry,
  67. m1 = new THREE.LineBasicMaterial( { color: c1, opacity: 1, blending: THREE.AdditiveBlending } ),
  68. m2 = new THREE.LineBasicMaterial( { color: c2, opacity: 1, blending: THREE.AdditiveBlending } ),
  69. m3 = new THREE.LineBasicMaterial( { color: c3, opacity: 1, blending: THREE.AdditiveBlending } ),
  70. parameters = [ [ m3, scale*0.5, [0,0,0], g1 ], [ m2, scale*0.5, [d, 0, 0], g1 ], [ m2, scale*0.5, [-d, 0, 0], g1 ],
  71. [ m2, scale*0.5, [0,d,0], g1 ], [ m2, scale*0.5, [d, d, 0], g1 ], [ m2, scale*0.5, [-d, d, 0], g1 ],
  72. [ m2, scale*0.5, [0,-d,0], g1 ], [ m2, scale*0.5, [d, -d, 0], g1 ], [ m2, scale*0.5, [-d, -d, 0], g1 ],
  73. [ m1, scale*0.5, [2*d, 0, 0], g1 ], [ m1, scale*0.5, [-2*d, 0, 0], g1 ],
  74. [ m1, scale*0.5, [2*d, d, 0], g1 ], [ m1, scale*0.5, [-2*d, d, 0], g1 ],
  75. [ m1, scale*0.5, [2*d, -d, 0], g1 ], [ m1, scale*0.5, [-2*d, -d, 0], g1 ],
  76. ];
  77. for ( i = 0; i < parameters.length; ++i ) {
  78. p = parameters[ i ];
  79. line = new THREE.Line( p[ 3 ], p[ 0 ] );
  80. line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
  81. line.position.x = p[ 2 ][ 0 ];
  82. line.position.y = p[ 2 ][ 1 ];
  83. line.position.z = p[ 2 ][ 2 ];
  84. scene.addObject( line );
  85. }
  86. /*
  87. stats = new Stats();
  88. stats.domElement.style.position = 'absolute';
  89. stats.domElement.style.top = '0px';
  90. container.appendChild(stats.domElement);
  91. */
  92. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  93. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  94. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  95. }
  96. // port of Processing Java code by Thomas Diewald
  97. // http://www.openprocessing.org/visuals/?visualID=15599
  98. function hilbert3D( center, side, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) {
  99. var half = side/2,
  100. vec_s = [
  101. new THREE.Vector3( center.x - half, center.y + half, center.z - half ),
  102. new THREE.Vector3( center.x - half, center.y + half, center.z + half ),
  103. new THREE.Vector3( center.x - half, center.y - half, center.z + half ),
  104. new THREE.Vector3( center.x - half, center.y - half, center.z - half ),
  105. new THREE.Vector3( center.x + half, center.y - half, center.z - half ),
  106. new THREE.Vector3( center.x + half, center.y - half, center.z + half ),
  107. new THREE.Vector3( center.x + half, center.y + half, center.z + half ),
  108. new THREE.Vector3( center.x + half, center.y + half, center.z - half )
  109. ],
  110. vec = [ vec_s[ v0 ], vec_s[ v1 ], vec_s[ v2 ], vec_s[ v3 ], vec_s[ v4 ], vec_s[ v5 ], vec_s[ v6 ], vec_s[ v7 ] ];
  111. if( --iterations >= 0 ) {
  112. var tmp = [];
  113. tmp = tmp.concat( hilbert3D ( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
  114. tmp = tmp.concat( hilbert3D ( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  115. tmp = tmp.concat( hilbert3D ( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  116. tmp = tmp.concat( hilbert3D ( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  117. tmp = tmp.concat( hilbert3D ( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  118. tmp = tmp.concat( hilbert3D ( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  119. tmp = tmp.concat( hilbert3D ( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  120. tmp = tmp.concat( hilbert3D ( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) );
  121. return tmp;
  122. }
  123. return vec;
  124. }
  125. //
  126. function onDocumentMouseMove(event) {
  127. mouseX = event.clientX - windowHalfX;
  128. mouseY = event.clientY - windowHalfY;
  129. }
  130. function onDocumentTouchStart( 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 onDocumentTouchMove( event ) {
  138. if ( event.touches.length == 1 ) {
  139. event.preventDefault();
  140. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  141. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  142. }
  143. }
  144. //
  145. function loop() {
  146. camera.position.x += ( mouseX - camera.position.x ) * .05;
  147. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  148. camera.updateMatrix();
  149. var time = new Date().getTime() * 0.0015;
  150. for( var i = 0; i<scene.objects.length; i++ ) {
  151. scene.objects[i].rotation.y = time * ( i % 2 ? 1 : -1);
  152. }
  153. renderer.render(scene, camera);
  154. //stats.update();
  155. }
  156. </script>
  157. </body>
  158. </html>