webgl_kinect.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>three.js - kinect</title>
  6. <meta charset="utf-8">
  7. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  8. <style>
  9. body {
  10. font-family: Monospace;
  11. background-color: #000000;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #ffffff;
  17. font-family: Monospace;
  18. font-size: 13px;
  19. text-align: center;
  20. font-weight: bold;
  21. position: absolute;
  22. top: 0px; width: 100%;
  23. padding: 5px;
  24. }
  25. a {
  26. color: #0040ff;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <script src="../build/three.min.js"></script>
  32. <script src='js/libs/dat.gui.min.js'></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script id="vs" type="x-shader/x-vertex">
  36. uniform sampler2D map;
  37. uniform float width;
  38. uniform float height;
  39. uniform float nearClipping, farClipping;
  40. uniform float pointSize;
  41. uniform float zOffset;
  42. varying vec2 vUv;
  43. const float XtoZ = 1.11146; // tan( 1.0144686 / 2.0 ) * 2.0;
  44. const float YtoZ = 0.83359; // tan( 0.7898090 / 2.0 ) * 2.0;
  45. void main() {
  46. vUv = vec2( position.x / width, position.y / height );
  47. vec4 color = texture2D( map, vUv );
  48. float depth = ( color.r + color.g + color.b ) / 3.0;
  49. // Projection code by @kcmic
  50. float z = ( 1.0 - depth ) * (farClipping - nearClipping) + nearClipping;
  51. vec4 pos = vec4(
  52. ( position.x / width - 0.5 ) * z * XtoZ,
  53. ( position.y / height - 0.5 ) * z * YtoZ,
  54. - z + zOffset,
  55. 1.0);
  56. gl_PointSize = pointSize;
  57. gl_Position = projectionMatrix * modelViewMatrix * pos;
  58. }
  59. </script>
  60. <script id="fs" type="x-shader/x-fragment">
  61. uniform sampler2D map;
  62. varying vec2 vUv;
  63. void main() {
  64. vec4 color = texture2D( map, vUv );
  65. gl_FragColor = vec4( color.r, color.g, color.b, 0.2 );
  66. }
  67. </script>
  68. <script>
  69. var container;
  70. var scene, camera, light, renderer;
  71. var geometry, cube, mesh, material;
  72. var mouse, center;
  73. var stats;
  74. var video, texture;
  75. if ( Detector.webgl ) {
  76. init();
  77. animate();
  78. } else {
  79. document.body.appendChild( Detector.getWebGLErrorMessage() );
  80. }
  81. function init() {
  82. container = document.createElement( 'div' );
  83. document.body.appendChild( container );
  84. var info = document.createElement( 'div' );
  85. info.id = 'info';
  86. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> - kinect';
  87. document.body.appendChild( info );
  88. stats = new Stats();
  89. stats.domElement.style.position = 'absolute';
  90. stats.domElement.style.top = '0px';
  91. // container.appendChild( stats.domElement );
  92. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  93. camera.position.set( 0, 0, 500 );
  94. scene = new THREE.Scene();
  95. center = new THREE.Vector3();
  96. center.z = - 1000;
  97. video = document.createElement( 'video' );
  98. video.addEventListener( 'loadedmetadata', function ( event ) {
  99. texture = new THREE.Texture( video );
  100. texture.generateMipmaps = false;
  101. var width = 640, height = 480;
  102. var nearClipping = 850, farClipping = 4000;
  103. geometry = new THREE.BufferGeometry();
  104. var vertices = new Float32Array( width * height * 3 );
  105. for ( var i = 0, j = 0, l = vertices.length; i < l; i += 3, j ++ ) {
  106. vertices[ i ] = j % width;
  107. vertices[ i + 1 ] = Math.floor( j / width );
  108. }
  109. geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  110. material = new THREE.ShaderMaterial( {
  111. uniforms: {
  112. "map": { type: "t", value: texture },
  113. "width": { type: "f", value: width },
  114. "height": { type: "f", value: height },
  115. "nearClipping": { type: "f", value: nearClipping },
  116. "farClipping": { type: "f", value: farClipping },
  117. "pointSize": { type: "f", value: 2 },
  118. "zOffset": { type: "f", value: 1000 }
  119. },
  120. vertexShader: document.getElementById( 'vs' ).textContent,
  121. fragmentShader: document.getElementById( 'fs' ).textContent,
  122. blending: THREE.AdditiveBlending,
  123. depthTest: false, depthWrite: false,
  124. transparent: true
  125. } );
  126. mesh = new THREE.PointCloud( geometry, material );
  127. scene.add( mesh );
  128. setInterval( function () {
  129. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  130. texture.needsUpdate = true;
  131. }
  132. }, 1000 / 30 );
  133. var gui = new dat.GUI();
  134. gui.add( material.uniforms.nearClipping, 'value', 1, 10000, 1.0 ).name( 'nearClipping' );
  135. gui.add( material.uniforms.farClipping, 'value', 1, 10000, 1.0 ).name( 'farClipping' );
  136. gui.add( material.uniforms.pointSize, 'value', 1, 10, 1.0 ).name( 'pointSize' );
  137. gui.add( material.uniforms.zOffset, 'value', 0, 4000, 1.0 ).name( 'zOffset' );
  138. gui.close();
  139. }, false );
  140. video.loop = true;
  141. video.src = 'textures/kinect.webm';
  142. video.play();
  143. renderer = new THREE.WebGLRenderer();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. container.appendChild( renderer.domElement );
  146. mouse = new THREE.Vector3( 0, 0, 1 );
  147. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  148. //
  149. window.addEventListener( 'resize', onWindowResize, false );
  150. }
  151. function onWindowResize() {
  152. camera.aspect = window.innerWidth / window.innerHeight;
  153. camera.updateProjectionMatrix();
  154. renderer.setSize( window.innerWidth, window.innerHeight );
  155. }
  156. function onDocumentMouseMove( event ) {
  157. mouse.x = ( event.clientX - window.innerWidth / 2 ) * 8;
  158. mouse.y = ( event.clientY - window.innerHeight / 2 ) * 8;
  159. }
  160. function animate() {
  161. requestAnimationFrame( animate );
  162. render();
  163. stats.update();
  164. }
  165. function render() {
  166. camera.position.x += ( mouse.x - camera.position.x ) * 0.05;
  167. camera.position.y += ( - mouse.y - camera.position.y ) * 0.05;
  168. camera.lookAt( center );
  169. renderer.render( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>