lights.html 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Lights</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 – Lights">
  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>Lights</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 and also the article on <a href="setup.html">setting up your environment</a>. The
  32. <a href="textures.html">previous article was about textures</a>.</p>
  33. <p>Let's go over how to use the various kinds of lights in three.</p>
  34. <p>Starting with one of our previous samples let's update the camera.
  35. We'll set the field of view to 45 degrees, the far plane to 100 units,
  36. and we'll move the camera 10 units up and 20 units back from the origin</p>
  37. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">*const fov = 45;
  38. const aspect = 2; // the canvas default
  39. const near = 0.1;
  40. *const far = 100;
  41. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  42. +camera.position.set(0, 10, 20);
  43. </pre>
  44. <p>Next let's add <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> let the user spin
  45. or <em>orbit</em> the camera around some point. The <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> are
  46. an optional feature of three.js so first we need to include them
  47. in our page</p>
  48. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  49. +import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  50. </pre>
  51. <p>Then we can use them. We pass the <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> a camera to
  52. control and the DOM element to use to get input events</p>
  53. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const controls = new OrbitControls(camera, canvas);
  54. controls.target.set(0, 5, 0);
  55. controls.update();
  56. </pre>
  57. <p>We also set the target to orbit around to 5 units above the origin
  58. and then call <code class="notranslate" translate="no">controls.update</code> so the controls will use the new
  59. target.</p>
  60. <p>Next up let's make some things to light up. First we'll make ground
  61. plane. We'll apply a tiny 2x2 pixel checkerboard texture that looks
  62. like this</p>
  63. <div class="threejs_center">
  64. <img src="../examples/resources/images/checker.png" class="border" style="
  65. image-rendering: pixelated;
  66. width: 128px;
  67. ">
  68. </div>
  69. <p>First we load the texture, set it to repeating, set the filtering to
  70. nearest, and set how many times we want it to repeat. Since the
  71. texture is a 2x2 pixel checkerboard, by repeating and setting the
  72. repeat to half the size of the plane each check on the checkerboard
  73. will be exactly 1 unit large;</p>
  74. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 40;
  75. const loader = new THREE.TextureLoader();
  76. const texture = loader.load('resources/images/checker.png');
  77. texture.wrapS = THREE.RepeatWrapping;
  78. texture.wrapT = THREE.RepeatWrapping;
  79. texture.magFilter = THREE.NearestFilter;
  80. texture.colorSpace = THREE.SRGBColorSpace;
  81. const repeats = planeSize / 2;
  82. texture.repeat.set(repeats, repeats);
  83. </pre>
  84. <p>We then make a plane geometry, a material for the plane, and a mesh
  85. to insert it in the scene. Planes default to being in the XY plane
  86. but the ground is in the XZ plane so we rotate it.</p>
  87. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  88. const planeMat = new THREE.MeshPhongMaterial({
  89. map: texture,
  90. side: THREE.DoubleSide,
  91. });
  92. const mesh = new THREE.Mesh(planeGeo, planeMat);
  93. mesh.rotation.x = Math.PI * -.5;
  94. scene.add(mesh);
  95. </pre>
  96. <p>Let's add a cube and a sphere so we have 3 things to light including the plane</p>
  97. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  98. const cubeSize = 4;
  99. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  100. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  101. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  102. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  103. scene.add(mesh);
  104. }
  105. {
  106. const sphereRadius = 3;
  107. const sphereWidthDivisions = 32;
  108. const sphereHeightDivisions = 16;
  109. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  110. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  111. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  112. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  113. scene.add(mesh);
  114. }
  115. </pre>
  116. <p>Now that we have a scene to light up let's add lights!</p>
  117. <h2 id="-ambientlight-"><a href="/docs/#api/en/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a></h2>
  118. <p>First let's make an <a href="/docs/#api/en/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a></p>
  119. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  120. const intensity = 1;
  121. const light = new THREE.AmbientLight(color, intensity);
  122. scene.add(light);
  123. </pre>
  124. <p>Let's also make it so we can adjust the light's parameters.
  125. We'll use <a href="https://github.com/georgealways/lil-gui">lil-gui</a> again.
  126. To be able to adjust the color via lil-gui we need a small helper
  127. that presents a property to lil-gui that looks like a CSS hex color string
  128. (eg: <code class="notranslate" translate="no">#FF8844</code>). Our helper will get the color from a named property,
  129. convert it to a hex string to offer to lil-gui. When lil-gui tries
  130. to set the helper's property we'll assign the result back to the light's
  131. color.</p>
  132. <p>Here's the helper:</p>
  133. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ColorGUIHelper {
  134. constructor(object, prop) {
  135. this.object = object;
  136. this.prop = prop;
  137. }
  138. get value() {
  139. return `#${this.object[this.prop].getHexString()}`;
  140. }
  141. set value(hexString) {
  142. this.object[this.prop].set(hexString);
  143. }
  144. }
  145. </pre>
  146. <p>And here's our code setting up lil-gui</p>
  147. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  148. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  149. gui.add(light, 'intensity', 0, 2, 0.01);
  150. </pre>
  151. <p>And here's the result</p>
  152. <p></p><div translate="no" class="threejs_example_container notranslate">
  153. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-ambient.html"></iframe></div>
  154. <a class="threejs_center" href="/manual/examples/lights-ambient.html" target="_blank">click here to open in a separate window</a>
  155. </div>
  156. <p></p>
  157. <p>Click and drag in the scene to <em>orbit</em> the camera.</p>
  158. <p>Notice there is no definition. The shapes are flat. The <a href="/docs/#api/en/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a> effectively
  159. just multiplies the material's color by the light's color times the
  160. intensity.</p>
  161. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">color = materialColor * light.color * light.intensity;
  162. </pre><p>That's it. It has no direction.
  163. This style of ambient lighting is actually not all that
  164. useful as lighting as it's 100% even so other than changing the color
  165. of everything in the scene it doesn't look much like <em>lighting</em>.
  166. What it does help with is making the darks not too dark.</p>
  167. <h2 id="-hemispherelight-"><a href="/docs/#api/en/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a></h2>
  168. <p>Let's switch the code to a <a href="/docs/#api/en/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>. A <a href="/docs/#api/en/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>
  169. takes a sky color and a ground color and just multiplies the
  170. material's color between those 2 colors—the sky color if the
  171. surface of the object is pointing up and the ground color if
  172. the surface of the object is pointing down.</p>
  173. <p>Here's the new code</p>
  174. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const color = 0xFFFFFF;
  175. +const skyColor = 0xB1E1FF; // light blue
  176. +const groundColor = 0xB97A20; // brownish orange
  177. const intensity = 1;
  178. -const light = new THREE.AmbientLight(color, intensity);
  179. +const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  180. scene.add(light);
  181. </pre>
  182. <p>Let's also update the lil-gui code to edit both colors</p>
  183. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  184. -gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  185. +gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('skyColor');
  186. +gui.addColor(new ColorGUIHelper(light, 'groundColor'), 'value').name('groundColor');
  187. gui.add(light, 'intensity', 0, 2, 0.01);
  188. </pre>
  189. <p>The result:</p>
  190. <p></p><div translate="no" class="threejs_example_container notranslate">
  191. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-hemisphere.html"></iframe></div>
  192. <a class="threejs_center" href="/manual/examples/lights-hemisphere.html" target="_blank">click here to open in a separate window</a>
  193. </div>
  194. <p></p>
  195. <p>Notice again there is almost no definition, everything looks kind
  196. of flat. The <a href="/docs/#api/en/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> used in combination with another light
  197. can help give a nice kind of influence of the color of the sky
  198. and ground. In that way it's best used in combination with some
  199. other light or a substitute for an <a href="/docs/#api/en/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>.</p>
  200. <h2 id="-directionallight-"><a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a></h2>
  201. <p>Let's switch the code to a <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>.
  202. A <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> is often used to represent the sun.</p>
  203. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  204. const intensity = 1;
  205. const light = new THREE.DirectionalLight(color, intensity);
  206. light.position.set(0, 10, 0);
  207. light.target.position.set(-5, 0, 0);
  208. scene.add(light);
  209. scene.add(light.target);
  210. </pre>
  211. <p>Notice that we had to add the <code class="notranslate" translate="no">light</code> and the <code class="notranslate" translate="no">light.target</code>
  212. to the scene. A three.js <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> will shine
  213. in the direction of its target.</p>
  214. <p>Let's make it so we can move the target by adding it to
  215. our GUI.</p>
  216. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  217. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  218. gui.add(light, 'intensity', 0, 2, 0.01);
  219. gui.add(light.target.position, 'x', -10, 10);
  220. gui.add(light.target.position, 'z', -10, 10);
  221. gui.add(light.target.position, 'y', 0, 10);
  222. </pre>
  223. <p></p><div translate="no" class="threejs_example_container notranslate">
  224. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional.html"></iframe></div>
  225. <a class="threejs_center" href="/manual/examples/lights-directional.html" target="_blank">click here to open in a separate window</a>
  226. </div>
  227. <p></p>
  228. <p>It's kind of hard to see what's going on. Three.js has a bunch
  229. of helper objects we can add to our scene to help visualize
  230. invisible parts of a scene. In this case we'll use the
  231. <a href="/docs/#api/en/helpers/DirectionalLightHelper"><code class="notranslate" translate="no">DirectionalLightHelper</code></a> which will draw a plane, to represent
  232. the light, and a line from the light to the target. We just
  233. pass it the light and add it to the scene.</p>
  234. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const helper = new THREE.DirectionalLightHelper(light);
  235. scene.add(helper);
  236. </pre>
  237. <p>While we're at it let's make it so we can set both the position
  238. of the light and the target. To do this we'll make a function
  239. that given a <a href="/docs/#api/en/math/Vector3"><code class="notranslate" translate="no">Vector3</code></a> will adjust its <code class="notranslate" translate="no">x</code>, <code class="notranslate" translate="no">y</code>, and <code class="notranslate" translate="no">z</code> properties
  240. using <code class="notranslate" translate="no">lil-gui</code>.</p>
  241. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeXYZGUI(gui, vector3, name, onChangeFn) {
  242. const folder = gui.addFolder(name);
  243. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  244. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  245. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  246. folder.open();
  247. }
  248. </pre>
  249. <p>Note that we need to call the helper's <code class="notranslate" translate="no">update</code> function
  250. anytime we change something so the helper knows to update
  251. itself. As such we pass in an <code class="notranslate" translate="no">onChangeFn</code> function to
  252. get called anytime lil-gui updates a value.</p>
  253. <p>Then we can use that for both the light's position
  254. and the target's position like this</p>
  255. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function updateLight() {
  256. + light.target.updateMatrixWorld();
  257. + helper.update();
  258. +}
  259. +updateLight();
  260. const gui = new GUI();
  261. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  262. gui.add(light, 'intensity', 0, 2, 0.01);
  263. +makeXYZGUI(gui, light.position, 'position', updateLight);
  264. +makeXYZGUI(gui, light.target.position, 'target', updateLight);
  265. </pre>
  266. <p>Now we can move the light, and its target</p>
  267. <p></p><div translate="no" class="threejs_example_container notranslate">
  268. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional-w-helper.html"></iframe></div>
  269. <a class="threejs_center" href="/manual/examples/lights-directional-w-helper.html" target="_blank">click here to open in a separate window</a>
  270. </div>
  271. <p></p>
  272. <p>Orbit the camera and it gets easier to see. The plane
  273. represents a <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> because a directional
  274. light computes light coming in one direction. There is no
  275. <em>point</em> the light comes from, it's an infinite plane of light
  276. shooting out parallel rays of light.</p>
  277. <h2 id="-pointlight-"><a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a></h2>
  278. <p>A <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> is a light that sits at a point and shoots light
  279. in all directions from that point. Let's change the code.</p>
  280. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  281. -const intensity = 1;
  282. +const intensity = 150;
  283. -const light = new THREE.DirectionalLight(color, intensity);
  284. +const light = new THREE.PointLight(color, intensity);
  285. light.position.set(0, 10, 0);
  286. -light.target.position.set(-5, 0, 0);
  287. scene.add(light);
  288. -scene.add(light.target);
  289. </pre>
  290. <p>Let's also switch to a <a href="/docs/#api/en/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a></p>
  291. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const helper = new THREE.DirectionalLightHelper(light);
  292. +const helper = new THREE.PointLightHelper(light);
  293. scene.add(helper);
  294. </pre>
  295. <p>and as there is no target the <code class="notranslate" translate="no">onChange</code> function can be simpler.</p>
  296. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateLight() {
  297. - light.target.updateMatrixWorld();
  298. helper.update();
  299. }
  300. -updateLight();
  301. </pre>
  302. <p>Note that at some level a <a href="/docs/#api/en/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a> has no um, point.
  303. It just draws a small wireframe diamond. It could just as easily
  304. be any shape you want, just add a mesh to the light itself.</p>
  305. <p>A <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> has the added property of <a href="/docs/#api/en/lights/PointLight#distance"><code class="notranslate" translate="no">distance</code></a>.
  306. If the <code class="notranslate" translate="no">distance</code> is 0 then the <a href="/docs/#api/en/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> shines to
  307. infinity. If the <code class="notranslate" translate="no">distance</code> is greater than 0 then the light shines
  308. its full intensity at the light and fades to no influence at <code class="notranslate" translate="no">distance</code>
  309. units away from the light.</p>
  310. <p>Let's setup the GUI so we can adjust the distance.</p>
  311. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  312. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  313. gui.add(light, 'intensity', 0, 2, 0.01);
  314. +gui.add(light, 'distance', 0, 40).onChange(updateLight);
  315. makeXYZGUI(gui, light.position, 'position', updateLight);
  316. -makeXYZGUI(gui, light.target.position, 'target', updateLight);
  317. </pre>
  318. <p>And now try it out.</p>
  319. <p></p><div translate="no" class="threejs_example_container notranslate">
  320. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point.html"></iframe></div>
  321. <a class="threejs_center" href="/manual/examples/lights-point.html" target="_blank">click here to open in a separate window</a>
  322. </div>
  323. <p></p>
  324. <p>Notice when <code class="notranslate" translate="no">distance</code> is &gt; 0 how the light fades out.</p>
  325. <h2 id="-spotlight-"><a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a></h2>
  326. <p>Spotlights are effectively a point light with a cone
  327. attached where the light only shines inside the cone.
  328. There's actually 2 cones. An outer cone and an inner
  329. cone. Between the inner cone and the outer cone the
  330. light fades from full intensity to zero.</p>
  331. <p>To use a <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> we need a target just like
  332. the directional light. The light's cone will
  333. open toward the target.</p>
  334. <p>Modifying our <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> with helper from above</p>
  335. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  336. -const intensity = 1;
  337. +const intensity = 150;
  338. -const light = new THREE.DirectionalLight(color, intensity);
  339. +const light = new THREE.SpotLight(color, intensity);
  340. scene.add(light);
  341. scene.add(light.target);
  342. -const helper = new THREE.DirectionalLightHelper(light);
  343. +const helper = new THREE.SpotLightHelper(light);
  344. scene.add(helper);
  345. </pre>
  346. <p>The spotlight's cone's angle is set with the <a href="/docs/#api/en/lights/SpotLight#angle"><code class="notranslate" translate="no">angle</code></a>
  347. property in radians. We'll use our <code class="notranslate" translate="no">DegRadHelper</code> from the
  348. <a href="textures.html">texture article</a> to present a UI in
  349. degrees.</p>
  350. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(new DegRadHelper(light, 'angle'), 'value', 0, 90).name('angle').onChange(updateLight);
  351. </pre>
  352. <p>The inner cone is defined by setting the <a href="/docs/#api/en/lights/SpotLight#penumbra"><code class="notranslate" translate="no">penumbra</code></a> property
  353. as a percentage from the outer cone. In other words when <code class="notranslate" translate="no">penumbra</code> is 0 then the
  354. inner cone is the same size (0 = no difference) from the outer cone. When the
  355. <code class="notranslate" translate="no">penumbra</code> is 1 then the light fades starting in the center of the cone to the
  356. outer cone. When <code class="notranslate" translate="no">penumbra</code> is .5 then the light fades starting from 50% between
  357. the center of the outer cone.</p>
  358. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(light, 'penumbra', 0, 1, 0.01);
  359. </pre>
  360. <p></p><div translate="no" class="threejs_example_container notranslate">
  361. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-spot-w-helper.html"></iframe></div>
  362. <a class="threejs_center" href="/manual/examples/lights-spot-w-helper.html" target="_blank">click here to open in a separate window</a>
  363. </div>
  364. <p></p>
  365. <p>Notice with the default <code class="notranslate" translate="no">penumbra</code> of 0 the spotlight has a very sharp edge
  366. whereas as you adjust the <code class="notranslate" translate="no">penumbra</code> toward 1 the edge blurs.</p>
  367. <p>It might be hard to see the <em>cone</em> of the spotlight. The reason is it's
  368. below the ground. Shorten the distance to around 5 and you'll see the open
  369. end of the cone.</p>
  370. <h2 id="-rectarealight-"><a href="/docs/#api/en/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a></h2>
  371. <p>There's one more type of light, the <a href="/docs/#api/en/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>, which represents
  372. exactly what it sounds like, a rectangular area of light like a long
  373. fluorescent light or maybe a frosted sky light in a ceiling.</p>
  374. <p>The <a href="/docs/#api/en/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> only works with the <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> and the
  375. <a href="/docs/#api/en/materials/MeshPhysicalMaterial"><code class="notranslate" translate="no">MeshPhysicalMaterial</code></a> so let's change all our materials to <a href="/docs/#api/en/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a></p>
  376. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> ...
  377. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  378. - const planeMat = new THREE.MeshPhongMaterial({
  379. + const planeMat = new THREE.MeshStandardMaterial({
  380. map: texture,
  381. side: THREE.DoubleSide,
  382. });
  383. const mesh = new THREE.Mesh(planeGeo, planeMat);
  384. mesh.rotation.x = Math.PI * -.5;
  385. scene.add(mesh);
  386. }
  387. {
  388. const cubeSize = 4;
  389. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  390. - const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  391. + const cubeMat = new THREE.MeshStandardMaterial({color: '#8AC'});
  392. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  393. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  394. scene.add(mesh);
  395. }
  396. {
  397. const sphereRadius = 3;
  398. const sphereWidthDivisions = 32;
  399. const sphereHeightDivisions = 16;
  400. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  401. - const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  402. + const sphereMat = new THREE.MeshStandardMaterial({color: '#CA8'});
  403. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  404. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  405. scene.add(mesh);
  406. }
  407. </pre>
  408. <p>To use the <a href="/docs/#api/en/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> we need to include some extra three.js optional data and we'll
  409. include the <a href="/docs/#api/en/helpers/RectAreaLightHelper"><code class="notranslate" translate="no">RectAreaLightHelper</code></a> to help us visualize the light</p>
  410. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  411. +import {RectAreaLightUniformsLib} from 'three/addons/lights/RectAreaLightUniformsLib.js';
  412. +import {RectAreaLightHelper} from 'three/addons/helpers/RectAreaLightHelper.js';
  413. </pre>
  414. <p>and we need to call <code class="notranslate" translate="no">RectAreaLightUniformsLib.init</code></p>
  415. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  416. const canvas = document.querySelector('#c');
  417. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  418. + RectAreaLightUniformsLib.init();
  419. </pre>
  420. <p>If you forget the data the light will still work but it will look funny so
  421. be sure to remember to include the extra data.</p>
  422. <p>Now we can create the light</p>
  423. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  424. *const intensity = 5;
  425. +const width = 12;
  426. +const height = 4;
  427. *const light = new THREE.RectAreaLight(color, intensity, width, height);
  428. light.position.set(0, 10, 0);
  429. +light.rotation.x = THREE.MathUtils.degToRad(-90);
  430. scene.add(light);
  431. *const helper = new RectAreaLightHelper(light);
  432. *light.add(helper);
  433. </pre>
  434. <p>One thing to notice is that unlike the <a href="/docs/#api/en/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> and the <a href="/docs/#api/en/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>, the
  435. <a href="/docs/#api/en/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> does not use a target. It just uses its rotation. Another thing
  436. to notice is the helper needs to be a child of the light. It is not a child of the
  437. scene like other helpers.</p>
  438. <p>Let's also adjust the GUI. We'll make it so we can rotate the light and adjust
  439. its <code class="notranslate" translate="no">width</code> and <code class="notranslate" translate="no">height</code></p>
  440. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  441. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  442. gui.add(light, 'intensity', 0, 10, 0.01);
  443. gui.add(light, 'width', 0, 20);
  444. gui.add(light, 'height', 0, 20);
  445. gui.add(new DegRadHelper(light.rotation, 'x'), 'value', -180, 180).name('x rotation');
  446. gui.add(new DegRadHelper(light.rotation, 'y'), 'value', -180, 180).name('y rotation');
  447. gui.add(new DegRadHelper(light.rotation, 'z'), 'value', -180, 180).name('z rotation');
  448. makeXYZGUI(gui, light.position, 'position');
  449. </pre>
  450. <p>And here is that.</p>
  451. <p></p><div translate="no" class="threejs_example_container notranslate">
  452. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-rectarea.html"></iframe></div>
  453. <a class="threejs_center" href="/manual/examples/lights-rectarea.html" target="_blank">click here to open in a separate window</a>
  454. </div>
  455. <p></p>
  456. <p>It's important to note each light you add to the scene slows down how fast
  457. three.js renders the scene so you should always try to use as few as
  458. possible to achieve your goals.</p>
  459. <p>Next up let's go over <a href="cameras.html">dealing with cameras</a>.</p>
  460. <p><canvas id="c"></canvas></p>
  461. <script type="module" src="../resources/threejs-lights.js"></script>
  462. </div>
  463. </div>
  464. </div>
  465. <script src="../resources/prettify.js"></script>
  466. <script src="../resources/lesson.js"></script>
  467. </body></html>