shadows-point-light.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 cubeSize = 30;
  82. const cubeGeo = new THREE.BoxGeometry( cubeSize, cubeSize, cubeSize );
  83. const cubeMat = new THREE.MeshPhongMaterial( {
  84. color: '#CCC',
  85. side: THREE.BackSide,
  86. } );
  87. const mesh = new THREE.Mesh( cubeGeo, cubeMat );
  88. mesh.receiveShadow = true;
  89. mesh.position.set( 0, cubeSize / 2 - 0.1, 0 );
  90. scene.add( mesh );
  91. }
  92. {
  93. const sphereRadius = 3;
  94. const sphereWidthDivisions = 32;
  95. const sphereHeightDivisions = 16;
  96. const sphereGeo = new THREE.SphereGeometry( sphereRadius, sphereWidthDivisions, sphereHeightDivisions );
  97. const sphereMat = new THREE.MeshPhongMaterial( { color: '#CA8' } );
  98. const mesh = new THREE.Mesh( sphereGeo, sphereMat );
  99. mesh.castShadow = true;
  100. mesh.receiveShadow = true;
  101. mesh.position.set( - sphereRadius - 1, sphereRadius + 2, 0 );
  102. scene.add( mesh );
  103. }
  104. class ColorGUIHelper {
  105. constructor( object, prop ) {
  106. this.object = object;
  107. this.prop = prop;
  108. }
  109. get value() {
  110. return `#${this.object[ this.prop ].getHexString()}`;
  111. }
  112. set value( hexString ) {
  113. this.object[ this.prop ].set( hexString );
  114. }
  115. }
  116. function makeXYZGUI( gui, vector3, name, onChangeFn ) {
  117. const folder = gui.addFolder( name );
  118. folder.add( vector3, 'x', - 10, 10 ).onChange( onChangeFn );
  119. folder.add( vector3, 'y', 0, 10 ).onChange( onChangeFn );
  120. folder.add( vector3, 'z', - 10, 10 ).onChange( onChangeFn );
  121. // folder.open();
  122. }
  123. {
  124. const color = 0xFFFFFF;
  125. const intensity = 100;
  126. const light = new THREE.PointLight( color, intensity );
  127. light.castShadow = true;
  128. light.position.set( 0, 10, 0 );
  129. scene.add( light );
  130. const helper = new THREE.PointLightHelper( light );
  131. scene.add( helper );
  132. function updateCamera() {
  133. }
  134. class MinMaxGUIHelper {
  135. constructor( obj, minProp, maxProp, minDif ) {
  136. this.obj = obj;
  137. this.minProp = minProp;
  138. this.maxProp = maxProp;
  139. this.minDif = minDif;
  140. }
  141. get min() {
  142. return this.obj[ this.minProp ];
  143. }
  144. set min( v ) {
  145. this.obj[ this.minProp ] = v;
  146. this.obj[ this.maxProp ] = Math.max( this.obj[ this.maxProp ], v + this.minDif );
  147. }
  148. get max() {
  149. return this.obj[ this.maxProp ];
  150. }
  151. set max( v ) {
  152. this.obj[ this.maxProp ] = v;
  153. this.min = this.min; // this will call the min setter
  154. }
  155. }
  156. const gui = new GUI();
  157. gui.addColor( new ColorGUIHelper( light, 'color' ), 'value' ).name( 'color' );
  158. gui.add( light, 'intensity', 0, 200 );
  159. gui.add( light, 'distance', 0, 40 ).onChange( updateCamera );
  160. {
  161. const folder = gui.addFolder( 'Shadow Camera' );
  162. folder.open();
  163. const minMaxGUIHelper = new MinMaxGUIHelper( light.shadow.camera, 'near', 'far', 0.1 );
  164. folder.add( minMaxGUIHelper, 'min', 0.1, 50, 0.1 ).name( 'near' ).onChange( updateCamera );
  165. folder.add( minMaxGUIHelper, 'max', 0.1, 50, 0.1 ).name( 'far' ).onChange( updateCamera );
  166. }
  167. makeXYZGUI( gui, light.position, 'position', updateCamera );
  168. }
  169. function resizeRendererToDisplaySize( renderer ) {
  170. const canvas = renderer.domElement;
  171. const width = canvas.clientWidth;
  172. const height = canvas.clientHeight;
  173. const needResize = canvas.width !== width || canvas.height !== height;
  174. if ( needResize ) {
  175. renderer.setSize( width, height, false );
  176. }
  177. return needResize;
  178. }
  179. function render() {
  180. resizeRendererToDisplaySize( renderer );
  181. {
  182. const canvas = renderer.domElement;
  183. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  184. camera.updateProjectionMatrix();
  185. }
  186. renderer.render( scene, camera );
  187. requestAnimationFrame( render );
  188. }
  189. requestAnimationFrame( render );
  190. }
  191. main();
  192. </script>
  193. </html>