webgl_interactive_raycasting_points.html 7.0 KB

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