lights.html 32 KB

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