webgl_interactive_lines.html 5.4 KB

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