webgl_kinect.html 5.8 KB

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