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