webgl_interactive_lines.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive lines</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.js"></script>
  18. <script src="js/libs/stats.min.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, projector, raycaster, renderer, parentTransform, sphereInter;
  22. var mouse = new THREE.Vector2();
  23. var radius = 100, theta = 0;
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  35. container.appendChild( info );
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. scene = new THREE.Scene();
  38. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  39. light.position.set( 1, 1, 1 ).normalize();
  40. scene.add( light );
  41. var light = new THREE.DirectionalLight( 0xffffff );
  42. light.position.set( -1, -1, -1 ).normalize();
  43. scene.add( light );
  44. var sphereGeometry = new THREE.SphereGeometry(3);
  45. sphereInter = new THREE.Mesh( sphereGeometry, new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
  46. sphereInter.visible = false;
  47. scene.add( sphereInter );
  48. var geometry = new THREE.Geometry();
  49. geometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
  50. geometry.vertices.push( new THREE.Vector3( 15, 15, 15 ) );
  51. parentTransform = new THREE.Mesh();
  52. parentTransform.position.x = Math.random() * 800 - 400;
  53. parentTransform.position.y = Math.random() * 800 - 400;
  54. parentTransform.position.z = Math.random() * 800 - 400;
  55. parentTransform.rotation.x = Math.random() * 2 * Math.PI;
  56. parentTransform.rotation.y = Math.random() * 2 * Math.PI;
  57. parentTransform.rotation.z = Math.random() * 2 * Math.PI;
  58. // parentTransform.scale.x = Math.random() + 0.5;
  59. // parentTransform.scale.y = Math.random() + 0.5;
  60. // parentTransform.scale.z = Math.random() + 0.5;
  61. for ( var i = 0; i < 1000; i ++ ) {
  62. var object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff, linewidth: 2 } ) );
  63. object.position.x = Math.random() * 800 - 400;
  64. object.position.y = Math.random() * 800 - 400;
  65. object.position.z = Math.random() * 800 - 400;
  66. object.rotation.x = Math.random() * 2 * Math.PI;
  67. object.rotation.y = Math.random() * 2 * Math.PI;
  68. object.rotation.z = Math.random() * 2 * Math.PI;
  69. // object.scale.x = Math.random() + 0.5;
  70. // object.scale.y = Math.random() + 0.5;
  71. // object.scale.z = Math.random() + 0.5;
  72. parentTransform.add( object );
  73. }
  74. scene.add(parentTransform);
  75. projector = new THREE.Projector();
  76. raycaster = new THREE.Raycaster();
  77. raycaster.linePrecision = 5;
  78. renderer = new THREE.WebGLRenderer();
  79. renderer.sortObjects = false;
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. container.appendChild(renderer.domElement);
  82. stats = new Stats();
  83. stats.domElement.style.position = 'absolute';
  84. stats.domElement.style.top = '0px';
  85. container.appendChild( stats.domElement );
  86. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  87. //
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. }
  90. function onWindowResize() {
  91. camera.aspect = window.innerWidth / window.innerHeight;
  92. camera.updateProjectionMatrix();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function onDocumentMouseMove( event ) {
  96. event.preventDefault();
  97. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  98. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  99. }
  100. //
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. render();
  104. stats.update();
  105. }
  106. function render() {
  107. theta += 0.1;
  108. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  109. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  110. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  111. camera.lookAt( scene.position );
  112. // find intersections
  113. var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
  114. projector.unprojectVector( vector, camera );
  115. raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
  116. var intersects = raycaster.intersectObjects( parentTransform.children, true);
  117. if ( intersects.length > 0 ) {
  118. sphereInter.visible = true;
  119. sphereInter.position = (intersects[0].point).clone();
  120. } else {
  121. sphereInter.visible = false;
  122. }
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>