shadows-directional-light.html 4.8 KB

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