webgl_interactive_cubes_gpu.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. .info {
  15. position: absolute;
  16. background-color: black;
  17. opacity: 0.8;
  18. color: white;
  19. text-align: center;
  20. top: 0px;
  21. width: 100%;
  22. }
  23. .info a {
  24. color: #00ffff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="info">
  30. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - gpu picking
  31. </div>
  32. <div id="container"></div>
  33. <script src="../build/three.js"></script>
  34. <script src="js/controls/TrackballControls.js"></script>
  35. <script src="js/utils/BufferGeometryUtils.js"></script>
  36. <script src="js/libs/stats.min.js"></script>
  37. <script>
  38. var container, stats;
  39. var camera, controls, scene, renderer;
  40. var pickingData = [], pickingTexture, pickingScene;
  41. var highlightBox;
  42. var mouse = new THREE.Vector2();
  43. var offset = new THREE.Vector3( 10, 10, 10 );
  44. init();
  45. animate();
  46. function init() {
  47. container = document.getElementById( "container" );
  48. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  49. camera.position.z = 1000;
  50. controls = new THREE.TrackballControls( camera );
  51. controls.rotateSpeed = 1.0;
  52. controls.zoomSpeed = 1.2;
  53. controls.panSpeed = 0.8;
  54. controls.noZoom = false;
  55. controls.noPan = false;
  56. controls.staticMoving = true;
  57. controls.dynamicDampingFactor = 0.3;
  58. scene = new THREE.Scene();
  59. scene.background = new THREE.Color( 0xffffff );
  60. pickingScene = new THREE.Scene();
  61. pickingTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
  62. pickingTexture.texture.minFilter = THREE.LinearFilter;
  63. scene.add( new THREE.AmbientLight( 0x555555 ) );
  64. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  65. light.position.set( 0, 500, 2000 );
  66. scene.add( light );
  67. var pickingMaterial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } );
  68. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors, shininess: 0 } );
  69. function applyVertexColors( geometry, color ) {
  70. var position = geometry.attributes.position;
  71. var colors = [];
  72. for ( var i = 0; i < position.count; i ++ ) {
  73. colors.push( color.r, color.g, color.b );
  74. }
  75. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  76. }
  77. var geometriesDrawn = [];
  78. var geometriesPicking = [];
  79. var matrix = new THREE.Matrix4();
  80. var quaternion = new THREE.Quaternion();
  81. var color = new THREE.Color();
  82. for ( var i = 0; i < 5000; i ++ ) {
  83. var geometry = new THREE.BoxBufferGeometry();
  84. var position = new THREE.Vector3();
  85. position.x = Math.random() * 10000 - 5000;
  86. position.y = Math.random() * 6000 - 3000;
  87. position.z = Math.random() * 8000 - 4000;
  88. var rotation = new THREE.Euler();
  89. rotation.x = Math.random() * 2 * Math.PI;
  90. rotation.y = Math.random() * 2 * Math.PI;
  91. rotation.z = Math.random() * 2 * Math.PI;
  92. var scale = new THREE.Vector3();
  93. scale.x = Math.random() * 200 + 100;
  94. scale.y = Math.random() * 200 + 100;
  95. scale.z = Math.random() * 200 + 100;
  96. quaternion.setFromEuler( rotation, false );
  97. matrix.compose( position, quaternion, scale );
  98. geometry.applyMatrix( matrix );
  99. // give the geometry's vertices a random color, to be displayed
  100. applyVertexColors( geometry, color.setHex( Math.random() * 0xffffff ) );
  101. geometriesDrawn.push( geometry );
  102. geometry = geometry.clone();
  103. // give the geometry's vertices a color corresponding to the "id"
  104. applyVertexColors( geometry, color.setHex( i ) );
  105. geometriesPicking.push( geometry );
  106. pickingData[ i ] = {
  107. position: position,
  108. rotation: rotation,
  109. scale: scale
  110. };
  111. }
  112. var objects = new THREE.Mesh( THREE.BufferGeometryUtils.mergeBufferGeometries( geometriesDrawn ), defaultMaterial );
  113. scene.add( objects );
  114. pickingScene.add( new THREE.Mesh( THREE.BufferGeometryUtils.mergeBufferGeometries( geometriesPicking ), pickingMaterial ) );
  115. highlightBox = new THREE.Mesh(
  116. new THREE.BoxBufferGeometry(),
  117. new THREE.MeshLambertMaterial( { color: 0xffff00 }
  118. ) );
  119. scene.add( highlightBox );
  120. renderer = new THREE.WebGLRenderer( { antialias: true } );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. container.appendChild( renderer.domElement );
  124. stats = new Stats();
  125. container.appendChild( stats.dom );
  126. renderer.domElement.addEventListener( 'mousemove', onMouseMove );
  127. }
  128. //
  129. function onMouseMove( e ) {
  130. mouse.x = e.clientX;
  131. mouse.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. renderer.render( pickingScene, camera, pickingTexture );
  141. //create buffer for reading single pixel
  142. var pixelBuffer = new Uint8Array( 4 );
  143. //read the pixel under the mouse from the texture
  144. renderer.readRenderTargetPixels( pickingTexture, mouse.x, pickingTexture.height - mouse.y, 1, 1, pixelBuffer );
  145. //interpret the pixel as an ID
  146. var id = ( pixelBuffer[ 0 ] << 16 ) | ( pixelBuffer[ 1 ] << 8 ) | ( pixelBuffer[ 2 ] );
  147. var data = pickingData[ id ];
  148. if ( data) {
  149. //move our highlightBox so that it surrounds the picked object
  150. if ( data.position && data.rotation && data.scale ){
  151. highlightBox.position.copy( data.position );
  152. highlightBox.rotation.copy( data.rotation );
  153. highlightBox.scale.copy( data.scale ).add( offset );
  154. highlightBox.visible = true;
  155. }
  156. } else {
  157. highlightBox.visible = false;
  158. }
  159. }
  160. function render() {
  161. controls.update();
  162. pick();
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>