webgl_kinect.html 5.9 KB

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