webgl_interactive_cubes_gpu.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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">three.js</a> webgl - gpu picking
  31. </div>
  32. <div id="container"></div>
  33. <script src="../build/three.min.js"></script>
  34. <script src="js/controls/TrackballControls.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. var container, stats;
  38. var camera, controls, scene, renderer;
  39. var pickingData = [], pickingTexture, pickingScene;
  40. var objects = [];
  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. pickingScene = new THREE.Scene();
  60. pickingTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight );
  61. pickingTexture.minFilter = THREE.LinearFilter;
  62. pickingTexture.generateMipmaps = false;
  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 geometry = new THREE.Geometry(),
  68. pickingGeometry = new THREE.Geometry(),
  69. pickingMaterial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } ),
  70. defaultMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } );
  71. function applyVertexColors( g, c ) {
  72. g.faces.forEach( function( f ) {
  73. var n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  74. for( var j = 0; j < n; j ++ ) {
  75. f.vertexColors[ j ] = c;
  76. }
  77. } );
  78. }
  79. var geom = new THREE.BoxGeometry( 1, 1, 1 );
  80. var color = new THREE.Color();
  81. var matrix = new THREE.Matrix4();
  82. var quaternion = new THREE.Quaternion();
  83. for ( var i = 0; i < 5000; i ++ ) {
  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. // give the geom's vertices a random color, to be displayed
  99. applyVertexColors( geom, color.setHex( Math.random() * 0xffffff ) );
  100. geometry.merge( geom, matrix );
  101. // give the geom's vertices a color corresponding to the "id"
  102. applyVertexColors( geom, color.setHex( i ) );
  103. pickingGeometry.merge( geom, matrix );
  104. pickingData[ i ] = {
  105. position: position,
  106. rotation: rotation,
  107. scale: scale
  108. };
  109. }
  110. var drawnObject = new THREE.Mesh( geometry, defaultMaterial );
  111. scene.add( drawnObject );
  112. pickingScene.add( new THREE.Mesh( pickingGeometry, pickingMaterial ) );
  113. highlightBox = new THREE.Mesh(
  114. new THREE.BoxGeometry( 1, 1, 1 ),
  115. new THREE.MeshLambertMaterial( { color: 0xffff00 }
  116. ) );
  117. scene.add( highlightBox );
  118. renderer = new THREE.WebGLRenderer( { antialias: true } );
  119. renderer.setClearColor( 0xffffff );
  120. renderer.setPixelRatio( window.devicePixelRatio );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. renderer.sortObjects = false;
  123. container.appendChild( renderer.domElement );
  124. stats = new Stats();
  125. stats.domElement.style.position = 'absolute';
  126. stats.domElement.style.top = '0px';
  127. container.appendChild( stats.domElement );
  128. renderer.domElement.addEventListener( 'mousemove', onMouseMove );
  129. }
  130. //
  131. function onMouseMove( e ) {
  132. mouse.x = e.clientX;
  133. mouse.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. renderer.render( pickingScene, camera, pickingTexture );
  143. //create buffer for reading single pixel
  144. var pixelBuffer = new Uint8Array( 4 );
  145. //read the pixel under the mouse from the texture
  146. renderer.readRenderTargetPixels(pickingTexture, mouse.x, pickingTexture.height - mouse.y, 1, 1, pixelBuffer);
  147. //interpret the pixel as an ID
  148. var id = ( pixelBuffer[0] << 16 ) | ( pixelBuffer[1] << 8 ) | ( pixelBuffer[2] );
  149. var data = pickingData[ id ];
  150. if ( data) {
  151. //move our highlightBox so that it surrounds the picked object
  152. if ( data.position && data.rotation && data.scale ){
  153. highlightBox.position.copy( data.position );
  154. highlightBox.rotation.copy( data.rotation );
  155. highlightBox.scale.copy( data.scale ).add( offset );
  156. highlightBox.visible = true;
  157. }
  158. } else {
  159. highlightBox.visible = false;
  160. }
  161. }
  162. function render() {
  163. controls.update();
  164. pick();
  165. renderer.render( scene, camera );
  166. }
  167. </script>
  168. </body>
  169. </html>