lights-directional.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 - Lights - Directional</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. const controls = new OrbitControls( camera, canvas );
  45. controls.target.set( 0, 5, 0 );
  46. controls.update();
  47. const scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 'black' );
  49. {
  50. const planeSize = 40;
  51. const loader = new THREE.TextureLoader();
  52. const texture = loader.load( 'resources/images/checker.png' );
  53. texture.wrapS = THREE.RepeatWrapping;
  54. texture.wrapT = THREE.RepeatWrapping;
  55. texture.magFilter = THREE.NearestFilter;
  56. texture.colorSpace = THREE.SRGBColorSpace;
  57. const repeats = planeSize / 2;
  58. texture.repeat.set( repeats, repeats );
  59. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  60. const planeMat = new THREE.MeshPhongMaterial( {
  61. map: texture,
  62. side: THREE.DoubleSide,
  63. } );
  64. const mesh = new THREE.Mesh( planeGeo, planeMat );
  65. mesh.rotation.x = Math.PI * - .5;
  66. scene.add( mesh );
  67. }
  68. {
  69. const cubeSize = 4;
  70. const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
  71. const cubeMat = new THREE.MeshPhongMaterial( { color: '#8AC' } );
  72. const mesh = new THREE.Mesh( cubeGeo, cubeMat );
  73. mesh.position.set( cubeSize + 1, cubeSize / 2, 0 );
  74. scene.add( mesh );
  75. }
  76. {
  77. const sphereRadius = 3;
  78. const sphereWidthDivisions = 32;
  79. const sphereHeightDivisions = 16;
  80. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  81. const sphereMat = new THREE.MeshPhongMaterial( { color: '#CA8' } );
  82. const mesh = new THREE.Mesh( sphereGeo, sphereMat );
  83. mesh.position.set( - sphereRadius - 1, sphereRadius + 2, 0 );
  84. scene.add( mesh );
  85. }
  86. class ColorGUIHelper {
  87. constructor( object, prop ) {
  88. this.object = object;
  89. this.prop = prop;
  90. }
  91. get value() {
  92. return `#${this.object[ this.prop ].getHexString()}`;
  93. }
  94. set value( hexString ) {
  95. this.object[ this.prop ].set( hexString );
  96. }
  97. }
  98. {
  99. const color = 0xFFFFFF;
  100. const intensity = 1;
  101. const light = new THREE.DirectionalLight( color, intensity );
  102. light.position.set( 0, 10, 0 );
  103. light.target.position.set( - 5, 0, 0 );
  104. scene.add( light );
  105. scene.add( light.target );
  106. const gui = new GUI();
  107. gui.addColor( new ColorGUIHelper( light, 'color' ), 'value' ).name( 'color' );
  108. gui.add( light, 'intensity', 0, 5, 0.01 );
  109. gui.add( light.target.position, 'x', - 10, 10, .01 );
  110. gui.add( light.target.position, 'z', - 10, 10, .01 );
  111. gui.add( light.target.position, 'y', 0, 10, .01 );
  112. }
  113. function resizeRendererToDisplaySize( renderer ) {
  114. const canvas = renderer.domElement;
  115. const width = canvas.clientWidth;
  116. const height = canvas.clientHeight;
  117. const needResize = canvas.width !== width || canvas.height !== height;
  118. if ( needResize ) {
  119. renderer.setSize( width, height, false );
  120. }
  121. return needResize;
  122. }
  123. function render() {
  124. if ( resizeRendererToDisplaySize( renderer ) ) {
  125. const canvas = renderer.domElement;
  126. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  127. camera.updateProjectionMatrix();
  128. }
  129. renderer.render( scene, camera );
  130. requestAnimationFrame( render );
  131. }
  132. requestAnimationFrame( render );
  133. }
  134. main();
  135. </script>
  136. </html>