lights.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <!DOCTYPE html><html lang="ko"><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. <link rel="stylesheet" href="/manual/ko/lang.css">
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="lesson-title">
  25. <h1>조명(Lights)</h1>
  26. </div>
  27. <div class="lesson">
  28. <div class="lesson-main">
  29. <p>※ 이 글은 Three.js의 튜토리얼 시리즈로서,
  30. 먼저 <a href="fundamentals.html">Three.js의 기본 구조에 관한 글</a>과
  31. <a href="setup.html">개발 환경 설정하는 법</a>을 읽고 오길 권장합니다.</p>
  32. <p>이전 글은 <a href="textures.html">텍스처에 관한 글</a>이었죠. 이번에는
  33. Three.js의 다양한 조명을 어떻게 쓰는지 알아보겠습니다.</p>
  34. <p>먼저 이전 예제에서 카메라를 수정하겠습니다. 시야각(fov, field of view)은
  35. 45도, <code class="notranslate" translate="no">far</code>면은 100칸, 카메라의 위치는 중점에서 위로 10칸, 뒤로 20칸 옮깁니다.</p>
  36. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">*const fov = 45;
  37. const aspect = 2; // canvas 요소의 기본 비율
  38. const near = 0.1;
  39. *const far = 100;
  40. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  41. +camera.position.set(0, 10, 20);
  42. </pre>
  43. <p>다음으로 <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>는 특정 좌표를
  44. 중심으로 카메라를 자전 또는 <em>공전(orbit)</em>하도록 해줍니다. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>는
  45. 별도 모듈이므로, 먼저 페이지에 로드해야 합니다.</p>
  46. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  47. +import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  48. </pre>
  49. <p>이제 <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>에 카메라와, DOM 이벤트를 감지할 수 있도록
  50. canvas 요소를 넘겨줍니다.</p>
  51. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const controls = new OrbitControls(camera, canvas);
  52. controls.target.set(0, 5, 0);
  53. controls.update();
  54. </pre>
  55. <p>또한 시점을 중점에서 위로 5칸 올린 후 <code class="notranslate" translate="no">controls.update</code> 메서드를
  56. 호출해 <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>가 새로운 시점을 바라보게 합니다.</p>
  57. <p>다음으로 빛을 받을 무언가를 만들어보겠습니다. 먼저 땅의 역할을 할
  58. 평면을 만들고, 평면에 2x2 픽셀의 체크판 텍스처를 씌우겠습니다.</p>
  59. <div class="threejs_center">
  60. <img src="../examples/resources/images/checker.png" class="border" style="
  61. image-rendering: pixelated;
  62. width: 128px;
  63. ">
  64. </div>
  65. <p>일단 텍스처를 불러온 뒤, 반복하도록 래핑(wrapping)을 설정해줍니다. 필터는
  66. <code class="notranslate" translate="no">NearestFilter</code>, 텍스처가 2x2 픽셀의 체크판이니 <code class="notranslate" translate="no">repeat</code> 속성을 평면의
  67. 반으로 설정하면 체크판의 각 칸은 정확히 (장면의) 1칸이 될 겁니다.</p>
  68. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 40;
  69. const loader = new THREE.TextureLoader();
  70. const texture = loader.load('resources/images/checker.png');
  71. texture.wrapS = THREE.RepeatWrapping;
  72. texture.wrapT = THREE.RepeatWrapping;
  73. texture.magFilter = THREE.NearestFilter;
  74. texture.colorSpace = THREE.SRGBColorSpace;
  75. const repeats = planeSize / 2;
  76. texture.repeat.set(repeats, repeats);
  77. </pre>
  78. <p>그리고 평면 <code class="notranslate" translate="no">geometry</code>, 평면에 쓸 재질(material), 장면(scene)에 추가할
  79. <code class="notranslate" translate="no">mesh</code>를 만듭니다. 평면은 기본적으로 XY축을 기준으로 하니, XZ축을 기준으로
  80. 하려면 평면을 회전시켜야 합니다.</p>
  81. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  82. const planeMat = new THREE.MeshPhongMaterial({
  83. map: texture,
  84. side: THREE.DoubleSide,
  85. });
  86. const mesh = new THREE.Mesh(planeGeo, planeMat);
  87. mesh.rotation.x = Math.PI * -.5;
  88. scene.add(mesh);
  89. </pre>
  90. <p>정육면체와 구체도 추가해서 평면까지 총 3개의 물체를 추가하도록 하죠.</p>
  91. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  92. const cubeSize = 4;
  93. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  94. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  95. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  96. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  97. scene.add(mesh);
  98. }
  99. {
  100. const sphereRadius = 3;
  101. const sphereWidthDivisions = 32;
  102. const sphereHeightDivisions = 16;
  103. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  104. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  105. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  106. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  107. scene.add(mesh);
  108. }
  109. </pre>
  110. <p>빛을 받을 물체를 만들었으니 이제 조명을 가지고 놀아봅시다!</p>
  111. <h2 id="-ambientlight-"><a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a></h2>
  112. <p>먼저 <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>(자연광)를 써보겠습니다.</p>
  113. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  114. const intensity = 1;
  115. const light = new THREE.AmbientLight(color, intensity);
  116. scene.add(light);
  117. </pre>
  118. <p>이 조명도 <a href="https://github.com/georgealways/lil-gui">lil-gui</a>를 사용해
  119. 속성을 조정할 수 있도록 만들겠습니다. lil-gui로 색상을 조정하려면 간단한
  120. 헬퍼 클래스가 필요합니다. 이 클래스는 색상을 CSS hex(예: <code class="notranslate" translate="no">#FF8844</code>) 값으로
  121. 변경해 lil-gui에 넘겨주는 역할을 할 거예요. 그리고 lil-gui가 클래스의
  122. 속성을 지정할 때, 이를 조명에 직접 지정하도록 합니다.</p>
  123. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ColorGUIHelper {
  124. constructor(object, prop) {
  125. this.object = object;
  126. this.prop = prop;
  127. }
  128. get value() {
  129. return `#${this.object[this.prop].getHexString()}`;
  130. }
  131. set value(hexString) {
  132. this.object[this.prop].set(hexString);
  133. }
  134. }
  135. </pre>
  136. <p>아래는 lil-gui를 만드는 코드입니다.</p>
  137. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  138. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  139. gui.add(light, 'intensity', 0, 2, 0.01);
  140. </pre>
  141. <p>결과물은 다음과 같죠.</p>
  142. <p></p><div translate="no" class="threejs_example_container notranslate">
  143. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-ambient.html"></iframe></div>
  144. <a class="threejs_center" href="/manual/examples/lights-ambient.html" target="_blank">새 탭에서 보기</a>
  145. </div>
  146. <p></p>
  147. <p>카메라를 <em>공전시키기(orbit)</em> 위해 화면을 드래그해보세요.</p>
  148. <p>물체들이 평평하고, 윤곽이 뚜렷하지 않습니다. <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>는 물체와
  149. 조명의 색, 그리고 조명의 밝기를 곱한 것과 같죠.</p>
  150. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">color = materialColor * light.color * light.intensity;
  151. </pre><p>이게 전부입니다. <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>에는 방향이라는 개념이 없죠. 주변광은
  152. 완전히 고르게 적용되고 공간 안 물체의 색을 바꾸는 역할만 하기 때문에
  153. 실용적이지 않은데다 그다지 <em>조명</em>처럼 느껴지지도 않습니다. 어두운 장면을
  154. 덜 어둡게 만드는 정도에만 도움이 되죠.</p>
  155. <h2 id="-hemispherelight-"><a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a></h2>
  156. <p>조명을 <a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>(반구광)으로 바꾸겠습니다. <a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>는
  157. 천장과 바닥의 색을 인자로 받아, 물체의 천장을 바라보는 면은 천장 색, 바닥을
  158. 바라보는 면은 바닥 색으로 혼합합니다.</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const color = 0xFFFFFF;
  160. +const skyColor = 0xB1E1FF; // 하늘색
  161. +const groundColor = 0xB97A20; // 오렌지 브라운
  162. const intensity = 1;
  163. -const light = new THREE.AmbientLight(color, intensity);
  164. +const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  165. scene.add(light);
  166. </pre>
  167. <p>마찬가지로 lil-gui를 수정해 두 색상을 조정할 수 있도록 합니다.</p>
  168. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  169. -gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  170. +gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('skyColor');
  171. +gui.addColor(new ColorGUIHelper(light, 'groundColor'), 'value').name('groundColor');
  172. gui.add(light, 'intensity', 0, 2, 0.01);
  173. </pre>
  174. <p></p><div translate="no" class="threejs_example_container notranslate">
  175. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-hemisphere.html"></iframe></div>
  176. <a class="threejs_center" href="/manual/examples/lights-hemisphere.html" target="_blank">새 탭에서 보기</a>
  177. </div>
  178. <p></p>
  179. <p>이 또한 그다지 입체적이지 않습니다. 아까보다는 낮지만 전체적으로 2D처럼
  180. 보이네요. <a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>는 주로 풍경을 표현하거나 할 때 다른 조명과
  181. 함께 사용합니다. 다른 조명과 조합할 때 유용하고, 간단히는 <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>
  182. 대신 사용할 수 있죠.</p>
  183. <h2 id="-directionallight-"><a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a></h2>
  184. <p>이번에는 조명을 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>(직사광)로 바꿔보죠. <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>는
  185. 주로 태양을 표현할 때 사용합니다.</p>
  186. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  187. const intensity = 1;
  188. const light = new THREE.DirectionalLight(color, intensity);
  189. light.position.set(0, 10, 0);
  190. light.target.position.set(-5, 0, 0);
  191. scene.add(light);
  192. scene.add(light.target);
  193. </pre>
  194. <p>먼저 <code class="notranslate" translate="no">light</code>와 <code class="notranslate" translate="no">light.target</code>(목표)을 모두 장면에 추가해야 합니다.
  195. 그래야 Three.js의 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>가 목표가 있는 방향으로 빛을
  196. 쬘 테니까요.</p>
  197. <p>이 역시 GUI를 사용해 목표의 위치를 조정할 수 있도록 만들겠습니다.</p>
  198. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  199. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  200. gui.add(light, 'intensity', 0, 2, 0.01);
  201. gui.add(light.target.position, 'x', -10, 10);
  202. gui.add(light.target.position, 'z', -10, 10);
  203. gui.add(light.target.position, 'y', 0, 10);
  204. </pre>
  205. <p></p><div translate="no" class="threejs_example_container notranslate">
  206. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional.html"></iframe></div>
  207. <a class="threejs_center" href="/manual/examples/lights-directional.html" target="_blank">새 탭에서 보기</a>
  208. </div>
  209. <p></p>
  210. <p>조명의 위치가 보이지 않으니 정확한 동작을 확인하기가 좀 어렵네요.
  211. 다행히 Three.js에는 눈에 보이지 않는 요소의 시각화를 도와주는
  212. 다양한 헬퍼 객체가 있습니다. 이 경우 <a href="/docs/#api/ko/helpers/DirectionalLightHelper"><code class="notranslate" translate="no">DirectionalLightHelper</code></a>를
  213. 사용해 조명을 면으로, 조명의 방향을 선으로 나타낼 수 있습니다.
  214. 사용법도 간단해서 조명을 인자로 넘겨주고 생성한 인스턴스를 장면에
  215. 추가하면 됩니다.</p>
  216. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const helper = new THREE.DirectionalLightHelper(light);
  217. scene.add(helper);
  218. </pre>
  219. <p>하는 김에 조명과 목표 둘 다 위치를 조정할 수 있도록 하겠습니다.
  220. <a href="/docs/#api/ko/math/Vector3"><code class="notranslate" translate="no">Vector3</code></a> 객체를 인자로 받아, <code class="notranslate" translate="no">lil-gui</code>로 이 객체의 <code class="notranslate" translate="no">x</code>, <code class="notranslate" translate="no">y</code>,
  221. <code class="notranslate" translate="no">z</code> 속성을 조정하는 함수를 하나 만듭니다.</p>
  222. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeXYZGUI(gui, vector3, name, onChangeFn) {
  223. const folder = gui.addFolder(name);
  224. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  225. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  226. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  227. folder.open();
  228. }
  229. </pre>
  230. <p>헬퍼 객체를 사용할 때는 헬퍼 객체의 <code class="notranslate" translate="no">update</code> 메서드를 수동으로
  231. 호출해줘야 합니다. 한 예로 lil-gui가 객체 속성을 변경할 때마다
  232. 인자로 넘겨준 <code class="notranslate" translate="no">onChangeFn</code>에서 헬퍼 객체의 <code class="notranslate" translate="no">update</code> 메서드를
  233. 호출할 수 있죠.</p>
  234. <p>그리고 조명의 위치, 목표의 위치 객체에 방금 만든 함수를 각각 적용합니다.</p>
  235. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function updateLight() {
  236. + light.target.updateMatrixWorld();
  237. + helper.update();
  238. +}
  239. +updateLight();
  240. const gui = new GUI();
  241. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  242. gui.add(light, 'intensity', 0, 2, 0.01);
  243. +makeXYZGUI(gui, light.position, 'position', updateLight);
  244. +makeXYZGUI(gui, light.target.position, 'target', updateLight);
  245. </pre>
  246. <p>이제 조명, 목표의 위치를 각각 조정할 수 있습니다.</p>
  247. <p></p><div translate="no" class="threejs_example_container notranslate">
  248. <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>
  249. <a class="threejs_center" href="/manual/examples/lights-directional-w-helper.html" target="_blank">새 탭에서 보기</a>
  250. </div>
  251. <p></p>
  252. <p>카메라를 돌려보면 아까보다 훨씬 동작이 명확하게 보일 겁니다.
  253. 평면은 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>를 나타내는데, 이는 직사광이 어느
  254. <em>한 점</em>에서 뻗어나오는 조명이 아니기 때문입니다. 무한한 광원이
  255. 목표를 향해 평행하게 빛을 내리쬐는 것이죠.</p>
  256. <h2 id="-pointlight-"><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a></h2>
  257. <p><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>는 한 점에서 무한히 뻗어나가는 광원입니다. 코드를
  258. 다시 한 번 수정해보죠.</p>
  259. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  260. -const intensity = 1;
  261. +const intensity = 150;
  262. -const light = new THREE.DirectionalLight(color, intensity);
  263. +const light = new THREE.PointLight(color, intensity);
  264. light.position.set(0, 10, 0);
  265. -light.target.position.set(-5, 0, 0);
  266. scene.add(light);
  267. -scene.add(light.target);
  268. </pre>
  269. <p>헬퍼 객체도 <a href="/docs/#api/ko/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a>로 바꾸겠습니다.</p>
  270. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const helper = new THREE.DirectionalLightHelper(light);
  271. +const helper = new THREE.PointLightHelper(light);
  272. scene.add(helper);
  273. </pre>
  274. <p><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>에는 목표가 없으므로 <code class="notranslate" translate="no">onChange</code> 함수도 훨씬 간단하게
  275. 짤 수 있습니다.</p>
  276. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateLight() {
  277. - light.target.updateMatrixWorld();
  278. helper.update();
  279. }
  280. -updateLight();
  281. </pre>
  282. <p><a href="/docs/#api/ko/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a>는 점의 표상을 그립니다. 점의 표상이란 점으로는 확인이 어려우니,
  283. 기본값으로 다이아몬드 형태의 와이어프레임(wireframe)을 대신 그려놓은 것이죠. 점의
  284. 형태는 조명에 <code class="notranslate" translate="no">mesh</code> 객체를 하나 넘겨 얼마든지 바꿀 수 있습니다.</p>
  285. <p><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>에는 추가로 <a href="/docs/#api/ko/lights/PointLight#distance"><code class="notranslate" translate="no">distance</code></a> 속성이 있습니다.
  286. <code class="notranslate" translate="no">distance</code>가이 0이면 <a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>의 밝기가 무한대임을 의미하고, 0보다 크면
  287. <code class="notranslate" translate="no">distance</code>에 지정된 거리만큼만 영향을 미칩니다.</p>
  288. <p>거리도 조정할 수 있도록 GUI에 추가하겠습니다.</p>
  289. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  290. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  291. gui.add(light, 'intensity', 0, 2, 0.01);
  292. +gui.add(light, 'distance', 0, 40).onChange(updateLight);
  293. makeXYZGUI(gui, light.position, 'position', updateLight);
  294. -makeXYZGUI(gui, light.target.position, 'target', updateLight);
  295. </pre>
  296. <p>이제 한 번 테스트해보죠.</p>
  297. <p></p><div translate="no" class="threejs_example_container notranslate">
  298. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point.html"></iframe></div>
  299. <a class="threejs_center" href="/manual/examples/lights-point.html" target="_blank">새 탭에서 보기</a>
  300. </div>
  301. <p></p>
  302. <p><code class="notranslate" translate="no">distance</code>가 0보다 클 때 조명의 밝기를 잘 관찰해보세요.</p>
  303. <h2 id="-spotlight-"><a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a></h2>
  304. <p>스포트라이트는 비유하자면 원뿔 안의 <a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>입니다.
  305. 차이점은 원뿔 안에서만 빛난다는 점이죠. <a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>의
  306. 원뿔은 종류는 외부 원뿔과 내부 원뿔 두 가지입니다.
  307. 빛의 밝기는 내부 원뿔에서 가장 세고, 외부 원뿔에 가까워질수록
  308. 0까지 낮아집니다.</p>
  309. <p><a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>와 마찬가지로 <a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>도 목표의 위치를
  310. 정해줘야 합니다. 원뿔의 밑면이 해당 목표물을 바라보게 되죠.</p>
  311. <p>위 예제의 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>와 헬퍼 객체를 수정하겠습니다.</p>
  312. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  313. -const intensity = 1;
  314. +const intensity = 150;
  315. -const light = new THREE.DirectionalLight(color, intensity);
  316. +const light = new THREE.SpotLight(color, intensity);
  317. scene.add(light);
  318. scene.add(light.target);
  319. -const helper = new THREE.DirectionalLightHelper(light);
  320. +const helper = new THREE.SpotLightHelper(light);
  321. scene.add(helper);
  322. </pre>
  323. <p>원뿔의 내각은 <a href="/docs/#api/ko/lights/SpotLight#angle"><code class="notranslate" translate="no">angle</code></a>에 호도(radians)값을 지정해
  324. 설정합니다. <a href="textures.html">텍스처 예제</a>에서 사용했던 <code class="notranslate" translate="no">DegRadHelper</code>
  325. 객체를 사용해 UI에는 도(degrees)로 표시하도록 하겠습니다.</p>
  326. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(new DegRadHelper(light, 'angle'), 'value', 0, 90).name('angle').onChange(updateLight);
  327. </pre>
  328. <p>내부 원뿔의 크기는 <a href="/docs/#api/ko/lights/SpotLight#penumbra"><code class="notranslate" translate="no">penumbra(반음영)</code></a> 속성을 외부
  329. 원뿔에 대한 비율(퍼센트)로 지정해 사용합니다. 다시 말해 <code class="notranslate" translate="no">penumbra</code> 속성이
  330. 0이면 외부 원뿔과 크기가 동일하다는 것이고, 1이면 빛이 중앙에서부터 외부
  331. 원뿔까지 점점 희미해짐을 의미하죠. <code class="notranslate" translate="no">penumbra</code> 속성이 0.5이라면? 중앙과 외부
  332. 원뿔의 사이 50% 지점부터 빛이 희미해짐을 의미합니다.</p>
  333. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(light, 'penumbra', 0, 1, 0.01);
  334. </pre>
  335. <p></p><div translate="no" class="threejs_example_container notranslate">
  336. <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>
  337. <a class="threejs_center" href="/manual/examples/lights-spot-w-helper.html" target="_blank">새 탭에서 보기</a>
  338. </div>
  339. <p></p>
  340. <p><code class="notranslate" translate="no">penumbra</code> 속성이 0일 때는 빛의 경계가 굉장히 분명한 것이 보일 겁니다.
  341. <code class="notranslate" translate="no">penumbra</code> 속성을 1에 가깝게 조정하면 경계가 점점 흐릿해지죠.</p>
  342. <p><a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>가 <em>원뿔 모양</em>처럼 보이지 않을지도 모릅니다. 이는 바닥이 원뿔의
  343. 거리보다 가까이 있기 때문으로, <code class="notranslate" translate="no">distance</code>를 약 5 정도로 조정하면 원뿔의 밑면을
  344. 확인할 수 있을 겁니다.</p>
  345. <h2 id="-rectarealight-"><a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a></h2>
  346. <p>마지막으로 살펴볼 조명은 <a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>입니다. 이름 그대로 사각 형태의
  347. 조명으로, 형광등이나 천장의 유리를 통과하는 태양빛을 표현하기에 적합합니다.</p>
  348. <p><a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>는 <a href="/docs/#api/ko/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a>과 <a href="/docs/#api/ko/materials/MeshPhysicalMaterial"><code class="notranslate" translate="no">MeshPhysicalMaterial</code></a>만
  349. 지원합니다. 예전 코드에서 재질(material)을 <a href="/docs/#api/ko/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a>로 바꾸겠습니다.</p>
  350. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> ...
  351. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  352. - const planeMat = new THREE.MeshPhongMaterial({
  353. + const planeMat = new THREE.MeshStandardMaterial({
  354. map: texture,
  355. side: THREE.DoubleSide,
  356. });
  357. const mesh = new THREE.Mesh(planeGeo, planeMat);
  358. mesh.rotation.x = Math.PI * -.5;
  359. scene.add(mesh);
  360. }
  361. {
  362. const cubeSize = 4;
  363. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  364. - const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  365. + const cubeMat = new THREE.MeshStandardMaterial({color: '#8AC'});
  366. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  367. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  368. scene.add(mesh);
  369. }
  370. {
  371. const sphereRadius = 3;
  372. const sphereWidthDivisions = 32;
  373. const sphereHeightDivisions = 16;
  374. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  375. - const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  376. + const sphereMat = new THREE.MeshStandardMaterial({color: '#CA8'});
  377. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  378. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  379. scene.add(mesh);
  380. }
  381. </pre>
  382. <p><a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>를 사용하려면 별도의 데이터를 불러와야 합니다. 또한
  383. <a href="/docs/#api/ko/helpers/RectAreaLightHelper"><code class="notranslate" translate="no">RectAreaLightHelper</code></a>도 같이 불러와 조명을 시각화하겠습니다.</p>
  384. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  385. +import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  386. +import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  387. </pre>
  388. <p>모듈을 불러온 후 <code class="notranslate" translate="no">RectAreaLightUniformsLib.init</code> 메서드를 호출합니다.</p>
  389. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  390. const canvas = document.querySelector('#c');
  391. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  392. + RectAreaLightUniformsLib.init();
  393. </pre>
  394. <p>데이터를 불러오지 않아도 에러는 발생하지 않지만, 이상하게 보일 것이므로
  395. 데이터를 불러와야 한다는 것을 꼭 기억하기 바랍니다.</p>
  396. <p>이제 조명을 추가합니다.</p>
  397. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  398. *const intensity = 5;
  399. +const width = 12;
  400. +const height = 4;
  401. *const light = new THREE.RectAreaLight(color, intensity, width, height);
  402. light.position.set(0, 10, 0);
  403. +light.rotation.x = THREE.MathUtils.degToRad(-90);
  404. scene.add(light);
  405. *const helper = new RectAreaLightHelper(light);
  406. *light.add(helper);
  407. </pre>
  408. <p><a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>는 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>, <a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>와 달리 목표를 사용하지 않습니다.
  409. 빛의 방향은 <code class="notranslate" translate="no">rotation</code>으로 설정할 수 있죠. 또 <a href="/docs/#api/ko/helpers/RectAreaLightHelper"><code class="notranslate" translate="no">RectAreaLightHelper</code></a>는 직접 조명을
  410. 자식으로 두는 다른 헬퍼 객체와 달리, 해당 조명의 자식이어야 합니다.</p>
  411. <p>조명의 <code class="notranslate" translate="no">rotation</code>, <code class="notranslate" translate="no">width</code>, <code class="notranslate" translate="no">height</code> 속성을 조정할 수 있도록 GUI도 수정해줍니다.</p>
  412. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  413. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  414. gui.add(light, 'intensity', 0, 10, 0.01);
  415. gui.add(light, 'width', 0, 20);
  416. gui.add(light, 'height', 0, 20);
  417. gui.add(new DegRadHelper(light.rotation, 'x'), 'value', -180, 180).name('x rotation');
  418. gui.add(new DegRadHelper(light.rotation, 'y'), 'value', -180, 180).name('y rotation');
  419. gui.add(new DegRadHelper(light.rotation, 'z'), 'value', -180, 180).name('z rotation');
  420. makeXYZGUI(gui, light.position, 'position');
  421. </pre>
  422. <p></p><div translate="no" class="threejs_example_container notranslate">
  423. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-rectarea.html"></iframe></div>
  424. <a class="threejs_center" href="/manual/examples/lights-rectarea.html" target="_blank">새 탭에서 보기</a>
  425. </div>
  426. <p></p>
  427. <p>조명은 <code class="notranslate" translate="no">renderer</code>가 장면을 렌더링하는 속도에 영향을 미칩니다. 그러니
  428. 가능한 적은 조명을 쓰는 게 좋죠.</p>
  429. <p>다음 장에서는 <a href="cameras.html">카메라 조작법</a>에 대해 알아보겠습니다.</p>
  430. <p><canvas id="c"></canvas></p>
  431. <script type="module" src="../resources/threejs-lights.js"></script>
  432. </div>
  433. </div>
  434. </div>
  435. <script src="../resources/prettify.js"></script>
  436. <script src="../resources/lesson.js"></script>
  437. </body></html>