cameras-orthographic-2-scenes.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 - Orthographic 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. "three/addons/": "../../examples/jsm/"
  47. }
  48. }
  49. </script>
  50. <script type="module">
  51. import * as THREE from 'three';
  52. import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  53. import {GUI} from 'three/addons/libs/lil-gui.module.min.js';
  54. function main() {
  55. const canvas = document.querySelector('#c');
  56. const view1Elem = document.querySelector('#view1');
  57. const view2Elem = document.querySelector('#view2');
  58. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  59. const size = 1;
  60. const near = 5;
  61. const far = 50;
  62. const camera = new THREE.OrthographicCamera(-size, size, size, -size, near, far);
  63. camera.zoom = 0.2;
  64. camera.position.set(0, 10, 20);
  65. const cameraHelper = new THREE.CameraHelper(camera);
  66. class MinMaxGUIHelper {
  67. constructor(obj, minProp, maxProp, minDif) {
  68. this.obj = obj;
  69. this.minProp = minProp;
  70. this.maxProp = maxProp;
  71. this.minDif = minDif;
  72. }
  73. get min() {
  74. return this.obj[this.minProp];
  75. }
  76. set min(v) {
  77. this.obj[this.minProp] = v;
  78. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  79. }
  80. get max() {
  81. return this.obj[this.maxProp];
  82. }
  83. set max(v) {
  84. this.obj[this.maxProp] = v;
  85. this.min = this.min; // this will call the min setter
  86. }
  87. }
  88. const gui = new GUI();
  89. gui.add(camera, 'zoom', 0.01, 1, 0.01).listen();
  90. const minMaxGUIHelper = new MinMaxGUIHelper(camera, 'near', 'far', 0.1);
  91. gui.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near');
  92. gui.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far');
  93. const controls = new OrbitControls(camera, view1Elem);
  94. controls.target.set(0, 5, 0);
  95. controls.update();
  96. const camera2 = new THREE.PerspectiveCamera(
  97. 60, // fov
  98. 2, // aspect
  99. 0.1, // near
  100. 500, // far
  101. );
  102. camera2.position.set(16, 28, 40);
  103. camera2.lookAt(0, 5, 0);
  104. const controls2 = new OrbitControls(camera2, view2Elem);
  105. controls2.target.set(0, 5, 0);
  106. controls2.update();
  107. const scene = new THREE.Scene();
  108. scene.background = new THREE.Color('black');
  109. scene.add(cameraHelper);
  110. {
  111. const planeSize = 40;
  112. const loader = new THREE.TextureLoader();
  113. const texture = loader.load('resources/images/checker.png');
  114. texture.wrapS = THREE.RepeatWrapping;
  115. texture.wrapT = THREE.RepeatWrapping;
  116. texture.magFilter = THREE.NearestFilter;
  117. const repeats = planeSize / 2;
  118. texture.repeat.set(repeats, repeats);
  119. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  120. const planeMat = new THREE.MeshPhongMaterial({
  121. map: texture,
  122. side: THREE.DoubleSide,
  123. });
  124. const mesh = new THREE.Mesh(planeGeo, planeMat);
  125. mesh.rotation.x = Math.PI * -.5;
  126. scene.add(mesh);
  127. }
  128. {
  129. const cubeSize = 4;
  130. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  131. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  132. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  133. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  134. scene.add(mesh);
  135. }
  136. {
  137. const sphereRadius = 3;
  138. const sphereWidthDivisions = 32;
  139. const sphereHeightDivisions = 16;
  140. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  141. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  142. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  143. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  144. scene.add(mesh);
  145. }
  146. {
  147. const color = 0xFFFFFF;
  148. const intensity = 1;
  149. const light = new THREE.DirectionalLight(color, intensity);
  150. light.position.set(0, 10, 0);
  151. light.target.position.set(-5, 0, 0);
  152. scene.add(light);
  153. scene.add(light.target);
  154. }
  155. function resizeRendererToDisplaySize(renderer) {
  156. const canvas = renderer.domElement;
  157. const width = canvas.clientWidth;
  158. const height = canvas.clientHeight;
  159. const needResize = canvas.width !== width || canvas.height !== height;
  160. if (needResize) {
  161. renderer.setSize(width, height, false);
  162. }
  163. return needResize;
  164. }
  165. function setScissorForElement(elem) {
  166. const canvasRect = canvas.getBoundingClientRect();
  167. const elemRect = elem.getBoundingClientRect();
  168. // compute a canvas relative rectangle
  169. const right = Math.min(elemRect.right, canvasRect.right) - canvasRect.left;
  170. const left = Math.max(0, elemRect.left - canvasRect.left);
  171. const bottom = Math.min(elemRect.bottom, canvasRect.bottom) - canvasRect.top;
  172. const top = Math.max(0, elemRect.top - canvasRect.top);
  173. const width = Math.min(canvasRect.width, right - left);
  174. const height = Math.min(canvasRect.height, bottom - top);
  175. // setup the scissor to only render to that part of the canvas
  176. const positiveYUpBottom = canvasRect.height - bottom;
  177. renderer.setScissor(left, positiveYUpBottom, width, height);
  178. renderer.setViewport(left, positiveYUpBottom, width, height);
  179. // return the aspect
  180. return width / height;
  181. }
  182. function render() {
  183. resizeRendererToDisplaySize(renderer);
  184. // turn on the scissor
  185. renderer.setScissorTest(true);
  186. // render the original view
  187. {
  188. const aspect = setScissorForElement(view1Elem);
  189. // update the camera for this aspect
  190. camera.left = -aspect;
  191. camera.right = aspect;
  192. camera.updateProjectionMatrix();
  193. cameraHelper.update();
  194. // don't draw the camera helper in the original view
  195. cameraHelper.visible = false;
  196. scene.background.set(0x000000);
  197. renderer.render(scene, camera);
  198. }
  199. // render from the 2nd camera
  200. {
  201. const aspect = setScissorForElement(view2Elem);
  202. // update the camera for this aspect
  203. camera2.aspect = aspect;
  204. camera2.updateProjectionMatrix();
  205. // draw the camera helper in the 2nd view
  206. cameraHelper.visible = true;
  207. scene.background.set(0x000040);
  208. renderer.render(scene, camera2);
  209. }
  210. requestAnimationFrame(render);
  211. }
  212. requestAnimationFrame(render);
  213. }
  214. main();
  215. </script>
  216. </html>