shadows.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Shadows</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Shadows">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Shadows</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>This article is part of a series of articles about three.js. The
  29. first article is <a href="fundamentals.html">three.js fundamentals</a>. If
  30. you haven't read that yet and you're new to three.js you might want to
  31. consider starting there. The
  32. <a href="cameras.html">previous article was about cameras</a> which is
  33. important to have read before you read this article as well as
  34. the <a href="lights.html">article before that one about lights</a>.</p>
  35. <p>Shadows on computers can be a complicated topic. There are various
  36. solutions and all of them have tradeoffs including the solutions
  37. available in three.js.</p>
  38. <p>Three.js by default uses <em>shadow maps</em>. The way a shadow map works
  39. is, <em>for every light that casts shadows all objects marked to cast
  40. shadows are rendered from the point of view of the light</em>. <strong>READ THAT
  41. AGAIN!</strong> and let it sink in.</p>
  42. <p>In other words, if you have 20 objects, and 5 lights, and
  43. all 20 objects are casting shadows and all 5 lights are casting
  44. shadows then your entire scene will be drawn 6 times. All 20 objects
  45. will be drawn for light #1, then all 20 objects will be drawn for
  46. light #2, then #3, etc and finally the actual scene will be drawn
  47. using data from the first 5 renders.</p>
  48. <p>It gets worse, if you have a point light casting shadows the scene
  49. has to be drawn 6 times just for that light!</p>
  50. <p>For these reasons it's common to find other solutions than to have
  51. a bunch of lights all generating shadows. One common solution
  52. is to have multiple lights but only one directional light generating
  53. shadows.</p>
  54. <p>Yet another solution is to use lightmaps and or ambient occlusion maps
  55. to pre-compute the effects of lighting offline. This results in static
  56. lighting or static lighting hints but at least it's fast. We'll
  57. cover both of those in another article.</p>
  58. <p>Another solution is to use fake shadows. Make a plane, put a grayscale
  59. texture in the plane that approximates a shadow,
  60. draw it above the ground below your object.</p>
  61. <p>For example let's use this texture as a fake shadow</p>
  62. <div class="threejs_center"><img src="../examples/resources/images/roundshadow.png"></div>
  63. <p>We'll use some of the code from <a href="cameras.html">the previous article</a>.</p>
  64. <p>Let's set the background color to white.</p>
  65. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  66. +scene.background = new THREE.Color('white');
  67. </pre>
  68. <p>Then we'll setup the same checkerboard ground but this time it's using
  69. a <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> as we don't need lighting for the ground.</p>
  70. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const loader = new THREE.TextureLoader();
  71. {
  72. const planeSize = 40;
  73. - const loader = new THREE.TextureLoader();
  74. const texture = loader.load('resources/images/checker.png');
  75. texture.wrapS = THREE.RepeatWrapping;
  76. texture.wrapT = THREE.RepeatWrapping;
  77. texture.magFilter = THREE.NearestFilter;
  78. const repeats = planeSize / 2;
  79. texture.repeat.set(repeats, repeats);
  80. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  81. const planeMat = new THREE.MeshBasicMaterial({
  82. map: texture,
  83. side: THREE.DoubleSide,
  84. });
  85. + planeMat.color.setRGB(1.5, 1.5, 1.5);
  86. const mesh = new THREE.Mesh(planeGeo, planeMat);
  87. mesh.rotation.x = Math.PI * -.5;
  88. scene.add(mesh);
  89. }
  90. </pre>
  91. <p>Note we're setting the color to <code class="notranslate" translate="no">1.5, 1.5, 1.5</code>. This will multiply the checkerboard
  92. texture's colors by 1.5, 1.5, 1.5. Since the texture's colors are 0x808080 and 0xC0C0C0
  93. which is medium gray and light gray, multiplying them by 1.5 will give us a white and
  94. light grey checkerboard.</p>
  95. <p>Let's load the shadow texture</p>
  96. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const shadowTexture = loader.load('resources/images/roundshadow.png');
  97. </pre>
  98. <p>and make an array to remember each sphere and associated objects.</p>
  99. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereShadowBases = [];
  100. </pre>
  101. <p>Then we'll make a sphere geometry</p>
  102. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereRadius = 1;
  103. const sphereWidthDivisions = 32;
  104. const sphereHeightDivisions = 16;
  105. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  106. </pre>
  107. <p>And a plane geometry for the fake shadow</p>
  108. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 1;
  109. const shadowGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  110. </pre>
  111. <p>Now we'll make a bunch of spheres. For each sphere we'll create a <code class="notranslate" translate="no">base</code>
  112. <a href="/docs/#api/en/core/Object3D"><code class="notranslate" translate="no">THREE.Object3D</code></a> and we'll make both the shadow plane mesh and the sphere mesh
  113. children of the base. That way if we move the base both the sphere and the shadow
  114. will move. We need to put the shadow slightly above the ground to prevent z-fighting.
  115. We also set <code class="notranslate" translate="no">depthWrite</code> to false so that the shadows don't mess each other up.
  116. We'll go over both of these issues in <a href="transparency.html">another article</a>.
  117. The shadow is a <a href="/docs/#api/en/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> because it doesn't need lighting.</p>
  118. <p>We make each sphere a different hue and then save off the base, the sphere mesh,
  119. the shadow mesh and the initial y position of each sphere.</p>
  120. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const numSpheres = 15;
  121. for (let i = 0; i &lt; numSpheres; ++i) {
  122. // make a base for the shadow and the sphere
  123. // so they move together.
  124. const base = new THREE.Object3D();
  125. scene.add(base);
  126. // add the shadow to the base
  127. // note: we make a new material for each sphere
  128. // so we can set that sphere's material transparency
  129. // separately.
  130. const shadowMat = new THREE.MeshBasicMaterial({
  131. map: shadowTexture,
  132. transparent: true, // so we can see the ground
  133. depthWrite: false, // so we don't have to sort
  134. });
  135. const shadowMesh = new THREE.Mesh(shadowGeo, shadowMat);
  136. shadowMesh.position.y = 0.001; // so we're above the ground slightly
  137. shadowMesh.rotation.x = Math.PI * -.5;
  138. const shadowSize = sphereRadius * 4;
  139. shadowMesh.scale.set(shadowSize, shadowSize, shadowSize);
  140. base.add(shadowMesh);
  141. // add the sphere to the base
  142. const u = i / numSpheres; // goes from 0 to 1 as we iterate the spheres.
  143. const sphereMat = new THREE.MeshPhongMaterial();
  144. sphereMat.color.setHSL(u, 1, .75);
  145. const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
  146. sphereMesh.position.set(0, sphereRadius + 2, 0);
  147. base.add(sphereMesh);
  148. // remember all 3 plus the y position
  149. sphereShadowBases.push({base, sphereMesh, shadowMesh, y: sphereMesh.position.y});
  150. }
  151. </pre>
  152. <p>We setup 2 lights. One is a <a href="/docs/#api/en/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> with the intensity set to 2 to really
  153. brighten things up.</p>
  154. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  155. const skyColor = 0xB1E1FF; // light blue
  156. const groundColor = 0xB97A20; // brownish orange
  157. const intensity = 2;
  158. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  159. scene.add(light);
  160. }
  161. </pre>
  162. <p>The other is a <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> so the spheres get some definition</p>
  163. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  164. const color = 0xFFFFFF;
  165. const intensity = 1;
  166. const light = new THREE.DirectionalLight(color, intensity);
  167. light.position.set(0, 10, 5);
  168. light.target.position.set(-5, 0, 0);
  169. scene.add(light);
  170. scene.add(light.target);
  171. }
  172. </pre>
  173. <p>It would render as is but let's animate there spheres.
  174. For each sphere, shadow, base set we move the base in the xz plane, we
  175. move the sphere up and down using <a href="/docs/#api/en/math/Math.abs(Math.sin(time))"><code class="notranslate" translate="no">Math.abs(Math.sin(time))</code></a>
  176. which gives us a bouncy animation. And, we also set the shadow material's
  177. opacity so that as each sphere goes higher its shadow fades out.</p>
  178. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  179. time *= 0.001; // convert to seconds
  180. ...
  181. sphereShadowBases.forEach((sphereShadowBase, ndx) =&gt; {
  182. const {base, sphereMesh, shadowMesh, y} = sphereShadowBase;
  183. // u is a value that goes from 0 to 1 as we iterate the spheres
  184. const u = ndx / sphereShadowBases.length;
  185. // compute a position for the base. This will move
  186. // both the sphere and its shadow
  187. const speed = time * .2;
  188. const angle = speed + u * Math.PI * 2 * (ndx % 1 ? 1 : -1);
  189. const radius = Math.sin(speed - ndx) * 10;
  190. base.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
  191. // yOff is a value that goes from 0 to 1
  192. const yOff = Math.abs(Math.sin(time * 2 + ndx));
  193. // move the sphere up and down
  194. sphereMesh.position.y = y + THREE.MathUtils.lerp(-2, 2, yOff);
  195. // fade the shadow as the sphere goes up
  196. shadowMesh.material.opacity = THREE.MathUtils.lerp(1, .25, yOff);
  197. });
  198. ...
  199. </pre>
  200. <p>And here's 15 kind of bouncing balls.</p>
  201. <p></p><div translate="no" class="threejs_example_container notranslate">
  202. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-fake.html"></iframe></div>
  203. <a class="threejs_center" href="/manual/examples/shadows-fake.html" target="_blank">click here to open in a separate window</a>
  204. </div>
  205. <p></p>
  206. <p>In some apps it's common to use a round or oval shadow for everything but
  207. of course you could also use different shaped shadow textures. You might also
  208. give the shadow a harder edge. A good example of using this type
  209. of shadow is <a href="https://www.google.com/search?tbm=isch&amp;q=animal+crossing+pocket+camp+screenshots">Animal Crossing Pocket Camp</a>
  210. where you can see each character has a simple round shadow. It's effective and cheap.
  211. <a href="https://www.google.com/search?q=monument+valley+screenshots&amp;tbm=isch">Monument Valley</a>
  212. appears to also use this kind of shadow for the main character.</p>
  213. <p>So, moving on to shadow maps, there are 3 lights which can cast shadows. The <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>,
  214. the <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>, and the <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>.</p>
  215. <p>Let's start with the <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> with the helper example from <a href="lights.html">the lights article</a>.</p>
  216. <p>The first thing we need to do is turn on shadows in the renderer.</p>
  217. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  218. +renderer.shadowMap.enabled = true;
  219. </pre>
  220. <p>Then we also need to tell the light to cast a shadow</p>
  221. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const light = new THREE.DirectionalLight(color, intensity);
  222. +light.castShadow = true;
  223. </pre>
  224. <p>We also need to go to each mesh in the scene and decide if it should
  225. both cast shadows and/or receive shadows.</p>
  226. <p>Let's make the plane (the ground) only receive shadows since we don't
  227. really care what happens underneath.</p>
  228. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(planeGeo, planeMat);
  229. mesh.receiveShadow = true;
  230. </pre>
  231. <p>For the cube and the sphere let's have them both receive and cast shadows</p>
  232. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  233. mesh.castShadow = true;
  234. mesh.receiveShadow = true;
  235. ...
  236. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  237. mesh.castShadow = true;
  238. mesh.receiveShadow = true;
  239. </pre>
  240. <p>And then we run it.</p>
  241. <p></p><div translate="no" class="threejs_example_container notranslate">
  242. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light.html"></iframe></div>
  243. <a class="threejs_center" href="/manual/examples/shadows-directional-light.html" target="_blank">click here to open in a separate window</a>
  244. </div>
  245. <p></p>
  246. <p>What happened? Why are parts of the shadows missing?</p>
  247. <p>The reason is shadow maps are created by rendering the scene from the point
  248. of view of the light. In this case there is a camera at the <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>
  249. that is looking at its target. Just like <a href="cameras.html">the camera's we previously covered</a>
  250. the light's shadow camera defines an area inside of which
  251. the shadows get rendered. In the example above that area is too small.</p>
  252. <p>In order to visualize that area we can get the light's shadow camera and add
  253. a <a href="/docs/#api/en/helpers/CameraHelper"><code class="notranslate" translate="no">CameraHelper</code></a> to the scene.</p>
  254. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  255. scene.add(cameraHelper);
  256. </pre>
  257. <p>And now you can see the area for which shadows are cast and received.</p>
  258. <p></p><div translate="no" class="threejs_example_container notranslate">
  259. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light-with-camera-helper.html"></iframe></div>
  260. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-helper.html" target="_blank">click here to open in a separate window</a>
  261. </div>
  262. <p></p>
  263. <p>Adjust the target x value back and forth and it should be pretty clear that only
  264. what's inside the light's shadow camera box is where shadows are drawn.</p>
  265. <p>We can adjust the size of that box by adjusting the light's shadow camera.</p>
  266. <p>Let's add some GUI setting to adjust the light's shadow camera box. Since a
  267. <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> represents light all going in a parallel direction, the
  268. <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> uses an <a href="/docs/#api/en/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> for its shadow camera.
  269. We went over how an <a href="/docs/#api/en/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> works in <a href="cameras.html">the previous article about cameras</a>.</p>
  270. <p>Recall an <a href="/docs/#api/en/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> defines
  271. its box or <em>view frustum</em> by its <code class="notranslate" translate="no">left</code>, <code class="notranslate" translate="no">right</code>, <code class="notranslate" translate="no">top</code>, <code class="notranslate" translate="no">bottom</code>, <code class="notranslate" translate="no">near</code>, <code class="notranslate" translate="no">far</code>,
  272. and <code class="notranslate" translate="no">zoom</code> properties.</p>
  273. <p>Again let's make a helper class for the lil-gui. We'll make a <code class="notranslate" translate="no">DimensionGUIHelper</code>
  274. that we'll pass an object and 2 properties. It will present one property that lil-gui
  275. can adjust and in response will set the two properties one positive and one negative.
  276. We can use this to set <code class="notranslate" translate="no">left</code> and <code class="notranslate" translate="no">right</code> as <code class="notranslate" translate="no">width</code> and <code class="notranslate" translate="no">up</code> and <code class="notranslate" translate="no">down</code> as <code class="notranslate" translate="no">height</code>.</p>
  277. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class DimensionGUIHelper {
  278. constructor(obj, minProp, maxProp) {
  279. this.obj = obj;
  280. this.minProp = minProp;
  281. this.maxProp = maxProp;
  282. }
  283. get value() {
  284. return this.obj[this.maxProp] * 2;
  285. }
  286. set value(v) {
  287. this.obj[this.maxProp] = v / 2;
  288. this.obj[this.minProp] = v / -2;
  289. }
  290. }
  291. </pre>
  292. <p>We'll also use the <code class="notranslate" translate="no">MinMaxGUIHelper</code> we created in the <a href="cameras.html">camera article</a>
  293. to adjust <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code>.</p>
  294. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  295. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  296. gui.add(light, 'intensity', 0, 2, 0.01);
  297. +{
  298. + const folder = gui.addFolder('Shadow Camera');
  299. + folder.open();
  300. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'left', 'right'), 'value', 1, 100)
  301. + .name('width')
  302. + .onChange(updateCamera);
  303. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'bottom', 'top'), 'value', 1, 100)
  304. + .name('height')
  305. + .onChange(updateCamera);
  306. + const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  307. + folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  308. + folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  309. + folder.add(light.shadow.camera, 'zoom', 0.01, 1.5, 0.01).onChange(updateCamera);
  310. +}
  311. </pre>
  312. <p>We tell the GUI to call our <code class="notranslate" translate="no">updateCamera</code> function anytime anything changes.
  313. Let's write that function to update the light, the helper for the light, the
  314. light's shadow camera, and the helper showing the light's shadow camera.</p>
  315. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateCamera() {
  316. // update the light target's matrixWorld because it's needed by the helper
  317. light.target.updateMatrixWorld();
  318. helper.update();
  319. // update the light's shadow camera's projection matrix
  320. light.shadow.camera.updateProjectionMatrix();
  321. // and now update the camera helper we're using to show the light's shadow camera
  322. cameraHelper.update();
  323. }
  324. updateCamera();
  325. </pre>
  326. <p>And now that we've given the light's shadow camera a GUI we can play with the values.</p>
  327. <p></p><div translate="no" class="threejs_example_container notranslate">
  328. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light-with-camera-gui.html"></iframe></div>
  329. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-gui.html" target="_blank">click here to open in a separate window</a>
  330. </div>
  331. <p></p>
  332. <p>Set the <code class="notranslate" translate="no">width</code> and <code class="notranslate" translate="no">height</code> to about 30 and you can see the shadows are correct
  333. and the areas that need to be in shadow for this scene are entirely covered.</p>
  334. <p>But this brings up the question, why not just set <code class="notranslate" translate="no">width</code> and <code class="notranslate" translate="no">height</code> to some
  335. giant numbers to just cover everything? Set the <code class="notranslate" translate="no">width</code> and <code class="notranslate" translate="no">height</code> to 100
  336. and you might see something like this</p>
  337. <div class="threejs_center"><img src="../resources/images/low-res-shadow-map.png" style="width: 369px"></div>
  338. <p>What's going on with these low-res shadows?!</p>
  339. <p>This issue is yet another shadow related setting to be aware of.
  340. Shadow maps are textures the shadows get drawn into.
  341. Those textures have a size. The shadow camera's area we set above is stretched
  342. across that size. That means the larger area you set, the more blocky your shadows will
  343. be.</p>
  344. <p>You can set the resolution of the shadow map's texture by setting <code class="notranslate" translate="no">light.shadow.mapSize.width</code>
  345. and <code class="notranslate" translate="no">light.shadow.mapSize.height</code>. They default to 512x512.
  346. The larger you make them the more memory they take and the slower they are to compute so you want
  347. to set them as small as you can and still make your scene work. The same is true with the
  348. light's shadow camera area. Smaller means better looking shadows so make the area as small as you
  349. can and still cover your scene. Be aware that each user's machine has a maximum texture size
  350. allowed which is available on the renderer as <a href="/docs/#api/en/renderers/WebGLRenderer#capabilities"><code class="notranslate" translate="no">renderer.capabilities.maxTextureSize</code></a>.</p>
  351. <!--
  352. Ok but what about `near` and `far` I hear you thinking. Can we set `near` to 0.00001 and far to `100000000`
  353. -->
  354. <p>Switching to the <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> the light's shadow camera becomes a <a href="/docs/#api/en/cameras/PerspectiveCamera"><code class="notranslate" translate="no">PerspectiveCamera</code></a>. Unlike the <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>'s shadow camera
  355. where we could manually set most its settings, <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>'s shadow camera is controlled by the <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> itself. The <code class="notranslate" translate="no">fov</code> for the shadow
  356. camera is directly connected to the <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>'s <code class="notranslate" translate="no">angle</code> setting.
  357. The <code class="notranslate" translate="no">aspect</code> is set automatically based on the size of the shadow map.</p>
  358. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.DirectionalLight(color, intensity);
  359. +const light = new THREE.SpotLight(color, intensity);
  360. </pre>
  361. <p>and we added back in the <code class="notranslate" translate="no">penumbra</code> and <code class="notranslate" translate="no">angle</code> settings
  362. from our <a href="lights.html">article about lights</a>.</p>
  363. <p></p><div translate="no" class="threejs_example_container notranslate">
  364. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-spot-light-with-camera-gui.html"></iframe></div>
  365. <a class="threejs_center" href="/manual/examples/shadows-spot-light-with-camera-gui.html" target="_blank">click here to open in a separate window</a>
  366. </div>
  367. <p></p>
  368. <!--
  369. You can notice, just like the last example if we set the angle high
  370. then the shadow map, the texture is spread over a very large area and
  371. the resolution of our shadows gets really low.
  372. div class="threejs_center"><img src="../resources/images/low-res-shadow-map-spotlight.png" style="width: 344px"></div>
  373. You can increase the size of the shadow map as mentioned above. You can
  374. also blur the result
  375. <div translate="no" class="threejs_example_container notranslate">
  376. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-spot-light-with-shadow-radius"></iframe></div>
  377. <a class="threejs_center" href="/manual/examples/shadows-spot-light-with-shadow-radius" target="_blank">click here to open in a separate window</a>
  378. </div>
  379. -->
  380. <p>And finally there's shadows with a <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>. Since a <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>
  381. shines in all directions the only relevant settings are <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code>.
  382. Otherwise the <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> shadow is effectively 6 <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> shadows
  383. each one pointing to the face of a cube around the light. This means
  384. <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> shadows are much slower since the entire scene must be
  385. drawn 6 times, one for each direction.</p>
  386. <p>Let's put a box around our scene so we can see shadows on the walls
  387. and ceiling. We'll set the material's <code class="notranslate" translate="no">side</code> property to <code class="notranslate" translate="no">THREE.BackSide</code>
  388. so we render the inside of the box instead of the outside. Like the floor
  389. we'll set it only to receive shadows. Also we'll set the position of the
  390. box so its bottom is slightly below the floor so the floor and the bottom
  391. of the box don't z-fight.</p>
  392. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  393. const cubeSize = 30;
  394. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  395. const cubeMat = new THREE.MeshPhongMaterial({
  396. color: '#CCC',
  397. side: THREE.BackSide,
  398. });
  399. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  400. mesh.receiveShadow = true;
  401. mesh.position.set(0, cubeSize / 2 - 0.1, 0);
  402. scene.add(mesh);
  403. }
  404. </pre>
  405. <p>And of course we need to switch the light to a <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>.</p>
  406. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.SpotLight(color, intensity);
  407. +const light = new THREE.PointLight(color, intensity);
  408. ....
  409. // so we can easily see where the point light is
  410. +const helper = new THREE.PointLightHelper(light);
  411. +scene.add(helper);
  412. </pre>
  413. <p></p><div translate="no" class="threejs_example_container notranslate">
  414. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-point-light.html"></iframe></div>
  415. <a class="threejs_center" href="/manual/examples/shadows-point-light.html" target="_blank">click here to open in a separate window</a>
  416. </div>
  417. <p></p>
  418. <p>Use the <code class="notranslate" translate="no">position</code> GUI settings to move the light around
  419. and you'll see the shadows fall on all the walls. You can
  420. also adjust <code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code> settings and see just like
  421. the other shadows when things are closer than <code class="notranslate" translate="no">near</code> they
  422. no longer receive a shadow and they are further than <code class="notranslate" translate="no">far</code>
  423. they are always in shadow.</p>
  424. <!--
  425. self shadow, shadow acne
  426. -->
  427. </div>
  428. </div>
  429. </div>
  430. <script src="../resources/prettify.js"></script>
  431. <script src="../resources/lesson.js"></script>
  432. </body></html>