threejs-cameras-perspective-2-scenes.html 6.7 KB

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