webgl_kinect.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. </style>
  16. </head>
  17. <body>
  18. <script src="../build/Three.js"></script>
  19. <script src='js/DAT.GUI.min.js'></script>
  20. <script src="js/RequestAnimationFrame.js"></script>
  21. <script src="js/Stats.js"></script>
  22. <script id="vs" type="x-shader/x-vertex">
  23. uniform sampler2D map;
  24. uniform float width;
  25. uniform float height;
  26. uniform float nearClipping, farClipping;
  27. uniform float zOffset;
  28. varying vec2 vUv;
  29. const float XtoZ = 1.11146; // tan( 1.0144686 / 2.0 ) * 2.0;
  30. const float YtoZ = 0.83359; // tan( 0.7898090 / 2.0 ) * 2.0;
  31. void main() {
  32. vUv = vec2( position.x / width, 1.0 - ( position.y / height ) );
  33. vec4 color = texture2D( map, vUv );
  34. float depth = ( color.r + color.g + color.b ) / 3.0;
  35. // Projection code by @kcmic
  36. float z = ( 1.0 - depth ) * (farClipping - nearClipping) + nearClipping;
  37. vec4 pos = vec4(
  38. ( position.x / width - 0.5 ) * z * XtoZ,
  39. ( position.y / height - 0.5 ) * z * YtoZ,
  40. - z + zOffset,
  41. 1.0);
  42. gl_PointSize = 2.0;
  43. gl_Position = projectionMatrix * modelViewMatrix * pos;
  44. }
  45. </script>
  46. <script id="fs" type="x-shader/x-fragment">
  47. uniform sampler2D map;
  48. varying vec2 vUv;
  49. void main() {
  50. vec4 color = texture2D( map, vUv );
  51. gl_FragColor = vec4( color.r, color.g, color.b, smoothstep( 8000.0, -8000.0, gl_FragCoord.z / gl_FragCoord.w ) );
  52. }
  53. </script>
  54. <script>
  55. var container;
  56. var scene, camera, light, renderer;
  57. var geometry, cube, mesh, material;
  58. var mouse, center;
  59. var stats;
  60. var video, texture;
  61. init();
  62. animate();
  63. function init() {
  64. container = document.createElement( 'div' );
  65. document.body.appendChild( container );
  66. stats = new Stats();
  67. stats.domElement.style.position = 'absolute';
  68. stats.domElement.style.top = '0px';
  69. // container.appendChild( stats.domElement );
  70. scene = new THREE.Scene();
  71. center = new THREE.Vector3();
  72. center.z = - 1000;
  73. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  74. camera.position.set( 0, 0, 500 );
  75. scene.add( camera );
  76. video = document.createElement( 'video' );
  77. video.addEventListener( 'loadedmetadata', function ( event ) {
  78. texture = new THREE.Texture( video );
  79. var width = 640, height = 480;
  80. var nearClipping = 850/*850*/, farClipping = 4000/*4000*/;
  81. geometry = new THREE.Geometry();
  82. for ( var i = 0, l = width * height; i < l; i ++ ) {
  83. var position = new THREE.Vector3();
  84. position.x = ( i % width );
  85. position.y = Math.floor( i / width );
  86. geometry.vertices.push( new THREE.Vertex( position ) );
  87. }
  88. material = new THREE.ShaderMaterial( {
  89. uniforms: {
  90. "map": { type: "t", value: 0, texture: texture },
  91. "width": { type: "f", value: width },
  92. "height": { type: "f", value: height },
  93. "nearClipping": { type: "f", value: nearClipping },
  94. "farClipping": { type: "f", value: farClipping },
  95. "zOffset": { type: "f", value: 1000 }
  96. },
  97. vertexShader: document.getElementById( 'vs' ).textContent,
  98. fragmentShader: document.getElementById( 'fs' ).textContent,
  99. depthWrite: false
  100. } );
  101. mesh = new THREE.ParticleSystem( geometry, material );
  102. mesh.position.x = 0;
  103. mesh.position.y = 0;
  104. scene.add( mesh );
  105. setInterval( function () {
  106. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  107. texture.needsUpdate = true;
  108. }
  109. }, 1000 / 30 );
  110. var gui = new DAT.GUI();
  111. gui.add( material.uniforms.zOffset, 'value' ).name( 'zOffset' ).min( 0 ).max( 4000 ).step( 1.0 );
  112. gui.close();
  113. }, false );
  114. video.loop = true;
  115. video.src = 'textures/kinect.webm';
  116. video.play();
  117. renderer = new THREE.WebGLRenderer();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. container.appendChild( renderer.domElement );
  120. mouse = new THREE.Vector3( 0, 0, 1 );
  121. projector = new THREE.Projector();
  122. ray = new THREE.Ray( camera.position );
  123. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  124. }
  125. function onDocumentMouseMove( event ) {
  126. mouse.x = ( event.clientX - window.innerWidth / 2 ) * 8;
  127. mouse.y = ( event.clientY - window.innerHeight / 2 ) * 8;
  128. }
  129. function animate() {
  130. requestAnimationFrame( animate );
  131. render();
  132. stats.update();
  133. }
  134. function render() {
  135. video.width = 256;
  136. video.height = 256;
  137. camera.position.x += ( mouse.x - camera.position.x ) * 0.05;
  138. camera.position.y += ( - mouse.y - camera.position.y ) * 0.05;
  139. camera.lookAt( center );
  140. renderer.render( scene, camera );
  141. }
  142. </script>
  143. </body>
  144. </html>