webgl_interactive_raycasting_points.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive - raycasting - points </div>
  12. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. let renderer, scene, camera, stats;
  27. let pointclouds;
  28. let raycaster;
  29. let intersection = null;
  30. let spheresIndex = 0;
  31. let clock;
  32. let toggle = 0;
  33. const pointer = new THREE.Vector2();
  34. const spheres = [];
  35. const threshold = 0.1;
  36. const pointSize = 0.05;
  37. const width = 80;
  38. const length = 160;
  39. const rotateY = new THREE.Matrix4().makeRotationY( 0.005 );
  40. init();
  41. animate();
  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. clock = new THREE.Clock();
  110. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  111. camera.position.set( 10, 10, 10 );
  112. camera.lookAt( scene.position );
  113. camera.updateMatrix();
  114. //
  115. const pcBuffer = generatePointcloud( new THREE.Color( 1, 0, 0 ), width, length );
  116. pcBuffer.scale.set( 5, 10, 10 );
  117. pcBuffer.position.set( - 5, 0, 0 );
  118. scene.add( pcBuffer );
  119. const pcIndexed = generateIndexedPointcloud( new THREE.Color( 0, 1, 0 ), width, length );
  120. pcIndexed.scale.set( 5, 10, 10 );
  121. pcIndexed.position.set( 0, 0, 0 );
  122. scene.add( pcIndexed );
  123. const pcIndexedOffset = generateIndexedWithOffsetPointcloud( new THREE.Color( 0, 1, 1 ), width, length );
  124. pcIndexedOffset.scale.set( 5, 10, 10 );
  125. pcIndexedOffset.position.set( 5, 0, 0 );
  126. scene.add( pcIndexedOffset );
  127. pointclouds = [ pcBuffer, pcIndexed, pcIndexedOffset ];
  128. //
  129. const sphereGeometry = new THREE.SphereGeometry( 0.1, 32, 32 );
  130. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  131. for ( let i = 0; i < 40; i ++ ) {
  132. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  133. scene.add( sphere );
  134. spheres.push( sphere );
  135. }
  136. //
  137. renderer = new THREE.WebGLRenderer( { antialias: true } );
  138. renderer.setPixelRatio( window.devicePixelRatio );
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. container.appendChild( renderer.domElement );
  141. //
  142. raycaster = new THREE.Raycaster();
  143. raycaster.params.Points.threshold = threshold;
  144. //
  145. stats = new Stats();
  146. container.appendChild( stats.dom );
  147. //
  148. window.addEventListener( 'resize', onWindowResize );
  149. document.addEventListener( 'pointermove', onPointerMove );
  150. }
  151. function onPointerMove( event ) {
  152. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  153. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. renderer.setSize( window.innerWidth, window.innerHeight );
  159. }
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. camera.applyMatrix4( rotateY );
  167. camera.updateMatrixWorld();
  168. raycaster.setFromCamera( pointer, camera );
  169. const intersections = raycaster.intersectObjects( pointclouds, false );
  170. intersection = ( intersections.length ) > 0 ? intersections[ 0 ] : null;
  171. if ( toggle > 0.02 && intersection !== null ) {
  172. spheres[ spheresIndex ].position.copy( intersection.point );
  173. spheres[ spheresIndex ].scale.set( 1, 1, 1 );
  174. spheresIndex = ( spheresIndex + 1 ) % spheres.length;
  175. toggle = 0;
  176. }
  177. for ( let i = 0; i < spheres.length; i ++ ) {
  178. const sphere = spheres[ i ];
  179. sphere.scale.multiplyScalar( 0.98 );
  180. sphere.scale.clampScalar( 0.01, 1 );
  181. }
  182. toggle += clock.getDelta();
  183. renderer.render( scene, camera );
  184. }
  185. </script>
  186. </body>
  187. </html>