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