webgl_materials_video_webcam.html 3.0 KB

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