2
0

webgl_interactive_lines.html 5.3 KB

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