webgl_interactive_lines.html 5.3 KB

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