webgl_camera_array.html 3.9 KB

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