2
0

webgl_camera_array.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - arraycamera</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. <!-- Import maps polyfill -->
  11. <!-- Remove this when import maps will be widely supported -->
  12. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. let camera, scene, renderer;
  24. let mesh;
  25. const AMOUNT = 6;
  26. init();
  27. animate();
  28. function init() {
  29. const ASPECT_RATIO = window.innerWidth / window.innerHeight;
  30. const WIDTH = ( window.innerWidth / AMOUNT ) * window.devicePixelRatio;
  31. const HEIGHT = ( window.innerHeight / AMOUNT ) * window.devicePixelRatio;
  32. const cameras = [];
  33. for ( let y = 0; y < AMOUNT; y ++ ) {
  34. for ( let x = 0; x < AMOUNT; x ++ ) {
  35. const subcamera = new THREE.PerspectiveCamera( 40, ASPECT_RATIO, 0.1, 10 );
  36. subcamera.viewport = new THREE.Vector4( Math.floor( x * WIDTH ), Math.floor( y * HEIGHT ), Math.ceil( WIDTH ), Math.ceil( HEIGHT ) );
  37. subcamera.position.x = ( x / AMOUNT ) - 0.5;
  38. subcamera.position.y = 0.5 - ( y / AMOUNT );
  39. subcamera.position.z = 1.5;
  40. subcamera.position.multiplyScalar( 2 );
  41. subcamera.lookAt( 0, 0, 0 );
  42. subcamera.updateMatrixWorld();
  43. cameras.push( subcamera );
  44. }
  45. }
  46. camera = new THREE.ArrayCamera( cameras );
  47. camera.position.z = 3;
  48. scene = new THREE.Scene();
  49. scene.add( new THREE.AmbientLight( 0x222244 ) );
  50. const light = new THREE.DirectionalLight();
  51. light.position.set( 0.5, 0.5, 1 );
  52. light.castShadow = true;
  53. light.shadow.camera.zoom = 4; // tighter shadow map
  54. scene.add( light );
  55. const geometryBackground = new THREE.PlaneGeometry( 100, 100 );
  56. const materialBackground = new THREE.MeshPhongMaterial( { color: 0x000066 } );
  57. const background = new THREE.Mesh( geometryBackground, materialBackground );
  58. background.receiveShadow = true;
  59. background.position.set( 0, 0, - 1 );
  60. scene.add( background );
  61. const geometryCylinder = new THREE.CylinderGeometry( 0.5, 0.5, 1, 32 );
  62. const materialCylinder = new THREE.MeshPhongMaterial( { color: 0xff0000 } );
  63. mesh = new THREE.Mesh( geometryCylinder, materialCylinder );
  64. mesh.castShadow = true;
  65. mesh.receiveShadow = true;
  66. scene.add( mesh );
  67. renderer = new THREE.WebGLRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.shadowMap.enabled = true;
  71. document.body.appendChild( renderer.domElement );
  72. //
  73. window.addEventListener( 'resize', onWindowResize );
  74. }
  75. function onWindowResize() {
  76. const ASPECT_RATIO = window.innerWidth / window.innerHeight;
  77. const WIDTH = ( window.innerWidth / AMOUNT ) * window.devicePixelRatio;
  78. const HEIGHT = ( window.innerHeight / AMOUNT ) * window.devicePixelRatio;
  79. camera.aspect = ASPECT_RATIO;
  80. camera.updateProjectionMatrix();
  81. for ( let y = 0; y < AMOUNT; y ++ ) {
  82. for ( let x = 0; x < AMOUNT; x ++ ) {
  83. const subcamera = camera.cameras[ AMOUNT * y + x ];
  84. subcamera.viewport.set(
  85. Math.floor( x * WIDTH ),
  86. Math.floor( y * HEIGHT ),
  87. Math.ceil( WIDTH ),
  88. Math.ceil( HEIGHT ) );
  89. subcamera.aspect = ASPECT_RATIO;
  90. subcamera.updateProjectionMatrix();
  91. }
  92. }
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. }
  95. function animate() {
  96. mesh.rotation.x += 0.005;
  97. mesh.rotation.z += 0.01;
  98. renderer.render( scene, camera );
  99. requestAnimationFrame( animate );
  100. }
  101. </script>
  102. </body>
  103. </html>