webgl_interactive_cubes_gpu.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.generateMipmaps = false;
  62. scene.add( new THREE.AmbientLight( 0x555555 ) );
  63. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  64. light.position.set( 0, 500, 2000 );
  65. light.castShadow = true;
  66. light.shadowCameraNear = 200;
  67. light.shadowCameraFar = camera.far;
  68. light.shadowCameraFov = 50;
  69. light.shadowBias = -0.00022;
  70. light.shadowDarkness = 0.5;
  71. light.shadowMapWidth = 1024;
  72. light.shadowMapHeight = 1024;
  73. scene.add( light );
  74. var geometry = new THREE.Geometry(),
  75. pickingGeometry = new THREE.Geometry(),
  76. pickingMaterial = new THREE.MeshBasicMaterial({
  77. vertexColors: THREE.VertexColors
  78. }),
  79. defaultMaterial = new THREE.MeshLambertMaterial({
  80. color: 0xffffff,
  81. shading: THREE.FlatShading,
  82. vertexColors: THREE.VertexColors
  83. });
  84. function applyVertexColors(g, c) {
  85. g.faces.forEach(function(f){
  86. var n = (f instanceof THREE.Face3) ? 3 : 4;
  87. for(var j=0; j<n; j++){
  88. f.vertexColors[j] = c;
  89. }
  90. });
  91. }
  92. for ( var i = 0; i < 5000; i ++ ) {
  93. var position = new THREE.Vector3();
  94. position.x = Math.random() * 10000 - 5000;
  95. position.y = Math.random() * 6000 - 3000;
  96. position.z = Math.random() * 8000 - 4000;
  97. var rotation = new THREE.Vector3();
  98. rotation.x = ( Math.random() * 2 * Math.PI);
  99. rotation.y = ( Math.random() * 2 * Math.PI);
  100. rotation.z = ( Math.random() * 2 * Math.PI);
  101. var scale = new THREE.Vector3();
  102. scale.x = Math.random() * 200 + 100;
  103. scale.y = Math.random() * 200 + 100;
  104. scale.z = Math.random() * 200 + 100;
  105. //give the geom's vertices a random color, to be displayed
  106. var geom = new THREE.CubeGeometry(1, 1, 1);
  107. var color = new THREE.Color(Math.random() * 0xffffff);
  108. applyVertexColors(geom, color);
  109. var cube = new THREE.Mesh(geom);
  110. cube.position.copy(position);
  111. cube.rotation.copy(rotation);
  112. cube.scale.copy(scale);
  113. THREE.GeometryUtils.merge(geometry, cube);
  114. //give the pickingGeom's vertices a color corresponding to the "id"
  115. var pickingGeom = new THREE.CubeGeometry(1, 1, 1);
  116. var pickingColor = new THREE.Color(i);
  117. applyVertexColors(pickingGeom, pickingColor);
  118. var pickingCube = new THREE.Mesh(pickingGeom);
  119. pickingCube.position.copy(position);
  120. pickingCube.rotation.copy(rotation);
  121. pickingCube.scale.copy(scale);
  122. THREE.GeometryUtils.merge(pickingGeometry, pickingCube);
  123. pickingData[i] = {
  124. position: position,
  125. rotation: rotation,
  126. scale: scale
  127. };
  128. }
  129. var drawnObject = new THREE.Mesh(geometry, defaultMaterial);
  130. //drawnObject.castShadow = true;
  131. //drawnObject.receiveShadow = true;
  132. scene.add(drawnObject);
  133. pickingScene.add(new THREE.Mesh(pickingGeometry, pickingMaterial));
  134. highlightBox = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshLambertMaterial({color: 0xffff00}));
  135. scene.add(highlightBox);
  136. projector = new THREE.Projector();
  137. renderer = new THREE.WebGLRenderer( { antialias: true, clearColor: 0xffffff } );
  138. renderer.sortObjects = false;
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. renderer.shadowMapEnabled = true;
  141. renderer.shadowMapSoft = true;
  142. container.appendChild( renderer.domElement );
  143. stats = new Stats();
  144. stats.domElement.style.position = 'absolute';
  145. stats.domElement.style.top = '0px';
  146. container.appendChild( stats.domElement );
  147. renderer.domElement.addEventListener('mousemove', onMouseMove);
  148. }
  149. //
  150. function onMouseMove(e) {
  151. mouse.x = e.clientX;
  152. mouse.y = e.clientY;
  153. }
  154. function animate() {
  155. requestAnimationFrame( animate );
  156. render();
  157. stats.update();
  158. }
  159. function pick() {
  160. //render the picking scene off-screen
  161. var gl = self.renderer.getContext();
  162. renderer.render(pickingScene, camera, pickingTexture);
  163. var pixelBuffer = new Uint8Array(4);
  164. //read the pixel under the mouse from the texture
  165. gl.readPixels(mouse.x, pickingTexture.height - mouse.y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelBuffer);
  166. //interpret the pixel as an ID
  167. var id = (pixelBuffer[0] << 16) | (pixelBuffer[1] << 8) | (pixelBuffer[2]);
  168. var data = pickingData[id];
  169. if(data){
  170. //move our highlightBox so that it surrounds the picked object
  171. if(data.position && data.rotation && data.scale){
  172. highlightBox.position.copy(data.position);
  173. highlightBox.rotation.copy(data.rotation);
  174. highlightBox.scale.copy(data.scale).addSelf(offset);
  175. highlightBox.visible = true;
  176. }
  177. } else {
  178. highlightBox.visible = false;
  179. }
  180. }
  181. function render() {
  182. controls.update();
  183. pick();
  184. renderer.render( scene, camera );
  185. }
  186. </script>
  187. </body>
  188. </html>