webgl_kinect.html 5.2 KB

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