cameras-orthographic-2-scenes.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. texture.colorSpace = THREE.SRGBColorSpace;
  118. const repeats = planeSize / 2;
  119. texture.repeat.set( repeats, repeats );
  120. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  121. const planeMat = new THREE.MeshPhongMaterial( {
  122. map: texture,
  123. side: THREE.DoubleSide,
  124. } );
  125. const mesh = new THREE.Mesh( planeGeo, planeMat );
  126. mesh.rotation.x = Math.PI * - .5;
  127. scene.add( mesh );
  128. }
  129. {
  130. const cubeSize = 4;
  131. const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
  132. const cubeMat = new THREE.MeshPhongMaterial( { color: '#8AC' } );
  133. const mesh = new THREE.Mesh( cubeGeo, cubeMat );
  134. mesh.position.set( cubeSize + 1, cubeSize / 2, 0 );
  135. scene.add( mesh );
  136. }
  137. {
  138. const sphereRadius = 3;
  139. const sphereWidthDivisions = 32;
  140. const sphereHeightDivisions = 16;
  141. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  142. const sphereMat = new THREE.MeshPhongMaterial( { color: '#CA8' } );
  143. const mesh = new THREE.Mesh( sphereGeo, sphereMat );
  144. mesh.position.set( - sphereRadius - 1, sphereRadius + 2, 0 );
  145. scene.add( mesh );
  146. }
  147. {
  148. const color = 0xFFFFFF;
  149. const intensity = 3;
  150. const light = new THREE.DirectionalLight( color, intensity );
  151. light.position.set( 0, 10, 0 );
  152. light.target.position.set( - 5, 0, 0 );
  153. scene.add( light );
  154. scene.add( light.target );
  155. }
  156. function resizeRendererToDisplaySize( renderer ) {
  157. const canvas = renderer.domElement;
  158. const width = canvas.clientWidth;
  159. const height = canvas.clientHeight;
  160. const needResize = canvas.width !== width || canvas.height !== height;
  161. if ( needResize ) {
  162. renderer.setSize( width, height, false );
  163. }
  164. return needResize;
  165. }
  166. function setScissorForElement( elem ) {
  167. const canvasRect = canvas.getBoundingClientRect();
  168. const elemRect = elem.getBoundingClientRect();
  169. // compute a canvas relative rectangle
  170. const right = Math.min( elemRect.right, canvasRect.right ) - canvasRect.left;
  171. const left = Math.max( 0, elemRect.left - canvasRect.left );
  172. const bottom = Math.min( elemRect.bottom, canvasRect.bottom ) - canvasRect.top;
  173. const top = Math.max( 0, elemRect.top - canvasRect.top );
  174. const width = Math.min( canvasRect.width, right - left );
  175. const height = Math.min( canvasRect.height, bottom - top );
  176. // setup the scissor to only render to that part of the canvas
  177. const positiveYUpBottom = canvasRect.height - bottom;
  178. renderer.setScissor( left, positiveYUpBottom, width, height );
  179. renderer.setViewport( left, positiveYUpBottom, width, height );
  180. // return the aspect
  181. return width / height;
  182. }
  183. function render() {
  184. resizeRendererToDisplaySize( renderer );
  185. // turn on the scissor
  186. renderer.setScissorTest( true );
  187. // render the original view
  188. {
  189. const aspect = setScissorForElement( view1Elem );
  190. // update the camera for this aspect
  191. camera.left = - aspect;
  192. camera.right = aspect;
  193. camera.updateProjectionMatrix();
  194. cameraHelper.update();
  195. // don't draw the camera helper in the original view
  196. cameraHelper.visible = false;
  197. scene.background.set( 0x000000 );
  198. renderer.render( scene, camera );
  199. }
  200. // render from the 2nd camera
  201. {
  202. const aspect = setScissorForElement( view2Elem );
  203. // update the camera for this aspect
  204. camera2.aspect = aspect;
  205. camera2.updateProjectionMatrix();
  206. // draw the camera helper in the 2nd view
  207. cameraHelper.visible = true;
  208. scene.background.set( 0x000040 );
  209. renderer.render( scene, camera2 );
  210. }
  211. requestAnimationFrame( render );
  212. }
  213. requestAnimationFrame( render );
  214. }
  215. main();
  216. </script>
  217. </html>