webgpu_camera_array.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - 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.webgpu.js",
  14. "three/webgpu": "../build/three.webgpu.js",
  15. "three/tsl": "../build/three.tsl.js",
  16. "three/addons/": "./jsm/"
  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. function init() {
  27. const ASPECT_RATIO = window.innerWidth / window.innerHeight;
  28. const WIDTH = ( window.innerWidth / AMOUNT );
  29. const HEIGHT = ( window.innerHeight / AMOUNT );
  30. const cameras = [];
  31. for ( let y = 0; y < AMOUNT; y ++ ) {
  32. for ( let x = 0; x < AMOUNT; x ++ ) {
  33. const subcamera = new THREE.PerspectiveCamera( 40, ASPECT_RATIO, 0.1, 10 );
  34. subcamera.viewport = new THREE.Vector4( Math.floor( x * WIDTH ), Math.floor( y * HEIGHT ), Math.ceil( WIDTH ), Math.ceil( HEIGHT ) );
  35. subcamera.position.x = ( x / AMOUNT ) - 0.5;
  36. subcamera.position.y = 0.5 - ( y / AMOUNT );
  37. subcamera.position.z = 1.5;
  38. subcamera.position.multiplyScalar( 2 );
  39. subcamera.lookAt( 0, 0, 0 );
  40. subcamera.updateMatrixWorld();
  41. cameras.push( subcamera );
  42. }
  43. }
  44. camera = new THREE.ArrayCamera( cameras );
  45. camera.position.z = 3;
  46. scene = new THREE.Scene();
  47. scene.add( new THREE.AmbientLight( 0x999999 ) );
  48. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  49. light.position.set( 0.5, 0.5, 1 );
  50. light.castShadow = true;
  51. light.shadow.camera.zoom = 4; // tighter shadow map
  52. scene.add( light );
  53. const geometryBackground = new THREE.PlaneGeometry( 100, 100 );
  54. const materialBackground = new THREE.MeshPhongMaterial( { color: 0x000066 } );
  55. const background = new THREE.Mesh( geometryBackground, materialBackground );
  56. background.receiveShadow = true;
  57. background.position.set( 0, 0, - 1 );
  58. scene.add( background );
  59. const geometryCylinder = new THREE.CylinderGeometry( 0.5, 0.5, 1, 32 );
  60. const materialCylinder = new THREE.MeshPhongMaterial( { color: 0xff0000 } );
  61. mesh = new THREE.Mesh( geometryCylinder, materialCylinder );
  62. mesh.castShadow = true;
  63. mesh.receiveShadow = true;
  64. scene.add( mesh );
  65. renderer = new THREE.WebGPURenderer();
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.setAnimationLoop( animate );
  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 );
  77. const HEIGHT = ( window.innerHeight / AMOUNT );
  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. }
  99. </script>
  100. </body>
  101. </html>
粤ICP备19079148号