webgl_kinect.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.js"></script>
  32. <script src='js/DAT.GUI.min.js'></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/Stats.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, smoothstep( 8000.0, -8000.0, gl_FragCoord.z / gl_FragCoord.w ) );
  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://github.com/mrdoob/three.js" 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. var width = 640, height = 480;
  101. var nearClipping = 850/*850*/, farClipping = 4000/*4000*/;
  102. geometry = new THREE.Geometry();
  103. for ( var i = 0, l = width * height; i < l; i ++ ) {
  104. var vertex = new THREE.Vector3();
  105. vertex.x = ( i % width );
  106. vertex.y = Math.floor( i / width );
  107. geometry.vertices.push( vertex );
  108. }
  109. material = new THREE.ShaderMaterial( {
  110. uniforms: {
  111. "map": { type: "t", value: 0, texture: texture },
  112. "width": { type: "f", value: width },
  113. "height": { type: "f", value: height },
  114. "nearClipping": { type: "f", value: nearClipping },
  115. "farClipping": { type: "f", value: farClipping },
  116. "pointSize": { type: "f", value: 2 },
  117. "zOffset": { type: "f", value: 1000 }
  118. },
  119. vertexShader: document.getElementById( 'vs' ).textContent,
  120. fragmentShader: document.getElementById( 'fs' ).textContent,
  121. depthWrite: false
  122. } );
  123. mesh = new THREE.ParticleSystem( geometry, material );
  124. mesh.position.x = 0;
  125. mesh.position.y = 0;
  126. scene.add( mesh );
  127. setInterval( function () {
  128. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  129. texture.needsUpdate = true;
  130. }
  131. }, 1000 / 30 );
  132. var gui = new DAT.GUI();
  133. gui.add( material.uniforms.nearClipping, 'value' ).name( 'nearClipping' ).min( 1 ).max( 10000 ).step( 1.0 );
  134. gui.add( material.uniforms.farClipping, 'value' ).name( 'farClipping' ).min( 1 ).max( 10000 ).step( 1.0 );
  135. gui.add( material.uniforms.pointSize, 'value' ).name( 'pointSize' ).min( 1 ).max( 10 ).step( 1.0 );
  136. gui.add( material.uniforms.zOffset, 'value' ).name( 'zOffset' ).min( 0 ).max( 4000 ).step( 1.0 );
  137. gui.close();
  138. }, false );
  139. video.loop = true;
  140. video.src = 'textures/kinect.webm';
  141. video.play();
  142. renderer = new THREE.WebGLRenderer();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. container.appendChild( renderer.domElement );
  145. mouse = new THREE.Vector3( 0, 0, 1 );
  146. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  147. //
  148. window.addEventListener( 'resize', onWindowResize, false );
  149. }
  150. function onWindowResize() {
  151. camera.aspect = window.innerWidth / window.innerHeight;
  152. camera.updateProjectionMatrix();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. }
  155. function onDocumentMouseMove( event ) {
  156. mouse.x = ( event.clientX - window.innerWidth / 2 ) * 8;
  157. mouse.y = ( event.clientY - window.innerHeight / 2 ) * 8;
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. render();
  162. stats.update();
  163. }
  164. function render() {
  165. camera.position.x += ( mouse.x - camera.position.x ) * 0.05;
  166. camera.position.y += ( - mouse.y - camera.position.y ) * 0.05;
  167. camera.lookAt( center );
  168. renderer.render( scene, camera );
  169. }
  170. </script>
  171. </body>
  172. </html>