lines_cubes_gl.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #oldie {
  26. font-family:monospace;
  27. font-size:13px;
  28. text-align:center;
  29. background:rgb(50,0,0);
  30. color:#fff;
  31. padding:1em;
  32. width:475px;
  33. margin:5em auto 0;
  34. display:none;
  35. }
  36. a {
  37. color: orange;
  38. text-decoration: none;
  39. }
  40. a:hover {
  41. color: #0080ff;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="info">
  47. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - lines WebGL demo
  48. [<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>]
  49. </div>
  50. <center>
  51. <div id="oldie">
  52. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>.<br/>
  53. <br/>
  54. Please try in
  55. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  56. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  57. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  58. </div>
  59. </center>
  60. <script type="text/javascript" src="../build/Three.js"></script>
  61. <script type="text/javascript" src="js/Stats.js"></script>
  62. <script type="text/javascript">
  63. if ( !is_browser_compatible() ) {
  64. document.getElementById( "oldie" ).style.display = "block";
  65. }
  66. var mouseX = 0, mouseY = 0,
  67. windowHalfX = window.innerWidth / 2,
  68. windowHalfY = window.innerHeight / 2,
  69. camera, scene, renderer,
  70. stats;
  71. init();
  72. setInterval( loop, 1000 / 60 );
  73. function init() {
  74. var i, material, container;
  75. container = document.createElement('div');
  76. document.body.appendChild(container);
  77. camera = new THREE.Camera( 33, window.innerWidth / window.innerHeight, 1, 10000 );
  78. camera.position.z = 700;
  79. scene = new THREE.Scene();
  80. renderer = new THREE.WebGLRenderer();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. var geometry = new THREE.Geometry(),
  84. points = hilbert3D( new THREE.Vector3( 0,0,0 ), 200.0, 4, 0, 1, 2, 3, 4, 5, 6, 7 );
  85. for ( i = 0; i < points.length; i ++ ) {
  86. geometry.vertices.push( new THREE.Vertex( points[i] ) );
  87. }
  88. // lines
  89. var line, p, scale = 0.3, d = 125, c1 = 0x553300, c2 = 0x555555, c3 = 0x992800, g1 = geometry,
  90. m1 = new THREE.LineBasicMaterial( { color: c1, opacity: 1, blending: THREE.AdditiveBlending } ),
  91. m2 = new THREE.LineBasicMaterial( { color: c2, opacity: 1, blending: THREE.AdditiveBlending } ),
  92. m3 = new THREE.LineBasicMaterial( { color: c3, opacity: 1, blending: THREE.AdditiveBlending } ),
  93. 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 ],
  94. [ m2, scale*0.5, [0,d,0], g1 ], [ m2, scale*0.5, [d, d, 0], g1 ], [ m2, scale*0.5, [-d, d, 0], g1 ],
  95. [ m2, scale*0.5, [0,-d,0], g1 ], [ m2, scale*0.5, [d, -d, 0], g1 ], [ m2, scale*0.5, [-d, -d, 0], g1 ],
  96. [ m1, scale*0.5, [2*d, 0, 0], g1 ], [ m1, scale*0.5, [-2*d, 0, 0], g1 ],
  97. [ m1, scale*0.5, [2*d, d, 0], g1 ], [ m1, scale*0.5, [-2*d, d, 0], g1 ],
  98. [ m1, scale*0.5, [2*d, -d, 0], g1 ], [ m1, scale*0.5, [-2*d, -d, 0], g1 ],
  99. ];
  100. for ( i = 0; i < parameters.length; ++i ) {
  101. p = parameters[ i ];
  102. line = new THREE.Line( p[ 3 ], p[ 0 ] );
  103. line.scale.x = line.scale.y = line.scale.z = p[ 1 ];
  104. line.position.x = p[ 2 ][ 0 ];
  105. line.position.y = p[ 2 ][ 1 ];
  106. line.position.z = p[ 2 ][ 2 ];
  107. scene.addObject( line );
  108. }
  109. /*
  110. stats = new Stats();
  111. stats.domElement.style.position = 'absolute';
  112. stats.domElement.style.top = '0px';
  113. container.appendChild(stats.domElement);
  114. */
  115. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  116. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  117. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  118. }
  119. // port of Processing Java code by Thomas Diewald
  120. // http://www.openprocessing.org/visuals/?visualID=15599
  121. function hilbert3D( center, side, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) {
  122. var half = side/2,
  123. vec_s = [
  124. new THREE.Vector3( center.x - half, center.y + half, center.z - half ),
  125. new THREE.Vector3( center.x - half, center.y + half, center.z + half ),
  126. new THREE.Vector3( center.x - half, center.y - half, center.z + half ),
  127. new THREE.Vector3( center.x - half, center.y - half, center.z - half ),
  128. new THREE.Vector3( center.x + half, center.y - half, center.z - half ),
  129. new THREE.Vector3( center.x + half, center.y - half, center.z + half ),
  130. new THREE.Vector3( center.x + half, center.y + half, center.z + half ),
  131. new THREE.Vector3( center.x + half, center.y + half, center.z - half )
  132. ],
  133. 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 ] ];
  134. if( --iterations >= 0 ) {
  135. var tmp = [];
  136. tmp = tmp.concat( hilbert3D ( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
  137. tmp = tmp.concat( hilbert3D ( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  138. tmp = tmp.concat( hilbert3D ( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  139. tmp = tmp.concat( hilbert3D ( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  140. tmp = tmp.concat( hilbert3D ( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  141. tmp = tmp.concat( hilbert3D ( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  142. tmp = tmp.concat( hilbert3D ( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  143. tmp = tmp.concat( hilbert3D ( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) );
  144. return tmp;
  145. }
  146. return vec;
  147. }
  148. //
  149. function onDocumentMouseMove(event) {
  150. mouseX = event.clientX - windowHalfX;
  151. mouseY = event.clientY - windowHalfY;
  152. }
  153. function onDocumentTouchStart( event ) {
  154. if ( event.touches.length > 1 ) {
  155. event.preventDefault();
  156. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  157. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  158. }
  159. }
  160. function onDocumentTouchMove( event ) {
  161. if ( event.touches.length == 1 ) {
  162. event.preventDefault();
  163. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  164. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  165. }
  166. }
  167. //
  168. function loop() {
  169. camera.position.x += ( mouseX - camera.position.x ) * .05;
  170. camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
  171. camera.updateMatrix();
  172. var time = new Date().getTime() * 0.0015;
  173. for( var i = 0; i<scene.objects.length; i++ ) {
  174. scene.objects[i].rotation.y = time * ( i % 2 ? 1 : -1);
  175. }
  176. renderer.render(scene, camera);
  177. //stats.update();
  178. }
  179. function is_browser_compatible() {
  180. // WebGL support
  181. try { var test = new Float32Array(1); } catch(e) { return false; }
  182. return true;
  183. }
  184. </script>
  185. </body>
  186. </html>