cameras-orthographic-2-scenes.html 6.9 KB

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