webgl_interactive_raycasting_points.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive - raycasting - points</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 - raycasting - points">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_interactive_raycasting_points.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_interactive_raycasting_points.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive - raycasting - points </div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. let renderer, scene, camera, stats;
  28. let pointclouds;
  29. let raycaster;
  30. let intersection = null;
  31. let spheresIndex = 0;
  32. let timer;
  33. let toggle = 0;
  34. const pointer = new THREE.Vector2();
  35. const spheres = [];
  36. const threshold = 0.1;
  37. const pointSize = 0.05;
  38. const width = 80;
  39. const length = 160;
  40. const rotateY = new THREE.Matrix4().makeRotationY( 0.005 );
  41. init();
  42. function generatePointCloudGeometry( color, width, length ) {
  43. const geometry = new THREE.BufferGeometry();
  44. const numPoints = width * length;
  45. const positions = new Float32Array( numPoints * 3 );
  46. const colors = new Float32Array( numPoints * 3 );
  47. let k = 0;
  48. for ( let i = 0; i < width; i ++ ) {
  49. for ( let j = 0; j < length; j ++ ) {
  50. const u = i / width;
  51. const v = j / length;
  52. const x = u - 0.5;
  53. const y = ( Math.cos( u * Math.PI * 4 ) + Math.sin( v * Math.PI * 8 ) ) / 20;
  54. const z = v - 0.5;
  55. positions[ 3 * k ] = x;
  56. positions[ 3 * k + 1 ] = y;
  57. positions[ 3 * k + 2 ] = z;
  58. const intensity = ( y + 0.1 ) * 5;
  59. colors[ 3 * k ] = color.r * intensity;
  60. colors[ 3 * k + 1 ] = color.g * intensity;
  61. colors[ 3 * k + 2 ] = color.b * intensity;
  62. k ++;
  63. }
  64. }
  65. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  66. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  67. geometry.computeBoundingBox();
  68. return geometry;
  69. }
  70. function generatePointcloud( color, width, length ) {
  71. const geometry = generatePointCloudGeometry( color, width, length );
  72. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  73. return new THREE.Points( geometry, material );
  74. }
  75. function generateIndexedPointcloud( color, width, length ) {
  76. const geometry = generatePointCloudGeometry( color, width, length );
  77. const numPoints = width * length;
  78. const indices = new Uint16Array( numPoints );
  79. let k = 0;
  80. for ( let i = 0; i < width; i ++ ) {
  81. for ( let j = 0; j < length; j ++ ) {
  82. indices[ k ] = k;
  83. k ++;
  84. }
  85. }
  86. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  87. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  88. return new THREE.Points( geometry, material );
  89. }
  90. function generateIndexedWithOffsetPointcloud( color, width, length ) {
  91. const geometry = generatePointCloudGeometry( color, width, length );
  92. const numPoints = width * length;
  93. const indices = new Uint16Array( numPoints );
  94. let k = 0;
  95. for ( let i = 0; i < width; i ++ ) {
  96. for ( let j = 0; j < length; j ++ ) {
  97. indices[ k ] = k;
  98. k ++;
  99. }
  100. }
  101. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  102. geometry.addGroup( 0, indices.length );
  103. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  104. return new THREE.Points( geometry, material );
  105. }
  106. function init() {
  107. const container = document.getElementById( 'container' );
  108. scene = new THREE.Scene();
  109. timer = new THREE.Timer();
  110. timer.connect( document );
  111. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  112. camera.position.set( 10, 10, 10 );
  113. camera.lookAt( scene.position );
  114. camera.updateMatrix();
  115. //
  116. const pcBuffer = generatePointcloud( new THREE.Color( 1, 0, 0 ), width, length );
  117. pcBuffer.scale.set( 5, 10, 10 );
  118. pcBuffer.position.set( - 5, 0, 0 );
  119. scene.add( pcBuffer );
  120. const pcIndexed = generateIndexedPointcloud( new THREE.Color( 0, 1, 0 ), width, length );
  121. pcIndexed.scale.set( 5, 10, 10 );
  122. pcIndexed.position.set( 0, 0, 0 );
  123. scene.add( pcIndexed );
  124. const pcIndexedOffset = generateIndexedWithOffsetPointcloud( new THREE.Color( 0, 1, 1 ), width, length );
  125. pcIndexedOffset.scale.set( 5, 10, 10 );
  126. pcIndexedOffset.position.set( 5, 0, 0 );
  127. scene.add( pcIndexedOffset );
  128. pointclouds = [ pcBuffer, pcIndexed, pcIndexedOffset ];
  129. //
  130. const sphereGeometry = new THREE.SphereGeometry( 0.1, 32, 32 );
  131. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  132. for ( let i = 0; i < 40; i ++ ) {
  133. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  134. scene.add( sphere );
  135. spheres.push( sphere );
  136. }
  137. //
  138. renderer = new THREE.WebGLRenderer( { antialias: true } );
  139. renderer.setPixelRatio( window.devicePixelRatio );
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. renderer.setAnimationLoop( animate );
  142. container.appendChild( renderer.domElement );
  143. //
  144. raycaster = new THREE.Raycaster();
  145. raycaster.params.Points.threshold = threshold;
  146. //
  147. stats = new Stats();
  148. container.appendChild( stats.dom );
  149. //
  150. window.addEventListener( 'resize', onWindowResize );
  151. document.addEventListener( 'pointermove', onPointerMove );
  152. }
  153. function onPointerMove( event ) {
  154. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  155. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. function animate() {
  163. timer.update();
  164. render();
  165. stats.update();
  166. }
  167. function render() {
  168. camera.applyMatrix4( rotateY );
  169. camera.updateMatrixWorld();
  170. raycaster.setFromCamera( pointer, camera );
  171. const intersections = raycaster.intersectObjects( pointclouds, false );
  172. intersection = ( intersections.length ) > 0 ? intersections[ 0 ] : null;
  173. if ( toggle > 0.02 && intersection !== null ) {
  174. spheres[ spheresIndex ].position.copy( intersection.point );
  175. spheres[ spheresIndex ].scale.set( 1, 1, 1 );
  176. spheresIndex = ( spheresIndex + 1 ) % spheres.length;
  177. toggle = 0;
  178. }
  179. for ( let i = 0; i < spheres.length; i ++ ) {
  180. const sphere = spheres[ i ];
  181. sphere.scale.multiplyScalar( 0.98 );
  182. sphere.scale.clampScalar( 0.01, 1 );
  183. }
  184. toggle += timer.getDelta();
  185. renderer.render( scene, camera );
  186. }
  187. </script>
  188. </body>
  189. </html>
粤ICP备19079148号