webgl_interactive_cubes_gpu.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive cubes (gpu)</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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - gpu picking
  21. </div>
  22. <div id="container"></div>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  38. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  39. let container, stats;
  40. let camera, controls, scene, renderer;
  41. let pickingTexture, pickingScene;
  42. let highlightBox;
  43. const pickingData = [];
  44. const pointer = new THREE.Vector2();
  45. const offset = new THREE.Vector3( 10, 10, 10 );
  46. init();
  47. animate();
  48. function init() {
  49. container = document.getElementById( 'container' );
  50. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  51. camera.position.z = 1000;
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0xffffff );
  54. pickingScene = new THREE.Scene();
  55. pickingTexture = new THREE.WebGLRenderTarget( 1, 1 );
  56. scene.add( new THREE.AmbientLight( 0x555555 ) );
  57. const light = new THREE.SpotLight( 0xffffff, 1.5 );
  58. light.position.set( 0, 500, 2000 );
  59. scene.add( light );
  60. const pickingMaterial = new THREE.MeshBasicMaterial( { vertexColors: true } );
  61. const defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: true, shininess: 0 } );
  62. function applyVertexColors( geometry, color ) {
  63. const position = geometry.attributes.position;
  64. const colors = [];
  65. for ( let i = 0; i < position.count; i ++ ) {
  66. colors.push( color.r, color.g, color.b );
  67. }
  68. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  69. }
  70. const geometriesDrawn = [];
  71. const geometriesPicking = [];
  72. const matrix = new THREE.Matrix4();
  73. const quaternion = new THREE.Quaternion();
  74. const color = new THREE.Color();
  75. for ( let i = 0; i < 5000; i ++ ) {
  76. let geometry = new THREE.BoxGeometry();
  77. const position = new THREE.Vector3();
  78. position.x = Math.random() * 10000 - 5000;
  79. position.y = Math.random() * 6000 - 3000;
  80. position.z = Math.random() * 8000 - 4000;
  81. const rotation = new THREE.Euler();
  82. rotation.x = Math.random() * 2 * Math.PI;
  83. rotation.y = Math.random() * 2 * Math.PI;
  84. rotation.z = Math.random() * 2 * Math.PI;
  85. const scale = new THREE.Vector3();
  86. scale.x = Math.random() * 200 + 100;
  87. scale.y = Math.random() * 200 + 100;
  88. scale.z = Math.random() * 200 + 100;
  89. quaternion.setFromEuler( rotation );
  90. matrix.compose( position, quaternion, scale );
  91. geometry.applyMatrix4( matrix );
  92. // give the geometry's vertices a random color, to be displayed
  93. applyVertexColors( geometry, color.setHex( Math.random() * 0xffffff ) );
  94. geometriesDrawn.push( geometry );
  95. geometry = geometry.clone();
  96. // give the geometry's vertices a color corresponding to the "id"
  97. applyVertexColors( geometry, color.setHex( i ) );
  98. geometriesPicking.push( geometry );
  99. pickingData[ i ] = {
  100. position: position,
  101. rotation: rotation,
  102. scale: scale
  103. };
  104. }
  105. const objects = new THREE.Mesh( BufferGeometryUtils.mergeBufferGeometries( geometriesDrawn ), defaultMaterial );
  106. scene.add( objects );
  107. pickingScene.add( new THREE.Mesh( BufferGeometryUtils.mergeBufferGeometries( geometriesPicking ), pickingMaterial ) );
  108. highlightBox = new THREE.Mesh(
  109. new THREE.BoxGeometry(),
  110. new THREE.MeshLambertMaterial( { color: 0xffff00 }
  111. ) );
  112. scene.add( highlightBox );
  113. renderer = new THREE.WebGLRenderer( { antialias: true } );
  114. renderer.setPixelRatio( window.devicePixelRatio );
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. container.appendChild( renderer.domElement );
  117. controls = new TrackballControls( camera, renderer.domElement );
  118. controls.rotateSpeed = 1.0;
  119. controls.zoomSpeed = 1.2;
  120. controls.panSpeed = 0.8;
  121. controls.noZoom = false;
  122. controls.noPan = false;
  123. controls.staticMoving = true;
  124. controls.dynamicDampingFactor = 0.3;
  125. stats = new Stats();
  126. container.appendChild( stats.dom );
  127. renderer.domElement.addEventListener( 'pointermove', onPointerMove );
  128. }
  129. //
  130. function onPointerMove( e ) {
  131. pointer.x = e.clientX;
  132. pointer.y = e.clientY;
  133. }
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function pick() {
  140. //render the picking scene off-screen
  141. // set the view offset to represent just a single pixel under the mouse
  142. camera.setViewOffset( renderer.domElement.width, renderer.domElement.height, pointer.x * window.devicePixelRatio | 0, pointer.y * window.devicePixelRatio | 0, 1, 1 );
  143. // render the scene
  144. renderer.setRenderTarget( pickingTexture );
  145. renderer.render( pickingScene, camera );
  146. // clear the view offset so rendering returns to normal
  147. camera.clearViewOffset();
  148. //create buffer for reading single pixel
  149. const pixelBuffer = new Uint8Array( 4 );
  150. //read the pixel
  151. renderer.readRenderTargetPixels( pickingTexture, 0, 0, 1, 1, pixelBuffer );
  152. //interpret the pixel as an ID
  153. const id = ( pixelBuffer[ 0 ] << 16 ) | ( pixelBuffer[ 1 ] << 8 ) | ( pixelBuffer[ 2 ] );
  154. const data = pickingData[ id ];
  155. if ( data ) {
  156. //move our highlightBox so that it surrounds the picked object
  157. if ( data.position && data.rotation && data.scale ) {
  158. highlightBox.position.copy( data.position );
  159. highlightBox.rotation.copy( data.rotation );
  160. highlightBox.scale.copy( data.scale ).add( offset );
  161. highlightBox.visible = true;
  162. }
  163. } else {
  164. highlightBox.visible = false;
  165. }
  166. }
  167. function render() {
  168. controls.update();
  169. pick();
  170. renderer.setRenderTarget( null );
  171. renderer.render( scene, camera );
  172. }
  173. </script>
  174. </body>
  175. </html>