2
0

webgl_interactive_raycasting_points.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. animate();
  39. function generatePointCloudGeometry( color, width, length ) {
  40. const geometry = new THREE.BufferGeometry();
  41. const numPoints = width * length;
  42. const positions = new Float32Array( numPoints * 3 );
  43. const colors = new Float32Array( numPoints * 3 );
  44. let k = 0;
  45. for ( let i = 0; i < width; i ++ ) {
  46. for ( let j = 0; j < length; j ++ ) {
  47. const u = i / width;
  48. const v = j / length;
  49. const x = u - 0.5;
  50. const y = ( Math.cos( u * Math.PI * 4 ) + Math.sin( v * Math.PI * 8 ) ) / 20;
  51. const z = v - 0.5;
  52. positions[ 3 * k ] = x;
  53. positions[ 3 * k + 1 ] = y;
  54. positions[ 3 * k + 2 ] = z;
  55. const intensity = ( y + 0.1 ) * 5;
  56. colors[ 3 * k ] = color.r * intensity;
  57. colors[ 3 * k + 1 ] = color.g * intensity;
  58. colors[ 3 * k + 2 ] = color.b * intensity;
  59. k ++;
  60. }
  61. }
  62. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  63. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  64. geometry.computeBoundingBox();
  65. return geometry;
  66. }
  67. function generatePointcloud( color, width, length ) {
  68. const geometry = generatePointCloudGeometry( color, width, length );
  69. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  70. return new THREE.Points( geometry, material );
  71. }
  72. function generateIndexedPointcloud( color, width, length ) {
  73. const geometry = generatePointCloudGeometry( color, width, length );
  74. const numPoints = width * length;
  75. const indices = new Uint16Array( numPoints );
  76. let k = 0;
  77. for ( let i = 0; i < width; i ++ ) {
  78. for ( let j = 0; j < length; j ++ ) {
  79. indices[ k ] = k;
  80. k ++;
  81. }
  82. }
  83. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  84. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  85. return new THREE.Points( geometry, material );
  86. }
  87. function generateIndexedWithOffsetPointcloud( color, width, length ) {
  88. const geometry = generatePointCloudGeometry( color, width, length );
  89. const numPoints = width * length;
  90. const indices = new Uint16Array( numPoints );
  91. let k = 0;
  92. for ( let i = 0; i < width; i ++ ) {
  93. for ( let j = 0; j < length; j ++ ) {
  94. indices[ k ] = k;
  95. k ++;
  96. }
  97. }
  98. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  99. geometry.addGroup( 0, indices.length );
  100. const material = new THREE.PointsMaterial( { size: pointSize, vertexColors: true } );
  101. return new THREE.Points( geometry, material );
  102. }
  103. function init() {
  104. const container = document.getElementById( 'container' );
  105. scene = new THREE.Scene();
  106. clock = new THREE.Clock();
  107. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  108. camera.position.set( 10, 10, 10 );
  109. camera.lookAt( scene.position );
  110. camera.updateMatrix();
  111. //
  112. const pcBuffer = generatePointcloud( new THREE.Color( 1, 0, 0 ), width, length );
  113. pcBuffer.scale.set( 5, 10, 10 );
  114. pcBuffer.position.set( - 5, 0, 0 );
  115. scene.add( pcBuffer );
  116. const pcIndexed = generateIndexedPointcloud( new THREE.Color( 0, 1, 0 ), width, length );
  117. pcIndexed.scale.set( 5, 10, 10 );
  118. pcIndexed.position.set( 0, 0, 0 );
  119. scene.add( pcIndexed );
  120. const pcIndexedOffset = generateIndexedWithOffsetPointcloud( new THREE.Color( 0, 1, 1 ), width, length );
  121. pcIndexedOffset.scale.set( 5, 10, 10 );
  122. pcIndexedOffset.position.set( 5, 0, 0 );
  123. scene.add( pcIndexedOffset );
  124. pointclouds = [ pcBuffer, pcIndexed, pcIndexedOffset ];
  125. //
  126. const sphereGeometry = new THREE.SphereGeometry( 0.1, 32, 32 );
  127. const sphereMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000 } );
  128. for ( let i = 0; i < 40; i ++ ) {
  129. const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
  130. scene.add( sphere );
  131. spheres.push( sphere );
  132. }
  133. //
  134. renderer = new THREE.WebGLRenderer( { antialias: true } );
  135. renderer.setPixelRatio( window.devicePixelRatio );
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  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. requestAnimationFrame( animate );
  159. render();
  160. stats.update();
  161. }
  162. function render() {
  163. camera.applyMatrix4( rotateY );
  164. camera.updateMatrixWorld();
  165. raycaster.setFromCamera( pointer, camera );
  166. const intersections = raycaster.intersectObjects( pointclouds, false );
  167. intersection = ( intersections.length ) > 0 ? intersections[ 0 ] : null;
  168. if ( toggle > 0.02 && intersection !== null ) {
  169. spheres[ spheresIndex ].position.copy( intersection.point );
  170. spheres[ spheresIndex ].scale.set( 1, 1, 1 );
  171. spheresIndex = ( spheresIndex + 1 ) % spheres.length;
  172. toggle = 0;
  173. }
  174. for ( let i = 0; i < spheres.length; i ++ ) {
  175. const sphere = spheres[ i ];
  176. sphere.scale.multiplyScalar( 0.98 );
  177. sphere.scale.clampScalar( 0.01, 1 );
  178. }
  179. toggle += clock.getDelta();
  180. renderer.render( scene, camera );
  181. }
  182. </script>
  183. </body>
  184. </html>