webgl_interactive_lines.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <script type="module">
  20. import {
  21. BufferGeometry,
  22. Color,
  23. Float32BufferAttribute,
  24. Line,
  25. LineSegments,
  26. Math as _Math,
  27. Mesh,
  28. MeshBasicMaterial,
  29. Object3D,
  30. PerspectiveCamera,
  31. Raycaster,
  32. Scene,
  33. SphereBufferGeometry,
  34. Vector2,
  35. Vector3,
  36. WebGLRenderer
  37. } from "../build/three.module.js";
  38. import Stats from './jsm/libs/stats.module.js';
  39. var container, stats;
  40. var camera, scene, raycaster, renderer, parentTransform, sphereInter;
  41. var mouse = new Vector2();
  42. var radius = 100, theta = 0;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.createElement( 'div' );
  47. document.body.appendChild( container );
  48. var info = document.createElement( 'div' );
  49. info.style.position = 'absolute';
  50. info.style.top = '10px';
  51. info.style.width = '100%';
  52. info.style.textAlign = 'center';
  53. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive lines';
  54. container.appendChild( info );
  55. camera = new PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  56. scene = new Scene();
  57. scene.background = new Color( 0xf0f0f0 );
  58. var geometry = new SphereBufferGeometry( 5 );
  59. var material = new MeshBasicMaterial( { color: 0xff0000 } );
  60. sphereInter = new Mesh( geometry, material );
  61. sphereInter.visible = false;
  62. scene.add( sphereInter );
  63. var lineGeometry = new BufferGeometry();
  64. var points = [];
  65. var point = new Vector3();
  66. var direction = new Vector3();
  67. for ( var i = 0; i < 50; i ++ ) {
  68. direction.x += Math.random() - 0.5;
  69. direction.y += Math.random() - 0.5;
  70. direction.z += Math.random() - 0.5;
  71. direction.normalize().multiplyScalar( 10 );
  72. point.add( direction );
  73. points.push( point.x, point.y, point.z );
  74. }
  75. lineGeometry.addAttribute( 'position', new Float32BufferAttribute( points, 3 ) );
  76. parentTransform = new Object3D();
  77. parentTransform.position.x = Math.random() * 40 - 20;
  78. parentTransform.position.y = Math.random() * 40 - 20;
  79. parentTransform.position.z = Math.random() * 40 - 20;
  80. parentTransform.rotation.x = Math.random() * 2 * Math.PI;
  81. parentTransform.rotation.y = Math.random() * 2 * Math.PI;
  82. parentTransform.rotation.z = Math.random() * 2 * Math.PI;
  83. parentTransform.scale.x = Math.random() + 0.5;
  84. parentTransform.scale.y = Math.random() + 0.5;
  85. parentTransform.scale.z = Math.random() + 0.5;
  86. for ( var i = 0; i < 50; i ++ ) {
  87. var object;
  88. if ( Math.random() > 0.5 ) {
  89. object = new Line( lineGeometry );
  90. } else {
  91. object = new LineSegments( lineGeometry );
  92. }
  93. object.position.x = Math.random() * 400 - 200;
  94. object.position.y = Math.random() * 400 - 200;
  95. object.position.z = Math.random() * 400 - 200;
  96. object.rotation.x = Math.random() * 2 * Math.PI;
  97. object.rotation.y = Math.random() * 2 * Math.PI;
  98. object.rotation.z = Math.random() * 2 * Math.PI;
  99. object.scale.x = Math.random() + 0.5;
  100. object.scale.y = Math.random() + 0.5;
  101. object.scale.z = Math.random() + 0.5;
  102. parentTransform.add( object );
  103. }
  104. scene.add( parentTransform );
  105. raycaster = new Raycaster();
  106. raycaster.linePrecision = 3;
  107. renderer = new WebGLRenderer( { antialias: true } );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. container.appendChild( renderer.domElement );
  111. stats = new Stats();
  112. container.appendChild( stats.dom );
  113. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  114. //
  115. window.addEventListener( 'resize', onWindowResize, false );
  116. }
  117. function onWindowResize() {
  118. camera.aspect = window.innerWidth / window.innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. }
  122. function onDocumentMouseMove( event ) {
  123. event.preventDefault();
  124. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  125. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  126. }
  127. //
  128. function animate() {
  129. requestAnimationFrame( animate );
  130. render();
  131. stats.update();
  132. }
  133. function render() {
  134. theta += 0.1;
  135. camera.position.x = radius * Math.sin( _Math.degToRad( theta ) );
  136. camera.position.y = radius * Math.sin( _Math.degToRad( theta ) );
  137. camera.position.z = radius * Math.cos( _Math.degToRad( theta ) );
  138. camera.lookAt( scene.position );
  139. camera.updateMatrixWorld();
  140. // find intersections
  141. raycaster.setFromCamera( mouse, camera );
  142. var intersects = raycaster.intersectObjects( parentTransform.children, true );
  143. if ( intersects.length > 0 ) {
  144. sphereInter.visible = true;
  145. sphereInter.position.copy( intersects[ 0 ].point );
  146. } else {
  147. sphereInter.visible = false;
  148. }
  149. renderer.render( scene, camera );
  150. }
  151. </script>
  152. </body>
  153. </html>