webgl_interactive_raycasting_points.html 6.9 KB

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