2
0

webgl_interactive_cubes_gpu.html 6.6 KB

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