webgpu_camera_array.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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="example.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  12. <div class="title-wrapper">
  13. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Array Camera</span>
  14. </div>
  15. <small>Array cameras allow to render the scene with multiple sub cameras but with a single render call.</small>
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.webgpu.js",
  21. "three/webgpu": "../build/three.webgpu.js",
  22. "three/tsl": "../build/three.tsl.js",
  23. "three/addons/": "./jsm/"
  24. }
  25. }
  26. </script>
  27. <script type="module">
  28. import * as THREE from 'three/webgpu';
  29. let camera, scene, renderer;
  30. let mesh;
  31. const AMOUNT = 6;
  32. init();
  33. function init() {
  34. const subCameras = [];
  35. for ( let i = 0; i < AMOUNT * AMOUNT; i ++ ) {
  36. const subCamera = new THREE.PerspectiveCamera( 40, 1, 0.1, 10 );
  37. subCamera.viewport = new THREE.Vector4();
  38. subCameras.push( subCamera );
  39. }
  40. camera = new THREE.ArrayCamera( subCameras );
  41. camera.position.z = 3;
  42. updateCameras();
  43. scene = new THREE.Scene();
  44. scene.add( new THREE.AmbientLight( 0x999999 ) );
  45. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  46. light.position.set( 0.5, 0.5, 1 );
  47. light.castShadow = true;
  48. light.shadow.bias = - 0.001;
  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.WebGPURenderer( /*{ forceWebGL: true }*/ );
  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 updateCameras() {
  73. const ASPECT_RATIO = window.innerWidth / window.innerHeight;
  74. const WIDTH = window.innerWidth / AMOUNT;
  75. const HEIGHT = window.innerHeight / AMOUNT;
  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.copy( camera ); // copy fov, aspect ratio, near, far from the root camera
  82. subcamera.viewport.set( Math.floor( x * WIDTH ), Math.floor( y * HEIGHT ), Math.ceil( WIDTH ), Math.ceil( HEIGHT ) );
  83. subcamera.updateProjectionMatrix();
  84. subcamera.position.x = ( x / AMOUNT ) - 0.5;
  85. subcamera.position.y = 0.5 - ( y / AMOUNT );
  86. subcamera.position.z = 1.5 + ( ( x + y ) * .5 );
  87. subcamera.position.multiplyScalar( 2 );
  88. subcamera.lookAt( 0, 0, 0 );
  89. subcamera.updateMatrixWorld();
  90. }
  91. }
  92. }
  93. function onWindowResize() {
  94. updateCameras();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. function animate() {
  98. mesh.rotation.x += 0.005;
  99. mesh.rotation.z += 0.01;
  100. renderer.render( scene, camera );
  101. }
  102. </script>
  103. </body>
  104. </html>
粤ICP备19079148号