cameras-perspective.html 4.6 KB

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