shadows-spot-light-with-camera-gui.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 - Spot Light w/CameraHelper</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 = 100;
  114. const light = new THREE.SpotLight( 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 cameraHelper = new THREE.CameraHelper( light.shadow.camera );
  121. scene.add( cameraHelper );
  122. function updateCamera() {
  123. // update the light target's matrixWorld because it's needed by the helper
  124. light.target.updateMatrixWorld();
  125. // update the light's shadow camera's projection matrix
  126. light.shadow.camera.updateProjectionMatrix();
  127. // and now update the camera helper we're using to show the light's shadow camera
  128. cameraHelper.update();
  129. }
  130. updateCamera();
  131. setTimeout( updateCamera );
  132. class MinMaxGUIHelper {
  133. constructor( obj, minProp, maxProp, minDif ) {
  134. this.obj = obj;
  135. this.minProp = minProp;
  136. this.maxProp = maxProp;
  137. this.minDif = minDif;
  138. }
  139. get min() {
  140. return this.obj[ this.minProp ];
  141. }
  142. set min( v ) {
  143. this.obj[ this.minProp ] = v;
  144. this.obj[ this.maxProp ] = Math.max( this.obj[ this.maxProp ], v + this.minDif );
  145. }
  146. get max() {
  147. return this.obj[ this.maxProp ];
  148. }
  149. set max( v ) {
  150. this.obj[ this.maxProp ] = v;
  151. this.min = this.min; // this will call the min setter
  152. }
  153. }
  154. class DegRadHelper {
  155. constructor( obj, prop ) {
  156. this.obj = obj;
  157. this.prop = prop;
  158. }
  159. get value() {
  160. return THREE.MathUtils.radToDeg( this.obj[ this.prop ] );
  161. }
  162. set value( v ) {
  163. this.obj[ this.prop ] = THREE.MathUtils.degToRad( v );
  164. }
  165. }
  166. const gui = new GUI();
  167. gui.addColor( new ColorGUIHelper( light, 'color' ), 'value' ).name( 'color' );
  168. gui.add( light, 'intensity', 0, 200 );
  169. gui.add( light, 'distance', 0, 40 ).onChange( updateCamera );
  170. gui.add( new DegRadHelper( light, 'angle' ), 'value', 0, 90 ).name( 'angle' ).onChange( updateCamera );
  171. gui.add( light, 'penumbra', 0, 1, 0.01 );
  172. {
  173. const folder = gui.addFolder( 'Shadow Camera' );
  174. folder.open();
  175. const minMaxGUIHelper = new MinMaxGUIHelper( light.shadow.camera, 'near', 'far', 0.1 );
  176. folder.add( minMaxGUIHelper, 'min', 0.1, 50, 0.1 ).name( 'near' ).onChange( updateCamera );
  177. folder.add( minMaxGUIHelper, 'max', 0.1, 50, 0.1 ).name( 'far' ).onChange( updateCamera );
  178. }
  179. makeXYZGUI( gui, light.position, 'position', updateCamera );
  180. makeXYZGUI( gui, light.target.position, 'target', updateCamera );
  181. }
  182. function resizeRendererToDisplaySize( renderer ) {
  183. const canvas = renderer.domElement;
  184. const width = canvas.clientWidth;
  185. const height = canvas.clientHeight;
  186. const needResize = canvas.width !== width || canvas.height !== height;
  187. if ( needResize ) {
  188. renderer.setSize( width, height, false );
  189. }
  190. return needResize;
  191. }
  192. function render() {
  193. resizeRendererToDisplaySize( renderer );
  194. {
  195. const canvas = renderer.domElement;
  196. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  197. camera.updateProjectionMatrix();
  198. }
  199. renderer.render( scene, camera );
  200. requestAnimationFrame( render );
  201. }
  202. requestAnimationFrame( render );
  203. }
  204. main();
  205. </script>
  206. </html>