1
0

webgl_interactive_cubes_gpu.html 6.3 KB

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