webgl_camera_array.html 3.7 KB

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