cameras-perspective-2-scenes.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Cameras - Perspective 2 views</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. .split {
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. width: 100%;
  23. height: 100%;
  24. display: flex;
  25. }
  26. .split>div {
  27. width: 100%;
  28. height: 100%;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <canvas id="c"></canvas>
  34. <div class="split">
  35. <div id="view1" tabindex="1"></div>
  36. <div id="view2" tabindex="2"></div>
  37. </div>
  38. </body>
  39. <!-- Import maps polyfill -->
  40. <!-- Remove this when import maps will be widely supported -->
  41. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  42. <script type="importmap">
  43. {
  44. "imports": {
  45. "three": "../../build/three.module.js"
  46. }
  47. }
  48. </script>
  49. <script type="module">
  50. import * as THREE from 'three';
  51. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  52. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  53. function main() {
  54. const canvas = document.querySelector('#c');
  55. const view1Elem = document.querySelector('#view1');
  56. const view2Elem = document.querySelector('#view2');
  57. const renderer = new THREE.WebGLRenderer({canvas});
  58. const fov = 45;
  59. const aspect = 2; // the canvas default
  60. const near = 5;
  61. const far = 100;
  62. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  63. camera.position.set(0, 10, 20);
  64. const cameraHelper = new THREE.CameraHelper(camera);
  65. class MinMaxGUIHelper {
  66. constructor(obj, minProp, maxProp, minDif) {
  67. this.obj = obj;
  68. this.minProp = minProp;
  69. this.maxProp = maxProp;
  70. this.minDif = minDif;
  71. }
  72. get min() {
  73. return this.obj[this.minProp];
  74. }
  75. set min(v) {
  76. this.obj[this.minProp] = v;
  77. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  78. }
  79. get max() {
  80. return this.obj[this.maxProp];
  81. }
  82. set max(v) {
  83. this.obj[this.maxProp] = v;
  84. this.min = this.min; // this will call the min setter
  85. }
  86. }
  87. const gui = new GUI();
  88. gui.add(camera, 'fov', 1, 180);
  89. const minMaxGUIHelper = new MinMaxGUIHelper(camera, 'near', 'far', 0.1);
  90. gui.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near');
  91. gui.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far');
  92. const controls = new OrbitControls(camera, view1Elem);
  93. controls.target.set(0, 5, 0);
  94. controls.update();
  95. const camera2 = new THREE.PerspectiveCamera(
  96. 60, // fov
  97. 2, // aspect
  98. 0.1, // near
  99. 500, // far
  100. );
  101. camera2.position.set(40, 10, 30);
  102. camera2.lookAt(0, 5, 0);
  103. const controls2 = new OrbitControls(camera2, view2Elem);
  104. controls2.target.set(0, 5, 0);
  105. controls2.update();
  106. const scene = new THREE.Scene();
  107. scene.background = new THREE.Color('black');
  108. scene.add(cameraHelper);
  109. {
  110. const planeSize = 40;
  111. const loader = new THREE.TextureLoader();
  112. const texture = loader.load('resources/images/checker.png');
  113. texture.wrapS = THREE.RepeatWrapping;
  114. texture.wrapT = THREE.RepeatWrapping;
  115. texture.magFilter = THREE.NearestFilter;
  116. const repeats = planeSize / 2;
  117. texture.repeat.set(repeats, repeats);
  118. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  119. const planeMat = new THREE.MeshPhongMaterial({
  120. map: texture,
  121. side: THREE.DoubleSide,
  122. });
  123. const mesh = new THREE.Mesh(planeGeo, planeMat);
  124. mesh.rotation.x = Math.PI * -.5;
  125. scene.add(mesh);
  126. }
  127. {
  128. const cubeSize = 4;
  129. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  130. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  131. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  132. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  133. scene.add(mesh);
  134. }
  135. {
  136. const sphereRadius = 3;
  137. const sphereWidthDivisions = 32;
  138. const sphereHeightDivisions = 16;
  139. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  140. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  141. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  142. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  143. scene.add(mesh);
  144. }
  145. {
  146. const color = 0xFFFFFF;
  147. const intensity = 1;
  148. const light = new THREE.DirectionalLight(color, intensity);
  149. light.position.set(0, 10, 0);
  150. light.target.position.set(-5, 0, 0);
  151. scene.add(light);
  152. scene.add(light.target);
  153. }
  154. function resizeRendererToDisplaySize(renderer) {
  155. const canvas = renderer.domElement;
  156. const width = canvas.clientWidth;
  157. const height = canvas.clientHeight;
  158. const needResize = canvas.width !== width || canvas.height !== height;
  159. if (needResize) {
  160. renderer.setSize(width, height, false);
  161. }
  162. return needResize;
  163. }
  164. function setScissorForElement(elem) {
  165. const canvasRect = canvas.getBoundingClientRect();
  166. const elemRect = elem.getBoundingClientRect();
  167. // compute a canvas relative rectangle
  168. const right = Math.min(elemRect.right, canvasRect.right) - canvasRect.left;
  169. const left = Math.max(0, elemRect.left - canvasRect.left);
  170. const bottom = Math.min(elemRect.bottom, canvasRect.bottom) - canvasRect.top;
  171. const top = Math.max(0, elemRect.top - canvasRect.top);
  172. const width = Math.min(canvasRect.width, right - left);
  173. const height = Math.min(canvasRect.height, bottom - top);
  174. // setup the scissor to only render to that part of the canvas
  175. const positiveYUpBottom = canvasRect.height - bottom;
  176. renderer.setScissor(left, positiveYUpBottom, width, height);
  177. renderer.setViewport(left, positiveYUpBottom, width, height);
  178. // return the aspect
  179. return width / height;
  180. }
  181. function render() {
  182. resizeRendererToDisplaySize(renderer);
  183. // turn on the scissor
  184. renderer.setScissorTest(true);
  185. // render the original view
  186. {
  187. const aspect = setScissorForElement(view1Elem);
  188. // adjust the camera for this aspect
  189. camera.aspect = aspect;
  190. camera.updateProjectionMatrix();
  191. cameraHelper.update();
  192. // don't draw the camera helper in the original view
  193. cameraHelper.visible = false;
  194. scene.background.set(0x000000);
  195. // render
  196. renderer.render(scene, camera);
  197. }
  198. // render from the 2nd camera
  199. {
  200. const aspect = setScissorForElement(view2Elem);
  201. // adjust the camera for this aspect
  202. camera2.aspect = aspect;
  203. camera2.updateProjectionMatrix();
  204. // draw the camera helper in the 2nd view
  205. cameraHelper.visible = true;
  206. scene.background.set(0x000040);
  207. renderer.render(scene, camera2);
  208. }
  209. requestAnimationFrame(render);
  210. }
  211. requestAnimationFrame(render);
  212. }
  213. main();
  214. </script>
  215. </html>