webgl_interactive_lines.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. geometry.vertices.push( new THREE.Vector3( 0, 30, -40 ) );
  52. geometry.vertices.push( new THREE.Vector3( -15, -15, -30 ) );
  53. parentTransform = new THREE.Mesh();
  54. parentTransform.position.x = Math.random() * 200 - 400;
  55. parentTransform.position.y = Math.random() * 200 - 400;
  56. parentTransform.position.z = Math.random() * 200 - 400;
  57. parentTransform.rotation.x = Math.random() * 2 * Math.PI;
  58. parentTransform.rotation.y = Math.random() * 2 * Math.PI;
  59. parentTransform.rotation.z = Math.random() * 2 * Math.PI;
  60. // parentTransform.scale.x = Math.random() + 0.5;
  61. // parentTransform.scale.y = Math.random() + 0.5;
  62. // parentTransform.scale.z = Math.random() + 0.5;
  63. for ( var i = 0; i < 100; i ++ ) {
  64. var type = Math.random() > 0.5 ? THREE.LineStrip : THREE.LinePieces;
  65. var object = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff, linewidth: 5 } ), type );
  66. object.position.x = Math.random() * 800 - 400;
  67. object.position.y = Math.random() * 800 - 400;
  68. object.position.z = Math.random() * 800 - 400;
  69. object.rotation.x = Math.random() * 2 * Math.PI;
  70. object.rotation.y = Math.random() * 2 * Math.PI;
  71. object.rotation.z = Math.random() * 2 * Math.PI;
  72. // object.scale.x = Math.random() + 0.5;
  73. // object.scale.y = Math.random() + 0.5;
  74. // object.scale.z = Math.random() + 0.5;
  75. parentTransform.add( object );
  76. }
  77. scene.add(parentTransform);
  78. projector = new THREE.Projector();
  79. raycaster = new THREE.Raycaster();
  80. raycaster.linePrecision = 5;
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.sortObjects = false;
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild(renderer.domElement);
  85. stats = new Stats();
  86. stats.domElement.style.position = 'absolute';
  87. stats.domElement.style.top = '0px';
  88. container.appendChild( stats.domElement );
  89. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  90. //
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function onDocumentMouseMove( event ) {
  99. event.preventDefault();
  100. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  101. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  102. }
  103. //
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. theta += 0.1;
  111. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  112. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  113. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  114. camera.lookAt( scene.position );
  115. // find intersections
  116. var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
  117. projector.unprojectVector( vector, camera );
  118. raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
  119. var intersects = raycaster.intersectObjects( parentTransform.children, true);
  120. if ( intersects.length > 0 ) {
  121. sphereInter.visible = true;
  122. sphereInter.position = intersects[0].point.clone();
  123. } else {
  124. sphereInter.visible = false;
  125. }
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>