webgl_interactive_cubes_gpu.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. THREE.ColorManagement.enabled = true;
  40. let container, stats;
  41. let camera, controls, scene, renderer;
  42. let pickingTexture, pickingScene;
  43. let highlightBox;
  44. const pickingData = [];
  45. const pointer = new THREE.Vector2();
  46. const offset = new THREE.Vector3( 10, 10, 10 );
  47. init();
  48. animate();
  49. function init() {
  50. container = document.getElementById( 'container' );
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  52. camera.position.z = 1000;
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0xffffff );
  55. pickingScene = new THREE.Scene();
  56. pickingTexture = new THREE.WebGLRenderTarget( 1, 1 );
  57. scene.add( new THREE.AmbientLight( 0x555555 ) );
  58. const light = new THREE.SpotLight( 0xffffff, 1.5 );
  59. light.position.set( 0, 500, 2000 );
  60. scene.add( light );
  61. const pickingMaterial = new THREE.MeshBasicMaterial( { vertexColors: true } );
  62. const defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: true, shininess: 0 } );
  63. function applyVertexColors( geometry, color ) {
  64. const position = geometry.attributes.position;
  65. const colors = [];
  66. for ( let i = 0; i < position.count; i ++ ) {
  67. colors.push( color.r, color.g, color.b );
  68. }
  69. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  70. }
  71. const geometriesDrawn = [];
  72. const geometriesPicking = [];
  73. const matrix = new THREE.Matrix4();
  74. const quaternion = new THREE.Quaternion();
  75. const color = new THREE.Color();
  76. for ( let i = 0; i < 5000; i ++ ) {
  77. let geometry = new THREE.BoxGeometry();
  78. const position = new THREE.Vector3();
  79. position.x = Math.random() * 10000 - 5000;
  80. position.y = Math.random() * 6000 - 3000;
  81. position.z = Math.random() * 8000 - 4000;
  82. const rotation = new THREE.Euler();
  83. rotation.x = Math.random() * 2 * Math.PI;
  84. rotation.y = Math.random() * 2 * Math.PI;
  85. rotation.z = Math.random() * 2 * Math.PI;
  86. const scale = new THREE.Vector3();
  87. scale.x = Math.random() * 200 + 100;
  88. scale.y = Math.random() * 200 + 100;
  89. scale.z = Math.random() * 200 + 100;
  90. quaternion.setFromEuler( rotation );
  91. matrix.compose( position, quaternion, scale );
  92. geometry.applyMatrix4( matrix );
  93. // give the geometry's vertices a random color, to be displayed
  94. applyVertexColors( geometry, color.setHex( Math.random() * 0xffffff ) );
  95. geometriesDrawn.push( geometry );
  96. geometry = geometry.clone();
  97. // give the geometry's vertices a color corresponding to the "id"
  98. applyVertexColors( geometry, color.setHex( i ) );
  99. geometriesPicking.push( geometry );
  100. pickingData[ i ] = {
  101. position: position,
  102. rotation: rotation,
  103. scale: scale
  104. };
  105. }
  106. const objects = new THREE.Mesh( BufferGeometryUtils.mergeGeometries( geometriesDrawn ), defaultMaterial );
  107. scene.add( objects );
  108. pickingScene.add( new THREE.Mesh( BufferGeometryUtils.mergeGeometries( geometriesPicking ), pickingMaterial ) );
  109. highlightBox = new THREE.Mesh(
  110. new THREE.BoxGeometry(),
  111. new THREE.MeshLambertMaterial( { color: 0xffff00 }
  112. ) );
  113. scene.add( highlightBox );
  114. renderer = new THREE.WebGLRenderer( { antialias: true } );
  115. renderer.setPixelRatio( window.devicePixelRatio );
  116. renderer.setSize( window.innerWidth, window.innerHeight );
  117. container.appendChild( renderer.domElement );
  118. controls = new TrackballControls( camera, renderer.domElement );
  119. controls.rotateSpeed = 1.0;
  120. controls.zoomSpeed = 1.2;
  121. controls.panSpeed = 0.8;
  122. controls.noZoom = false;
  123. controls.noPan = false;
  124. controls.staticMoving = true;
  125. controls.dynamicDampingFactor = 0.3;
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. renderer.domElement.addEventListener( 'pointermove', onPointerMove );
  129. }
  130. //
  131. function onPointerMove( e ) {
  132. pointer.x = e.clientX;
  133. pointer.y = e.clientY;
  134. }
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. render();
  138. stats.update();
  139. }
  140. function pick() {
  141. //render the picking scene off-screen
  142. // set the view offset to represent just a single pixel under the mouse
  143. camera.setViewOffset( renderer.domElement.width, renderer.domElement.height, pointer.x * window.devicePixelRatio | 0, pointer.y * window.devicePixelRatio | 0, 1, 1 );
  144. // render the scene
  145. renderer.setRenderTarget( pickingTexture );
  146. renderer.render( pickingScene, camera );
  147. // clear the view offset so rendering returns to normal
  148. camera.clearViewOffset();
  149. //create buffer for reading single pixel
  150. const pixelBuffer = new Uint8Array( 4 );
  151. //read the pixel
  152. renderer.readRenderTargetPixels( pickingTexture, 0, 0, 1, 1, pixelBuffer );
  153. //interpret the pixel as an ID
  154. const id = ( pixelBuffer[ 0 ] << 16 ) | ( pixelBuffer[ 1 ] << 8 ) | ( pixelBuffer[ 2 ] );
  155. const data = pickingData[ id ];
  156. if ( data ) {
  157. //move our highlightBox so that it surrounds the picked object
  158. if ( data.position && data.rotation && data.scale ) {
  159. highlightBox.position.copy( data.position );
  160. highlightBox.rotation.copy( data.rotation );
  161. highlightBox.scale.copy( data.scale ).add( offset );
  162. highlightBox.visible = true;
  163. }
  164. } else {
  165. highlightBox.visible = false;
  166. }
  167. }
  168. function render() {
  169. controls.update();
  170. pick();
  171. renderer.setRenderTarget( null );
  172. renderer.render( scene, camera );
  173. }
  174. </script>
  175. </body>
  176. </html>