webgl_interactive_lines.html 5.5 KB

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