webgl_interactive_cubes_gpu.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.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.texture.minFilter = THREE.LinearFilter;
  62. scene.add( new THREE.AmbientLight( 0x555555 ) );
  63. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  64. light.position.set( 0, 500, 2000 );
  65. scene.add( light );
  66. var geometry = new THREE.Geometry(),
  67. pickingGeometry = new THREE.Geometry(),
  68. pickingMaterial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } ),
  69. defaultMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors, shininess: 0 } );
  70. function applyVertexColors( g, c ) {
  71. g.faces.forEach( function( f ) {
  72. var n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  73. for( var j = 0; j < n; j ++ ) {
  74. f.vertexColors[ j ] = c;
  75. }
  76. } );
  77. }
  78. var geom = new THREE.BoxGeometry( 1, 1, 1 );
  79. var color = new THREE.Color();
  80. var matrix = new THREE.Matrix4();
  81. var quaternion = new THREE.Quaternion();
  82. for ( var i = 0; i < 5000; i ++ ) {
  83. var position = new THREE.Vector3();
  84. position.x = Math.random() * 10000 - 5000;
  85. position.y = Math.random() * 6000 - 3000;
  86. position.z = Math.random() * 8000 - 4000;
  87. var rotation = new THREE.Euler();
  88. rotation.x = Math.random() * 2 * Math.PI;
  89. rotation.y = Math.random() * 2 * Math.PI;
  90. rotation.z = Math.random() * 2 * Math.PI;
  91. var scale = new THREE.Vector3();
  92. scale.x = Math.random() * 200 + 100;
  93. scale.y = Math.random() * 200 + 100;
  94. scale.z = Math.random() * 200 + 100;
  95. quaternion.setFromEuler( rotation, false );
  96. matrix.compose( position, quaternion, scale );
  97. // give the geom's vertices a random color, to be displayed
  98. applyVertexColors( geom, color.setHex( Math.random() * 0xffffff ) );
  99. geometry.merge( geom, matrix );
  100. // give the geom's vertices a color corresponding to the "id"
  101. applyVertexColors( geom, color.setHex( i ) );
  102. pickingGeometry.merge( geom, matrix );
  103. pickingData[ i ] = {
  104. position: position,
  105. rotation: rotation,
  106. scale: scale
  107. };
  108. }
  109. var drawnObject = new THREE.Mesh( geometry, defaultMaterial );
  110. scene.add( drawnObject );
  111. pickingScene.add( new THREE.Mesh( pickingGeometry, pickingMaterial ) );
  112. highlightBox = new THREE.Mesh(
  113. new THREE.BoxGeometry( 1, 1, 1 ),
  114. new THREE.MeshLambertMaterial( { color: 0xffff00 }
  115. ) );
  116. scene.add( highlightBox );
  117. renderer = new THREE.WebGLRenderer( { antialias: true } );
  118. renderer.setClearColor( 0xffffff );
  119. renderer.setPixelRatio( window.devicePixelRatio );
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. renderer.sortObjects = false;
  122. container.appendChild( renderer.domElement );
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. renderer.domElement.addEventListener( 'mousemove', onMouseMove );
  126. }
  127. //
  128. function onMouseMove( e ) {
  129. mouse.x = e.clientX;
  130. mouse.y = e.clientY;
  131. }
  132. function animate() {
  133. requestAnimationFrame( animate );
  134. render();
  135. stats.update();
  136. }
  137. function pick() {
  138. //render the picking scene off-screen
  139. renderer.render( pickingScene, camera, pickingTexture );
  140. //create buffer for reading single pixel
  141. var pixelBuffer = new Uint8Array( 4 );
  142. //read the pixel under the mouse from the texture
  143. renderer.readRenderTargetPixels(pickingTexture, mouse.x, pickingTexture.height - mouse.y, 1, 1, pixelBuffer);
  144. //interpret the pixel as an ID
  145. var id = ( pixelBuffer[0] << 16 ) | ( pixelBuffer[1] << 8 ) | ( pixelBuffer[2] );
  146. var data = pickingData[ id ];
  147. if ( data) {
  148. //move our highlightBox so that it surrounds the picked object
  149. if ( data.position && data.rotation && data.scale ){
  150. highlightBox.position.copy( data.position );
  151. highlightBox.rotation.copy( data.rotation );
  152. highlightBox.scale.copy( data.scale ).add( offset );
  153. highlightBox.visible = true;
  154. }
  155. } else {
  156. highlightBox.visible = false;
  157. }
  158. }
  159. function render() {
  160. controls.update();
  161. pick();
  162. renderer.render( scene, camera );
  163. }
  164. </script>
  165. </body>
  166. </html>