webgl_interactive_raycasting_points.html 6.8 KB

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