cameras-perspective.html 4.4 KB

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