cameras-perspective.html 4.7 KB

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