cameras-orthographic-2-scenes.html 6.7 KB

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