2
0

webgl_interactive_lines.html 5.6 KB

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