webgl_interactive_cubes_gpu.html 6.5 KB

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