lights.html 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <!DOCTYPE html><html lang="ja"><head>
  2. <meta charset="utf-8">
  3. <title>のライト</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 – のライト">
  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. <!-- 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. </head>
  24. <body>
  25. <div class="container">
  26. <div class="lesson-title">
  27. <h1>のライト</h1>
  28. </div>
  29. <div class="lesson">
  30. <div class="lesson-main">
  31. <p>この記事はThree.jsの連載記事の1つです。
  32. 最初の記事は<a href="fundamentals.html">Three.jsの基礎知識</a>です。
  33. まだ読んでいない場合は、Three.jsの基礎知識や<a href="setup.html">セットアップ</a>から始めると良いと思います。
  34. 前回の記事は<a href="textures.html">テクスチャ</a>でした。</p>
  35. <p>今回はthree.jsの色々な種類のライトの使い方を確認していきます。</p>
  36. <p>前回のサンプルコードからカメラの設定を修正しましょう。
  37. 視野角(fov)を45度にし、遠平面(far)を100、カメラを原点からY座標に10、Z座標を20にします。</p>
  38. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">*const fov = 45;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. *const far = 100;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. +camera.position.set(0, 10, 20);
  44. </pre>
  45. <p>次に <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> を追加します。
  46. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> は、カメラをある点を中心に<em>軌道</em>を回転できます。
  47. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> はthree.jsのオプション機能なので、importする必要があります。</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>これでOrbitControlsを利用できます。
  52. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> にカメラと入力イベントを取得するDOM要素を渡します。</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>controls.targetのY座標を5にして <code class="notranslate" translate="no">controls.update</code> を呼び出します。</p>
  58. <p>次はライトアップするオブジェクトを作ってみましょう。
  59. まずは地上となる平面を作ります。
  60. この平面に2 x 2ピクセルの小さなチェッカーボードのテクスチャを適用します。</p>
  61. <div class="threejs_center">
  62. <img src="../examples/resources/images/checker.png" class="border" style="
  63. image-rendering: pixelated;
  64. width: 128px;
  65. ">
  66. </div>
  67. <p>最初にテクスチャを読み込み、何回テクスチャのリピートを繰り返すかを設定し、フィルターはニアレスト(nearest)にします。
  68. テクスチャは2 x 2ピクセルのチェッカーボードです。
  69. テクスチャのリピートは平面の半分の大きさにし、チェッカーボードの1つのチェック部分は1にします。</p>
  70. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 40;
  71. const loader = new THREE.TextureLoader();
  72. const texture = loader.load('resources/images/checker.png');
  73. texture.wrapS = THREE.RepeatWrapping;
  74. texture.wrapT = THREE.RepeatWrapping;
  75. texture.magFilter = THREE.NearestFilter;
  76. const repeats = planeSize / 2;
  77. texture.repeat.set(repeats, repeats);
  78. </pre>
  79. <p>平面のジオメトリとマテリアルを作り、それを元にシーンに追加するメッシュを作ります。
  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-"><code class="notranslate" translate="no">AmbientLight(環境光源)</code></h2>
  112. <p>最初に <a href="/docs/#api/ja/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>ライトのパラメーターを調整できるようにします。
  119. 今回も<a href="https://github.com/georgealways/lil-gui">lil-gui</a>を使います。
  120. lil-guiで色を調整するにはヘルパーが必要です。
  121. プロパティをlil-guiにCSSの16進数の形で表示します(例: <code class="notranslate" translate="no">#FF8844</code>)。
  122. ヘルパーは名前付きプロパティから色を取得し、16進数の文字列に変換してlil-guiに渡します。lil-guiがヘルパーのプロパティを設定し、結果をライトの色に戻します。</p>
  123. <p>これがヘルパーです。</p>
  124. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ColorGUIHelper {
  125. constructor(object, prop) {
  126. this.object = object;
  127. this.prop = prop;
  128. }
  129. get value() {
  130. return `#${this.object[this.prop].getHexString()}`;
  131. }
  132. set value(hexString) {
  133. this.object[this.prop].set(hexString);
  134. }
  135. }
  136. </pre>
  137. <p>lil-guiの設定は以下の通りです。</p>
  138. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  139. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  140. gui.add(light, 'intensity', 0, 2, 0.01);
  141. </pre>
  142. <p>これで以下のような結果になります。</p>
  143. <p></p><div translate="no" class="threejs_example_container notranslate">
  144. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-ambient.html"></iframe></div>
  145. <a class="threejs_center" href="/manual/examples/lights-ambient.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  146. </div>
  147. <p></p>
  148. <p>シーンをクリックしてドラッグして、カメラを<em>軌道</em>に乗せます。</p>
  149. <p>環境光源だけでは正しくライティング表現できてません。キューブと球体に陰影がなく形状が平面に見えます。
  150. 以下のように <a href="/docs/#api/ja/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a> はマテリアルの色とライトの色、ライトの強度(intensity)を掛けてます。</p>
  151. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">color = materialColor * light.color * light.intensity;
  152. </pre><p>それだけで方向性がないです。
  153. この環境光源は100%均一でシーン内の全ての色を変える以外は<em>ライティング</em>としてはあまり役に立ちません。
  154. 環境光源は暗すぎない暗さを作る事ができます。</p>
  155. <h2 id="-hemispherelight-"><code class="notranslate" translate="no">HemisphereLight(半球光源)</code></h2>
  156. <p>コードを <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> に切り替えてみましょう。
  157. <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> は空と地面の色を取得し、その2色とマテリアルの色を掛け合わせます。</p>
  158. <p>これが新しいコードです。</p>
  159. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const color = 0xFFFFFF;
  160. +const skyColor = 0xB1E1FF; // light blue
  161. +const groundColor = 0xB97A20; // brownish orange
  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>
  175. <p></p><div translate="no" class="threejs_example_container notranslate">
  176. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-hemisphere.html"></iframe></div>
  177. <a class="threejs_center" href="/manual/examples/lights-hemisphere.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  178. </div>
  179. <p></p>
  180. <p>まだ正しくライティング表現ができてなく、キューブと球体が平面に見えます。
  181. 別のライトと組み合わせて使用される <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> は、空や地面の色に良い影響を与えます。
  182. この方法では他のライトと組み合わせて使うか、<a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> を代わりに使うのがベストです。</p>
  183. <h2 id="-directionallight-"><code class="notranslate" translate="no">DirectionalLight(平行光源)</code></h2>
  184. <p>コードを <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> に切り替えてみましょう。
  185. <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> は太陽を表すのによく使われます。</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/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> はターゲットの方向にライティングします。</p>
  196. <p>GUIに追加してlight.targetを動かせるようにしてみましょう。</p>
  197. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  198. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  199. gui.add(light, 'intensity', 0, 2, 0.01);
  200. gui.add(light.target.position, 'x', -10, 10);
  201. gui.add(light.target.position, 'z', -10, 10);
  202. gui.add(light.target.position, 'y', 0, 10);
  203. </pre>
  204. <p></p><div translate="no" class="threejs_example_container notranslate">
  205. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional.html"></iframe></div>
  206. <a class="threejs_center" href="/manual/examples/lights-directional.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  207. </div>
  208. <p></p>
  209. <p>なんだか見づらいですね。
  210. Three.jsにはシーンに追加できるヘルパーオブジェクトがたくさんあり、シーンの見えない部分を視覚化するのに役立ちます。
  211. 今回は <a href="/docs/#api/ja/helpers/DirectionalLightHelper"><code class="notranslate" translate="no">DirectionalLightHelper</code></a> を使い、ライトから平面までの線を描画します。
  212. lightをDirectionalLightHelperに渡してシーンに追加します。</p>
  213. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const helper = new THREE.DirectionalLightHelper(light);
  214. scene.add(helper);
  215. </pre>
  216. <p>ライトの位置とターゲットの両方を設定できるようにしておきましょう。
  217. <a href="/docs/#api/ja/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>, <code class="notranslate" translate="no">z</code> プロパティを調整できる関数を作ります。</p>
  218. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeXYZGUI(gui, vector3, name, onChangeFn) {
  219. const folder = gui.addFolder(name);
  220. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  221. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  222. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  223. folder.open();
  224. }
  225. </pre>
  226. <p>変更時は常にヘルパーの <code class="notranslate" translate="no">update</code> 関数を呼び出す必要があります。
  227. そのため、lil-guiが値を更新時に <code class="notranslate" translate="no">onChangeFn</code> 関数を渡しています。</p>
  228. <p>makeXYZGUI関数はlight.positionとlight.target.positionの両方に使えます。</p>
  229. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function updateLight() {
  230. + light.target.updateMatrixWorld();
  231. + helper.update();
  232. +}
  233. +updateLight();
  234. const gui = new GUI();
  235. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  236. gui.add(light, 'intensity', 0, 2, 0.01);
  237. +makeXYZGUI(gui, light.position, 'position', updateLight);
  238. +makeXYZGUI(gui, light.target.position, 'target', updateLight);
  239. </pre>
  240. <p>これでライトを動かす事ができるようになりました。</p>
  241. <p></p><div translate="no" class="threejs_example_container notranslate">
  242. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional-w-helper.html"></iframe></div>
  243. <a class="threejs_center" href="/manual/examples/lights-directional-w-helper.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  244. </div>
  245. <p></p>
  246. <p>カメラを軌道に乗せると見やすくなります。
  247. この平面は <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> を表しており、DirectionalLightが一方向からのライティングを計算します。
  248. 光の出所は<em>点</em>ではなく、平面を無限に照らす平行光線です。</p>
  249. <h2 id="-pointlight-"><code class="notranslate" translate="no">PointLight(点光源)</code></h2>
  250. <p><a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> はある点から全方向に光を放つライトです。
  251. コード変更しましょう。</p>
  252. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  253. const intensity = 1;
  254. -const light = new THREE.DirectionalLight(color, intensity);
  255. +const light = new THREE.PointLight(color, intensity);
  256. light.position.set(0, 10, 0);
  257. -light.target.position.set(-5, 0, 0);
  258. scene.add(light);
  259. -scene.add(light.target);
  260. </pre>
  261. <p><a href="/docs/#api/ja/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a> に切り替えます。</p>
  262. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const helper = new THREE.DirectionalLightHelper(light);
  263. +const helper = new THREE.PointLightHelper(light);
  264. scene.add(helper);
  265. </pre>
  266. <p>light.targetがないので <code class="notranslate" translate="no">onChange</code> 関数はもっとシンプルになります。</p>
  267. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateLight() {
  268. - light.target.updateMatrixWorld();
  269. helper.update();
  270. }
  271. -updateLight();
  272. </pre>
  273. <p><a href="/docs/#api/ja/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a> には点がない事に注意して下さい。
  274. 小さなダイヤモンドのワイヤーフレームを描画します。
  275. 簡単に望む任意の形状にできて、ライト自体にメッシュを追加します。</p>
  276. <p><a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> は <a href="/docs/#api/ja/lights/PointLight#distance"><code class="notranslate" translate="no">distance</code></a>プロパティを持ちます。
  277. <code class="notranslate" translate="no">distance</code> が0ならば <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> は無限大に輝きます。
  278. <code class="notranslate" translate="no">distance</code> が0よりも大きい場合、ライトに向かってライトの全強度を照らし、ライトから離れた <code class="notranslate" translate="no">distance</code> では影響を受けないようにフェードアウトします。</p>
  279. <p>distanceを調整できるようにGUIを設定してみましょう。</p>
  280. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  281. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  282. gui.add(light, 'intensity', 0, 2, 0.01);
  283. +gui.add(light, 'distance', 0, 40).onChange(updateLight);
  284. makeXYZGUI(gui, light.position, 'position', updateLight);
  285. -makeXYZGUI(gui, light.target.position, 'target', updateLight);
  286. </pre>
  287. <p>これを試してみて下さい。</p>
  288. <p></p><div translate="no" class="threejs_example_container notranslate">
  289. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point.html"></iframe></div>
  290. <a class="threejs_center" href="/manual/examples/lights-point.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  291. </div>
  292. <p></p>
  293. <p><code class="notranslate" translate="no">distance</code> が &gt; 0 の時にライトがフェードアウトしている事に注目して下さい。</p>
  294. <h2 id="-spotlight-"><code class="notranslate" translate="no">SpotLight(集中光線)</code></h2>
  295. <p>集中光源は円錐体にライティングする時に効果的です。
  296. 実際は2つの円錐体があります。外側と内側の円錐体です。
  297. 内側と外側の円錐体の間では、ライトは強度のMAX値から0にフェードします。</p>
  298. <p><a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> を使うには、平行光源と同じようにターゲットが必要です。
  299. ライトの円錐体がターゲットに向かって照らされます。</p>
  300. <p>上記のヘルパーを使って <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> を修正します。</p>
  301. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  302. const intensity = 1;
  303. -const light = new THREE.DirectionalLight(color, intensity);
  304. +const light = new THREE.SpotLight(color, intensity);
  305. scene.add(light);
  306. scene.add(light.target);
  307. -const helper = new THREE.DirectionalLightHelper(light);
  308. +const helper = new THREE.SpotLightHelper(light);
  309. scene.add(helper);
  310. </pre>
  311. <p>集中光源の円錐体の角度は <a href="/docs/#api/ja/lights/SpotLight#angle"><code class="notranslate" translate="no">angle</code></a>プロパティでラジアン単位で設定します。
  312. <a href="textures.html">テクスチャ記事</a>の <code class="notranslate" translate="no">DegRadHelper</code> を使い、度数でUIに表示します。</p>
  313. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(new DegRadHelper(light, 'angle'), 'value', 0, 90).name('angle').onChange(updateLight);
  314. </pre>
  315. <p>内側の円錐は<a href="/docs/#api/ja/lights/SpotLight#penumbra"><code class="notranslate" translate="no">penumbra</code></a>プロパティを外側の円錐からの%として設定します。
  316. <code class="notranslate" translate="no">penumbra</code> が1の時、ライトは円錐の中心から外側の円錐に向かってフェードしていきます。
  317. <code class="notranslate" translate="no">penumbra</code> が5の時、ライトは外側の円錐体の中心から50%からフェードしていきます。</p>
  318. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">gui.add(light, 'penumbra', 0, 1, 0.01);
  319. </pre>
  320. <p></p><div translate="no" class="threejs_example_container notranslate">
  321. <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>
  322. <a class="threejs_center" href="/manual/examples/lights-spot-w-helper.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  323. </div>
  324. <p></p>
  325. <p>デフォルトの <code class="notranslate" translate="no">penumbra</code> が0の場合、集中光源は非常にシャープなエッジを持っていますが、1 に向けて <code class="notranslate" translate="no">penumbra</code> を調整するとエッジがぼやけます。</p>
  326. <p>集中光源の<em>円錐体</em>が見えにくいかもしれません。
  327. その理由は地面にあります。
  328. 距離を5くらいまで縮めると、円錐体の端が開いているのが見えてきます。</p>
  329. <h2 id="-rectarealight-"><code class="notranslate" translate="no">RectAreaLight(矩形光源)</code></h2>
  330. <p>もう1種類のライトに <a href="/docs/#api/ja/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> があります。
  331. これはまさにその名の通り、長い蛍光灯のような長方形のエリアのライトや天井にある曇り空のライトです。</p>
  332. <p><a href="/docs/#api/ja/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> は <a href="/docs/#api/ja/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> と <a href="/docs/#api/ja/materials/MeshPhysicalMaterial"><code class="notranslate" translate="no">MeshPhysicalMaterial</code></a> でしか動作しないので、全てのマテリアルを <a href="/docs/#api/ja/materials/MeshStandardMaterial"><code class="notranslate" translate="no">MeshStandardMaterial</code></a> に変更しましょう。</p>
  333. <pre class="prettyprint showlinemods notranslate lang-js" translate="no"> ...
  334. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  335. - const planeMat = new THREE.MeshPhongMaterial({
  336. + const planeMat = new THREE.MeshStandardMaterial({
  337. map: texture,
  338. side: THREE.DoubleSide,
  339. });
  340. const mesh = new THREE.Mesh(planeGeo, planeMat);
  341. mesh.rotation.x = Math.PI * -.5;
  342. scene.add(mesh);
  343. }
  344. {
  345. const cubeSize = 4;
  346. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  347. - const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  348. + const cubeMat = new THREE.MeshStandardMaterial({color: '#8AC'});
  349. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  350. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  351. scene.add(mesh);
  352. }
  353. {
  354. const sphereRadius = 3;
  355. const sphereWidthDivisions = 32;
  356. const sphereHeightDivisions = 16;
  357. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  358. - const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  359. + const sphereMat = new THREE.MeshStandardMaterial({color: '#CA8'});
  360. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  361. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  362. scene.add(mesh);
  363. }
  364. </pre>
  365. <p><a href="/docs/#api/ja/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> を使用するには、three.jsから追加のimportが必要です。
  366. ライトを可視化するために <a href="/docs/#api/ja/helpers/RectAreaLightHelper"><code class="notranslate" translate="no">RectAreaLightHelper</code></a> をimportします。</p>
  367. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  368. +import {RectAreaLightUniformsLib} from 'three/addons/lights/RectAreaLightUniformsLib.js';
  369. +import {RectAreaLightHelper} from 'three/addons/helpers/RectAreaLightHelper.js';
  370. </pre>
  371. <p><code class="notranslate" translate="no">RectAreaLightUniformsLib.init</code> を呼び出します。</p>
  372. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function main() {
  373. const canvas = document.querySelector('#c');
  374. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  375. + RectAreaLightUniformsLib.init();
  376. </pre>
  377. <p>RectAreaLightUniformsLibを忘れてもライトは動作しますが、見た目がおかしくなるので忘れないようにして下さい。</p>
  378. <p>これでライトを作れました。</p>
  379. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  380. *const intensity = 5;
  381. +const width = 12;
  382. +const height = 4;
  383. *const light = new THREE.RectAreaLight(color, intensity, width, height);
  384. light.position.set(0, 10, 0);
  385. +light.rotation.x = THREE.MathUtils.degToRad(-90);
  386. scene.add(light);
  387. *const helper = new RectAreaLightHelper(light);
  388. *light.add(helper);
  389. </pre>
  390. <p>注意すべき点は <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> や <a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> と異なり、<a href="/docs/#api/ja/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> はターゲットを使いません。
  391. その回転を利用しているだけです。
  392. もう1つ気をつける事は、ヘルパーがライトの子である必要があります。
  393. 他のヘルパーのようにシーンの子ではありません。</p>
  394. <p>GUIも調整してみましょう。
  395. ライトを回転させて <code class="notranslate" translate="no">width</code> と <code class="notranslate" translate="no">height</code> を調整できるようにします。</p>
  396. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  397. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  398. gui.add(light, 'intensity', 0, 10, 0.01);
  399. gui.add(light, 'width', 0, 20);
  400. gui.add(light, 'height', 0, 20);
  401. gui.add(new DegRadHelper(light.rotation, 'x'), 'value', -180, 180).name('x rotation');
  402. gui.add(new DegRadHelper(light.rotation, 'y'), 'value', -180, 180).name('y rotation');
  403. gui.add(new DegRadHelper(light.rotation, 'z'), 'value', -180, 180).name('z rotation');
  404. makeXYZGUI(gui, light.position, 'position');
  405. </pre>
  406. <p>そして、これが結果です。</p>
  407. <p></p><div translate="no" class="threejs_example_container notranslate">
  408. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-rectarea.html"></iframe></div>
  409. <a class="threejs_center" href="/manual/examples/lights-rectarea.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  410. </div>
  411. <p></p>
  412. <p>説明してない事が1つあり、 <a href="/docs/#api/ja/renderers/WebGLRenderer"><code class="notranslate" translate="no">WebGLRenderer</code></a> に <code class="notranslate" translate="no">physicallyCorrectLights</code> を設定します。
  413. ライトとの距離は、ライトの落ち方に影響を与えます。
  414. これは <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> と <a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> にのみ影響します。
  415. <a href="/docs/#api/ja/lights/RectAreaLight"><code class="notranslate" translate="no">RectAreaLight</code></a> は自動的にこれを行います。</p>
  416. <p>ライトの場合の基本的な考え方は、フェードアウトする距離を設定しない、<code class="notranslate" translate="no">intensity</code> を設定しない事です。
  417. 代わりにライトの<a href="/docs/#api/ja/lights/PointLight#power"><code class="notranslate" translate="no">power</code></a>をルーメン単位で設定すると、three.jsは実際のライトのように物理計算を行います。
  418. この場合の単位はメートルで、60Wの電球であれば800ルーメン程度の明るさになります。
  419. また<a href="/docs/#api/ja/lights/PointLight#decay"><code class="notranslate" translate="no">decay</code></a>プロパティもあります。
  420. 現実的な減衰のためには <code class="notranslate" translate="no">2</code> に設定します。</p>
  421. <p>試してみましょう。</p>
  422. <p>まずは物理的に正しいライトを作ります。</p>
  423. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  424. +renderer.physicallyCorrectLights = true;
  425. </pre>
  426. <p>次に <code class="notranslate" translate="no">power</code> を800ルーメンにし、<code class="notranslate" translate="no">decay</code> を2にします。
  427. <code class="notranslate" translate="no">distance</code> は <code class="notranslate" translate="no">Infinity</code> にします。</p>
  428. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  429. const intensity = 1;
  430. const light = new THREE.PointLight(color, intensity);
  431. light.power = 800;
  432. light.decay = 2;
  433. light.distance = Infinity;
  434. </pre>
  435. <p>GUIを追加して <code class="notranslate" translate="no">power</code> と <code class="notranslate" translate="no">decay</code> を変更できるようにします。</p>
  436. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  437. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  438. gui.add(light, 'decay', 0, 4, 0.01);
  439. gui.add(light, 'power', 0, 2000);
  440. </pre>
  441. <p></p><div translate="no" class="threejs_example_container notranslate">
  442. <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>
  443. <a class="threejs_center" href="/manual/examples/lights-point-physically-correct.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  444. </div>
  445. <p></p>
  446. <p>シーンにライトを追加するたびに、Three.jsのレンダリング速度が遅くなる事に注意して下さい。</p>
  447. <p>次は<a href="cameras.html">カメラの扱い方</a>についてです。</p>
  448. <p><canvas id="c"></canvas></p>
  449. <script type="module" src="../resources/threejs-lights.js"></script>
  450. </div>
  451. </div>
  452. </div>
  453. <script src="../resources/prettify.js"></script>
  454. <script src="../resources/lesson.js"></script>
  455. </body></html>