lights.html 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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="/manual/resources/lesson.css">
  12. <link rel="stylesheet" href="/manual/resources/lang.css">
  13. <!-- Import maps polyfill -->
  14. <!-- Remove this when import maps will be widely supported -->
  15. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../../build/three.module.js"
  20. }
  21. }
  22. </script>
  23. <link rel="stylesheet" href="/manual/ko/lang.css">
  24. </head>
  25. <body>
  26. <div class="container">
  27. <div class="lesson-title">
  28. <h1>조명(Lights)</h1>
  29. </div>
  30. <div class="lesson">
  31. <div class="lesson-main">
  32. <p>※ 이 글은 Three.js의 튜토리얼 시리즈로서,
  33. 먼저 <a href="fundamentals.html">Three.js의 기본 구조에 관한 글</a>과
  34. <a href="setup.html">개발 환경 설정하는 법</a>을 읽고 오길 권장합니다.</p>
  35. <p>이전 글은 <a href="textures.html">텍스처에 관한 글</a>이었죠. 이번에는
  36. Three.js의 다양한 조명을 어떻게 쓰는지 알아보겠습니다.</p>
  37. <p>먼저 이전 예제에서 카메라를 수정하겠습니다. 시야각(fov, field of view)은
  38. 45도, <code class="notranslate" translate="no">far</code>면은 100칸, 카메라의 위치는 중점에서 위로 10칸, 뒤로 20칸 옮깁니다.</p>
  39. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">*const fov = 45;
  40. const aspect = 2; // canvas 요소의 기본 비율
  41. const near = 0.1;
  42. *const far = 100;
  43. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  44. +camera.position.set(0, 10, 20);
  45. </pre>
  46. <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>는 특정 좌표를
  47. 중심으로 카메라를 자전 또는 <em>공전(orbit)</em>하도록 해줍니다. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>는
  48. 별도 모듈이므로, 먼저 페이지에 로드해야 합니다.</p>
  49. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  50. +import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  51. </pre>
  52. <p>이제 <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>에 카메라와, DOM 이벤트를 감지할 수 있도록
  53. canvas 요소를 넘겨줍니다.</p>
  54. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const controls = new OrbitControls(camera, canvas);
  55. controls.target.set(0, 5, 0);
  56. controls.update();
  57. </pre>
  58. <p>또한 시점을 중점에서 위로 5칸 올린 후 <code class="notranslate" translate="no">controls.update</code> 메서드를
  59. 호출해 <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a>가 새로운 시점을 바라보게 합니다.</p>
  60. <p>다음으로 빛을 받을 무언가를 만들어보겠습니다. 먼저 땅의 역할을 할
  61. 평면을 만들고, 평면에 2x2 픽셀의 체크판 텍스처를 씌우겠습니다.</p>
  62. <div class="threejs_center">
  63. <img src="../examples/resources/images/checker.png" class="border" style="
  64. image-rendering: pixelated;
  65. width: 128px;
  66. ">
  67. </div>
  68. <p>일단 텍스처를 불러온 뒤, 반복하도록 래핑(wrapping)을 설정해줍니다. 필터는
  69. <code class="notranslate" translate="no">NearestFilter</code>, 텍스처가 2x2 픽셀의 체크판이니 <code class="notranslate" translate="no">repeat</code> 속성을 평면의
  70. 반으로 설정하면 체크판의 각 칸은 정확히 (장면의) 1칸이 될 겁니다.</p>
  71. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 40;
  72. const loader = new THREE.TextureLoader();
  73. const texture = loader.load('resources/images/checker.png');
  74. texture.wrapS = THREE.RepeatWrapping;
  75. texture.wrapT = THREE.RepeatWrapping;
  76. texture.magFilter = THREE.NearestFilter;
  77. const repeats = planeSize / 2;
  78. texture.repeat.set(repeats, repeats);
  79. </pre>
  80. <p>그리고 평면 <code class="notranslate" translate="no">geometry</code>, 평면에 쓸 재질(material), 장면(scene)에 추가할
  81. <code class="notranslate" translate="no">mesh</code>를 만듭니다. 평면은 기본적으로 XY축을 기준으로 하니, XZ축을 기준으로
  82. 하려면 평면을 회전시켜야 합니다.</p>
  83. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  84. const planeMat = new THREE.MeshPhongMaterial({
  85. map: texture,
  86. side: THREE.DoubleSide,
  87. });
  88. const mesh = new THREE.Mesh(planeGeo, planeMat);
  89. mesh.rotation.x = Math.PI * -.5;
  90. scene.add(mesh);
  91. </pre>
  92. <p>정육면체와 구체도 추가해서 평면까지 총 3개의 물체를 추가하도록 하죠.</p>
  93. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  94. const cubeSize = 4;
  95. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  96. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  97. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  98. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  99. scene.add(mesh);
  100. }
  101. {
  102. const sphereRadius = 3;
  103. const sphereWidthDivisions = 32;
  104. const sphereHeightDivisions = 16;
  105. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  106. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  107. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  108. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  109. scene.add(mesh);
  110. }
  111. </pre>
  112. <p>빛을 받을 물체를 만들었으니 이제 조명을 가지고 놀아봅시다!</p>
  113. <h2 id="-ambientlight-"><a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a></h2>
  114. <p>먼저 <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>(자연광)를 써보겠습니다.</p>
  115. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  116. const intensity = 1;
  117. const light = new THREE.AmbientLight(color, intensity);
  118. scene.add(light);
  119. </pre>
  120. <p>이 조명도 <a href="https://github.com/georgealways/lil-gui">lil-gui</a>를 사용해
  121. 속성을 조정할 수 있도록 만들겠습니다. lil-gui로 색상을 조정하려면 간단한
  122. 헬퍼 클래스가 필요합니다. 이 클래스는 색상을 CSS hex(예: <code class="notranslate" translate="no">#FF8844</code>) 값으로
  123. 변경해 lil-gui에 넘겨주는 역할을 할 거예요. 그리고 lil-gui가 클래스의
  124. 속성을 지정할 때, 이를 조명에 직접 지정하도록 합니다.</p>
  125. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ColorGUIHelper {
  126. constructor(object, prop) {
  127. this.object = object;
  128. this.prop = prop;
  129. }
  130. get value() {
  131. return `#${this.object[this.prop].getHexString()}`;
  132. }
  133. set value(hexString) {
  134. this.object[this.prop].set(hexString);
  135. }
  136. }
  137. </pre>
  138. <p>아래는 lil-gui를 만드는 코드입니다.</p>
  139. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  140. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  141. gui.add(light, 'intensity', 0, 2, 0.01);
  142. </pre>
  143. <p>결과물은 다음과 같죠.</p>
  144. <p></p><div translate="no" class="threejs_example_container notranslate">
  145. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-ambient.html"></iframe></div>
  146. <a class="threejs_center" href="/manual/examples/lights-ambient.html" target="_blank">새 탭에서 보기</a>
  147. </div>
  148. <p></p>
  149. <p>카메라를 <em>공전시키기(orbit)</em> 위해 화면을 드래그해보세요.</p>
  150. <p>물체들이 평평하고, 윤곽이 뚜렷하지 않습니다. <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>는 물체와
  151. 조명의 색, 그리고 조명의 밝기를 곱한 것과 같죠.</p>
  152. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">color = materialColor * light.color * light.intensity;
  153. </pre><p>이게 전부입니다. <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>에는 방향이라는 개념이 없죠. 주변광은
  154. 완전히 고르게 적용되고 공간 안 물체의 색을 바꾸는 역할만 하기 때문에
  155. 실용적이지 않은데다 그다지 <em>조명</em>처럼 느껴지지도 않습니다. 어두운 장면을
  156. 덜 어둡게 만드는 정도에만 도움이 되죠.</p>
  157. <h2 id="-hemispherelight-"><a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a></h2>
  158. <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>는
  159. 천장과 바닥의 색을 인자로 받아, 물체의 천장을 바라보는 면은 천장 색, 바닥을
  160. 바라보는 면은 바닥 색으로 혼합합니다.</p>
  161. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const color = 0xFFFFFF;
  162. +const skyColor = 0xB1E1FF; // 하늘색
  163. +const groundColor = 0xB97A20; // 오렌지 브라운
  164. const intensity = 1;
  165. -const light = new THREE.AmbientLight(color, intensity);
  166. +const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  167. scene.add(light);
  168. </pre>
  169. <p>마찬가지로 lil-gui를 수정해 두 색상을 조정할 수 있도록 합니다.</p>
  170. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  171. -gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  172. +gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('skyColor');
  173. +gui.addColor(new ColorGUIHelper(light, 'groundColor'), 'value').name('groundColor');
  174. gui.add(light, 'intensity', 0, 2, 0.01);
  175. </pre>
  176. <p></p><div translate="no" class="threejs_example_container notranslate">
  177. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-hemisphere.html"></iframe></div>
  178. <a class="threejs_center" href="/manual/examples/lights-hemisphere.html" target="_blank">새 탭에서 보기</a>
  179. </div>
  180. <p></p>
  181. <p>이 또한 그다지 입체적이지 않습니다. 아까보다는 낮지만 전체적으로 2D처럼
  182. 보이네요. <a href="/docs/#api/ko/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>는 주로 풍경을 표현하거나 할 때 다른 조명과
  183. 함께 사용합니다. 다른 조명과 조합할 때 유용하고, 간단히는 <a href="/docs/#api/ko/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a>
  184. 대신 사용할 수 있죠.</p>
  185. <h2 id="-directionallight-"><a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a></h2>
  186. <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>는
  187. 주로 태양을 표현할 때 사용합니다.</p>
  188. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  189. const intensity = 1;
  190. const light = new THREE.DirectionalLight(color, intensity);
  191. light.position.set(0, 10, 0);
  192. light.target.position.set(-5, 0, 0);
  193. scene.add(light);
  194. scene.add(light.target);
  195. </pre>
  196. <p>먼저 <code class="notranslate" translate="no">light</code>와 <code class="notranslate" translate="no">light.target</code>(목표)을 모두 장면에 추가해야 합니다.
  197. 그래야 Three.js의 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>가 목표가 있는 방향으로 빛을
  198. 쬘 테니까요.</p>
  199. <p>이 역시 GUI를 사용해 목표의 위치를 조정할 수 있도록 만들겠습니다.</p>
  200. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  201. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  202. gui.add(light, 'intensity', 0, 2, 0.01);
  203. gui.add(light.target.position, 'x', -10, 10);
  204. gui.add(light.target.position, 'z', -10, 10);
  205. gui.add(light.target.position, 'y', 0, 10);
  206. </pre>
  207. <p></p><div translate="no" class="threejs_example_container notranslate">
  208. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional.html"></iframe></div>
  209. <a class="threejs_center" href="/manual/examples/lights-directional.html" target="_blank">새 탭에서 보기</a>
  210. </div>
  211. <p></p>
  212. <p>조명의 위치가 보이지 않으니 정확한 동작을 확인하기가 좀 어렵네요.
  213. 다행히 Three.js에는 눈에 보이지 않는 요소의 시각화를 도와주는
  214. 다양한 헬퍼 객체가 있습니다. 이 경우 <a href="/docs/#api/ko/helpers/DirectionalLightHelper"><code class="notranslate" translate="no">DirectionalLightHelper</code></a>를
  215. 사용해 조명을 면으로, 조명의 방향을 선으로 나타낼 수 있습니다.
  216. 사용법도 간단해서 조명을 인자로 넘겨주고 생성한 인스턴스를 장면에
  217. 추가하면 됩니다.</p>
  218. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const helper = new THREE.DirectionalLightHelper(light);
  219. scene.add(helper);
  220. </pre>
  221. <p>하는 김에 조명과 목표 둘 다 위치를 조정할 수 있도록 하겠습니다.
  222. <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>,
  223. <code class="notranslate" translate="no">z</code> 속성을 조정하는 함수를 하나 만듭니다.</p>
  224. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeXYZGUI(gui, vector3, name, onChangeFn) {
  225. const folder = gui.addFolder(name);
  226. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  227. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  228. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  229. folder.open();
  230. }
  231. </pre>
  232. <p>헬퍼 객체를 사용할 때는 헬퍼 객체의 <code class="notranslate" translate="no">update</code> 메서드를 수동으로
  233. 호출해줘야 합니다. 한 예로 lil-gui가 객체 속성을 변경할 때마다
  234. 인자로 넘겨준 <code class="notranslate" translate="no">onChangeFn</code>에서 헬퍼 객체의 <code class="notranslate" translate="no">update</code> 메서드를
  235. 호출할 수 있죠.</p>
  236. <p>그리고 조명의 위치, 목표의 위치 객체에 방금 만든 함수를 각각 적용합니다.</p>
  237. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function updateLight() {
  238. + light.target.updateMatrixWorld();
  239. + helper.update();
  240. +}
  241. +updateLight();
  242. const gui = new GUI();
  243. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  244. gui.add(light, 'intensity', 0, 2, 0.01);
  245. +makeXYZGUI(gui, light.position, 'position', updateLight);
  246. +makeXYZGUI(gui, light.target.position, 'target', updateLight);
  247. </pre>
  248. <p>이제 조명, 목표의 위치를 각각 조정할 수 있습니다.</p>
  249. <p></p><div translate="no" class="threejs_example_container notranslate">
  250. <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>
  251. <a class="threejs_center" href="/manual/examples/lights-directional-w-helper.html" target="_blank">새 탭에서 보기</a>
  252. </div>
  253. <p></p>
  254. <p>카메라를 돌려보면 아까보다 훨씬 동작이 명확하게 보일 겁니다.
  255. 평면은 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>를 나타내는데, 이는 직사광이 어느
  256. <em>한 점</em>에서 뻗어나오는 조명이 아니기 때문입니다. 무한한 광원이
  257. 목표를 향해 평행하게 빛을 내리쬐는 것이죠.</p>
  258. <h2 id="-pointlight-"><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a></h2>
  259. <p><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>는 한 점에서 무한히 뻗어나가는 광원입니다. 코드를
  260. 다시 한 번 수정해보죠.</p>
  261. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  262. const intensity = 1;
  263. -const light = new THREE.DirectionalLight(color, intensity);
  264. +const light = new THREE.PointLight(color, intensity);
  265. light.position.set(0, 10, 0);
  266. -light.target.position.set(-5, 0, 0);
  267. scene.add(light);
  268. -scene.add(light.target);
  269. </pre>
  270. <p>헬퍼 객체도 <a href="/docs/#api/ko/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a>로 바꾸겠습니다.</p>
  271. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const helper = new THREE.DirectionalLightHelper(light);
  272. +const helper = new THREE.PointLightHelper(light);
  273. scene.add(helper);
  274. </pre>
  275. <p><a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>에는 목표가 없으므로 <code class="notranslate" translate="no">onChange</code> 함수도 훨씬 간단하게
  276. 짤 수 있습니다.</p>
  277. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateLight() {
  278. - light.target.updateMatrixWorld();
  279. helper.update();
  280. }
  281. -updateLight();
  282. </pre>
  283. <p><a href="/docs/#api/ko/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a>는 점의 표상을 그립니다. 점의 표상이란 점으로는 확인이 어려우니,
  284. 기본값으로 다이아몬드 형태의 와이어프레임(wireframe)을 대신 그려놓은 것이죠. 점의
  285. 형태는 조명에 <code class="notranslate" translate="no">mesh</code> 객체를 하나 넘겨 얼마든지 바꿀 수 있습니다.</p>
  286. <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> 속성이 있습니다.
  287. <code class="notranslate" translate="no">distance</code>가이 0이면 <a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>의 밝기가 무한대임을 의미하고, 0보다 크면
  288. <code class="notranslate" translate="no">distance</code>에 지정된 거리만큼만 영향을 미칩니다.</p>
  289. <p>거리도 조정할 수 있도록 GUI에 추가하겠습니다.</p>
  290. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  291. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  292. gui.add(light, 'intensity', 0, 2, 0.01);
  293. +gui.add(light, 'distance', 0, 40).onChange(updateLight);
  294. makeXYZGUI(gui, light.position, 'position', updateLight);
  295. -makeXYZGUI(gui, light.target.position, 'target', updateLight);
  296. </pre>
  297. <p>이제 한 번 테스트해보죠.</p>
  298. <p></p><div translate="no" class="threejs_example_container notranslate">
  299. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point.html"></iframe></div>
  300. <a class="threejs_center" href="/manual/examples/lights-point.html" target="_blank">새 탭에서 보기</a>
  301. </div>
  302. <p></p>
  303. <p><code class="notranslate" translate="no">distance</code>가 0보다 클 때 조명의 밝기를 잘 관찰해보세요.</p>
  304. <h2 id="-spotlight-"><a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a></h2>
  305. <p>스포트라이트는 비유하자면 원뿔 안의 <a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>입니다.
  306. 차이점은 원뿔 안에서만 빛난다는 점이죠. <a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>의
  307. 원뿔은 종류는 외부 원뿔과 내부 원뿔 두 가지입니다.
  308. 빛의 밝기는 내부 원뿔에서 가장 세고, 외부 원뿔에 가까워질수록
  309. 0까지 낮아집니다.</p>
  310. <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>도 목표의 위치를
  311. 정해줘야 합니다. 원뿔의 밑면이 해당 목표물을 바라보게 되죠.</p>
  312. <p>위 예제의 <a href="/docs/#api/ko/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a>와 헬퍼 객체를 수정하겠습니다.</p>
  313. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  314. const intensity = 1;
  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({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>하나 설명하지 않은 것이 있습니다. 위 예제에는 <a href="/docs/#api/ko/renderers/WebGLRenderer"><code class="notranslate" translate="no">WebGLRenderer</code></a>의 <code class="notranslate" translate="no">physicallyCorrectLights(물리 기반 조명)</code>
  428. 설정이 있습니다. 이는 거리에 따라 빛이 어떻게 떨어질지 결정하는 속성으로,
  429. <a href="/docs/#api/ko/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a>와 <a href="/docs/#api/ko/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a>가 이 설정의 영향을 받습니다. <a href="/docs/#api/ko/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a>는
  430. 마찬가지로 설정의 영향도 받고, 기본적으로 이 설정을 사용하죠.</p>
  431. <p>이 설정을 사용하면 기본적으로 조명의 <code class="notranslate" translate="no">distance</code>나 <code class="notranslate" translate="no">intensity</code> 대신
  432. <a href="/docs/#api/ko/lights/PointLight#power"><code class="notranslate" translate="no">power</code></a> 속성을 루멘(lumens) 단위로 설정해야 합니다.
  433. 그러면 Three.js는 물리적 계산을 통해 실제 광원을 흉내내죠. 예제의
  434. 거리 단위는 미터(meters)이니, 60w짜리 전구는 약 800루멘 정도일 겁니다.
  435. 그리고 조명의 부서짐(decay) 정도를 설정하는 <a href="/docs/#api/ko/lights/PointLight#decay"><code class="notranslate" translate="no">decay</code></a>
  436. 속성도 있습니다. 현실적인 조명을 위해서는 <code class="notranslate" translate="no">2</code> 정도가 적당하죠.</p>
  437. <p>한 번 예제를 만들어 테스트해봅시다.</p>
  438. <p>먼저 <code class="notranslate" translate="no">renderer</code>의 <code class="notranslate" translate="no">physicallyCorrectLights</code> 속성을 켭니다.</p>
  439. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const renderer = new THREE.WebGLRenderer({canvas});
  440. +renderer.physicallyCorrectLights = true;
  441. </pre>
  442. <p>그리고 <code class="notranslate" translate="no">power</code>를 800루멘으로, <code class="notranslate" translate="no">decay</code> 속성을 2로, <code class="notranslate" translate="no">distance</code>
  443. 속성을 <code class="notranslate" translate="no">Infinity</code>로 설정합니다.</p>
  444. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  445. const intensity = 1;
  446. const light = new THREE.PointLight(color, intensity);
  447. light.power = 800;
  448. light.decay = 2;
  449. light.distance = Infinity;
  450. </pre>
  451. <p>마지막으로 GUI를 추가해 <code class="notranslate" translate="no">power</code>와 <code class="notranslate" translate="no">decay</code> 속성을 조정할 수 있도록
  452. 해줍니다.</p>
  453. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  454. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  455. gui.add(light, 'decay', 0, 4, 0.01);
  456. gui.add(light, 'power', 0, 2000);
  457. </pre>
  458. <p></p><div translate="no" class="threejs_example_container notranslate">
  459. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point-physically-correct.html"></iframe></div>
  460. <a class="threejs_center" href="/manual/examples/lights-point-physically-correct.html" target="_blank">새 탭에서 보기</a>
  461. </div>
  462. <p></p>
  463. <p>조명은 <code class="notranslate" translate="no">renderer</code>가 장면을 렌더링하는 속도에 영향을 미칩니다. 그러니
  464. 가능한 적은 조명을 쓰는 게 좋죠.</p>
  465. <p>다음 장에서는 <a href="cameras.html">카메라 조작법</a>에 대해 알아보겠습니다.</p>
  466. <p><canvas id="c"></canvas></p>
  467. <script type="module" src="../resources/threejs-lights.js"></script>
  468. </div>
  469. </div>
  470. </div>
  471. <script src="/manual/resources/prettify.js"></script>
  472. <script src="/manual/resources/lesson.js"></script>
  473. </body></html>