webgl_materials_video_webcam.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - video - webcam</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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - video webcam input
  12. </div>
  13. <video id="video" autoplay style="display:none"></video>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. var camera, scene, renderer, video;
  18. init();
  19. animate();
  20. function init() {
  21. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  22. camera.position.z = 0.01;
  23. scene = new THREE.Scene();
  24. video = document.getElementById( 'video' );
  25. var texture = new THREE.VideoTexture( video );
  26. var geometry = new THREE.PlaneBufferGeometry( 16, 9 );
  27. geometry.scale( 0.5, 0.5, 0.5 );
  28. var material = new THREE.MeshBasicMaterial( { map: texture } );
  29. var count = 128;
  30. var radius = 32;
  31. for ( var i = 1, l = count; i <= l; i ++ ) {
  32. var phi = Math.acos( - 1 + ( 2 * i ) / l );
  33. var theta = Math.sqrt( l * Math.PI ) * phi;
  34. var mesh = new THREE.Mesh( geometry, material );
  35. mesh.position.setFromSphericalCoords( radius, phi, theta );
  36. mesh.lookAt( camera.position );
  37. scene.add( mesh );
  38. }
  39. renderer = new THREE.WebGLRenderer( { antialias: true } );
  40. renderer.setPixelRatio( window.devicePixelRatio );
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. document.body.appendChild( renderer.domElement );
  43. var controls = new OrbitControls( camera, renderer.domElement );
  44. controls.enableZoom = false;
  45. controls.enablePan = false;
  46. window.addEventListener( 'resize', onWindowResize, false );
  47. //
  48. if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
  49. var constraints = { video: { width: 1280, height: 720, facingMode: 'user' } };
  50. navigator.mediaDevices.getUserMedia( constraints ).then( function ( stream ) {
  51. // apply the stream to the video element used in the texture
  52. video.srcObject = stream;
  53. video.play();
  54. } ).catch( function ( error ) {
  55. console.error( 'Unable to access the camera/webcam.', error );
  56. } );
  57. } else {
  58. console.error( 'MediaDevices interface not available.' );
  59. }
  60. }
  61. function onWindowResize() {
  62. camera.aspect = window.innerWidth / window.innerHeight;
  63. camera.updateProjectionMatrix();
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. }
  66. function animate() {
  67. requestAnimationFrame( animate );
  68. renderer.render( scene, camera );
  69. }
  70. </script>
  71. </body>
  72. </html>