webgl_video_kinect.html 5.5 KB

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