webgl_camera_array.html 3.5 KB

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