webgl_kinect.html 5.5 KB

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