webgl_video_kinect.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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="importmap">
  48. {
  49. "imports": {
  50. "three": "../build/three.module.js",
  51. "three/addons/": "./jsm/"
  52. }
  53. }
  54. </script>
  55. <script type="module">
  56. import * as THREE from 'three';
  57. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  58. let scene, camera, renderer;
  59. let geometry, mesh, material;
  60. let mouse, center;
  61. init();
  62. function init() {
  63. const container = document.createElement( 'div' );
  64. document.body.appendChild( container );
  65. const info = document.createElement( 'div' );
  66. info.id = 'info';
  67. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - kinect';
  68. document.body.appendChild( info );
  69. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 10000 );
  70. camera.position.set( 0, 0, 500 );
  71. scene = new THREE.Scene();
  72. center = new THREE.Vector3();
  73. center.z = - 1000;
  74. const video = document.getElementById( 'video' );
  75. const texture = new THREE.VideoTexture( video );
  76. texture.minFilter = THREE.NearestFilter;
  77. const width = 640, height = 480;
  78. const nearClipping = 850, farClipping = 4000;
  79. geometry = new THREE.BufferGeometry();
  80. const vertices = new Float32Array( width * height * 3 );
  81. for ( let i = 0, j = 0, l = vertices.length; i < l; i += 3, j ++ ) {
  82. vertices[ i ] = j % width;
  83. vertices[ i + 1 ] = Math.floor( j / width );
  84. }
  85. geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  86. material = new THREE.ShaderMaterial( {
  87. uniforms: {
  88. 'map': { value: texture },
  89. 'width': { value: width },
  90. 'height': { value: height },
  91. 'nearClipping': { value: nearClipping },
  92. 'farClipping': { value: farClipping },
  93. 'pointSize': { value: 2 },
  94. 'zOffset': { value: 1000 }
  95. },
  96. vertexShader: document.getElementById( 'vs' ).textContent,
  97. fragmentShader: document.getElementById( 'fs' ).textContent,
  98. blending: THREE.AdditiveBlending,
  99. depthTest: false, depthWrite: false,
  100. transparent: true
  101. } );
  102. mesh = new THREE.Points( geometry, material );
  103. scene.add( mesh );
  104. const gui = new GUI();
  105. gui.add( material.uniforms.nearClipping, 'value', 1, 10000, 1.0 ).name( 'nearClipping' );
  106. gui.add( material.uniforms.farClipping, 'value', 1, 10000, 1.0 ).name( 'farClipping' );
  107. gui.add( material.uniforms.pointSize, 'value', 1, 10, 1.0 ).name( 'pointSize' );
  108. gui.add( material.uniforms.zOffset, 'value', 0, 4000, 1.0 ).name( 'zOffset' );
  109. video.play();
  110. //
  111. renderer = new THREE.WebGLRenderer();
  112. renderer.setPixelRatio( window.devicePixelRatio );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. renderer.setAnimationLoop( animate );
  115. container.appendChild( renderer.domElement );
  116. mouse = new THREE.Vector3( 0, 0, 1 );
  117. document.addEventListener( 'mousemove', onDocumentMouseMove );
  118. //
  119. window.addEventListener( 'resize', onWindowResize );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function onDocumentMouseMove( event ) {
  127. mouse.x = ( event.clientX - window.innerWidth / 2 ) * 8;
  128. mouse.y = ( event.clientY - window.innerHeight / 2 ) * 8;
  129. }
  130. function animate() {
  131. camera.position.x += ( mouse.x - camera.position.x ) * 0.05;
  132. camera.position.y += ( - mouse.y - camera.position.y ) * 0.05;
  133. camera.lookAt( center );
  134. renderer.render( scene, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>