cameras-orthographic-2-scenes.html 6.7 KB

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