cameras-perspective.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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</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. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. import {GUI} from '../../examples/jsm/libs/lil-gui.module.min.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. const fov = 45;
  41. const aspect = 2; // the canvas default
  42. const near = 0.1;
  43. const far = 100;
  44. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  45. camera.position.set(0, 10, 20);
  46. class MinMaxGUIHelper {
  47. constructor(obj, minProp, maxProp, minDif) {
  48. this.obj = obj;
  49. this.minProp = minProp;
  50. this.maxProp = maxProp;
  51. this.minDif = minDif;
  52. }
  53. get min() {
  54. return this.obj[this.minProp];
  55. }
  56. set min(v) {
  57. this.obj[this.minProp] = v;
  58. this.obj[this.maxProp] = Math.max(this.obj[this.maxProp], v + this.minDif);
  59. }
  60. get max() {
  61. return this.obj[this.maxProp];
  62. }
  63. set max(v) {
  64. this.obj[this.maxProp] = v;
  65. this.min = this.min; // this will call the min setter
  66. }
  67. }
  68. function updateCamera() {
  69. camera.updateProjectionMatrix();
  70. }
  71. const gui = new GUI();
  72. gui.add(camera, 'fov', 1, 180).onChange(updateCamera);
  73. const minMaxGUIHelper = new MinMaxGUIHelper(camera, 'near', 'far', 0.1);
  74. gui.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  75. gui.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  76. const controls = new OrbitControls(camera, canvas);
  77. controls.target.set(0, 5, 0);
  78. controls.update();
  79. const scene = new THREE.Scene();
  80. scene.background = new THREE.Color('black');
  81. {
  82. const planeSize = 40;
  83. const loader = new THREE.TextureLoader();
  84. const texture = loader.load('resources/images/checker.png');
  85. texture.wrapS = THREE.RepeatWrapping;
  86. texture.wrapT = THREE.RepeatWrapping;
  87. texture.magFilter = THREE.NearestFilter;
  88. const repeats = planeSize / 2;
  89. texture.repeat.set(repeats, repeats);
  90. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  91. const planeMat = new THREE.MeshPhongMaterial({
  92. map: texture,
  93. side: THREE.DoubleSide,
  94. });
  95. const mesh = new THREE.Mesh(planeGeo, planeMat);
  96. mesh.rotation.x = Math.PI * -.5;
  97. scene.add(mesh);
  98. }
  99. {
  100. const cubeSize = 4;
  101. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  102. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  103. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  104. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  105. scene.add(mesh);
  106. }
  107. {
  108. const sphereRadius = 3;
  109. const sphereWidthDivisions = 32;
  110. const sphereHeightDivisions = 16;
  111. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  112. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  113. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  114. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  115. scene.add(mesh);
  116. }
  117. {
  118. const color = 0xFFFFFF;
  119. const intensity = 1;
  120. const light = new THREE.DirectionalLight(color, intensity);
  121. light.position.set(0, 10, 0);
  122. light.target.position.set(-5, 0, 0);
  123. scene.add(light);
  124. scene.add(light.target);
  125. }
  126. function resizeRendererToDisplaySize(renderer) {
  127. const canvas = renderer.domElement;
  128. const width = canvas.clientWidth;
  129. const height = canvas.clientHeight;
  130. const needResize = canvas.width !== width || canvas.height !== height;
  131. if (needResize) {
  132. renderer.setSize(width, height, false);
  133. }
  134. return needResize;
  135. }
  136. function render() {
  137. if (resizeRendererToDisplaySize(renderer)) {
  138. const canvas = renderer.domElement;
  139. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  140. camera.updateProjectionMatrix();
  141. }
  142. renderer.render(scene, camera);
  143. requestAnimationFrame(render);
  144. }
  145. requestAnimationFrame(render);
  146. }
  147. main();
  148. </script>
  149. </html>