lights.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>のライト</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>この記事はThree.jsの連載記事の1つです。
  29. 最初の記事は<a href="fundamentals.html">Three.jsの基礎知識</a>です。
  30. まだ読んでいない場合は、Three.jsの基礎知識や<a href="setup.html">セットアップ</a>から始めると良いと思います。
  31. 前回の記事は<a href="textures.html">テクスチャ</a>でした。</p>
  32. <p>今回はthree.jsの色々な種類のライトの使い方を確認していきます。</p>
  33. <p>前回のサンプルコードからカメラの設定を修正しましょう。
  34. 視野角(fov)を45度にし、遠平面(far)を100、カメラを原点からY座標に10、Z座標を20にします。</p>
  35. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">*const fov = 45;
  36. const aspect = 2; // the canvas default
  37. const near = 0.1;
  38. *const far = 100;
  39. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  40. +camera.position.set(0, 10, 20);
  41. </pre>
  42. <p>次に <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> を追加します。
  43. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> は、カメラをある点を中心に<em>軌道</em>を回転できます。
  44. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> はthree.jsのオプション機能なので、importする必要があります。</p>
  45. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">import * as THREE from 'three';
  46. +import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
  47. </pre>
  48. <p>これでOrbitControlsを利用できます。
  49. <a href="/docs/#examples/controls/OrbitControls"><code class="notranslate" translate="no">OrbitControls</code></a> にカメラと入力イベントを取得するDOM要素を渡します。</p>
  50. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const controls = new OrbitControls(camera, canvas);
  51. controls.target.set(0, 5, 0);
  52. controls.update();
  53. </pre>
  54. <p>controls.targetのY座標を5にして <code class="notranslate" translate="no">controls.update</code> を呼び出します。</p>
  55. <p>次はライトアップするオブジェクトを作ってみましょう。
  56. まずは地上となる平面を作ります。
  57. この平面に2 x 2ピクセルの小さなチェッカーボードのテクスチャを適用します。</p>
  58. <div class="threejs_center">
  59. <img src="../examples/resources/images/checker.png" class="border" style="
  60. image-rendering: pixelated;
  61. width: 128px;
  62. ">
  63. </div>
  64. <p>最初にテクスチャを読み込み、何回テクスチャのリピートを繰り返すかを設定し、フィルターはニアレスト(nearest)にします。
  65. テクスチャは2 x 2ピクセルのチェッカーボードです。
  66. テクスチャのリピートは平面の半分の大きさにし、チェッカーボードの1つのチェック部分は1にします。</p>
  67. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 40;
  68. const loader = new THREE.TextureLoader();
  69. const texture = loader.load('resources/images/checker.png');
  70. texture.wrapS = THREE.RepeatWrapping;
  71. texture.wrapT = THREE.RepeatWrapping;
  72. texture.magFilter = THREE.NearestFilter;
  73. texture.colorSpace = THREE.SRGBColorSpace;
  74. const repeats = planeSize / 2;
  75. texture.repeat.set(repeats, repeats);
  76. </pre>
  77. <p>平面のジオメトリとマテリアルを作り、それを元にシーンに追加するメッシュを作ります。
  78. 平面のデフォルトは縦向きなので横向きになるように回転します。</p>
  79. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  80. const planeMat = new THREE.MeshPhongMaterial({
  81. map: texture,
  82. side: THREE.DoubleSide,
  83. });
  84. const mesh = new THREE.Mesh(planeGeo, planeMat);
  85. mesh.rotation.x = Math.PI * -.5;
  86. scene.add(mesh);
  87. </pre>
  88. <p>キューブと球体を追加し、平面を含めて3つのオブジェクトをライティングします。</p>
  89. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  90. const cubeSize = 4;
  91. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  92. const cubeMat = new THREE.MeshPhongMaterial({color: '#8AC'});
  93. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  94. mesh.position.set(cubeSize + 1, cubeSize / 2, 0);
  95. scene.add(mesh);
  96. }
  97. {
  98. const sphereRadius = 3;
  99. const sphereWidthDivisions = 32;
  100. const sphereHeightDivisions = 16;
  101. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  102. const sphereMat = new THREE.MeshPhongMaterial({color: '#CA8'});
  103. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  104. mesh.position.set(-sphereRadius - 1, sphereRadius + 2, 0);
  105. scene.add(mesh);
  106. }
  107. </pre>
  108. <p>ライトアップするシーンができたのでライトを追加しましょう!</p>
  109. <h2 id="-ambientlight-"><code class="notranslate" translate="no">AmbientLight(環境光源)</code></h2>
  110. <p>最初に <a href="/docs/#api/ja/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a> を作りましょう。</p>
  111. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  112. const intensity = 1;
  113. const light = new THREE.AmbientLight(color, intensity);
  114. scene.add(light);
  115. </pre>
  116. <p>ライトのパラメーターを調整できるようにします。
  117. 今回も<a href="https://github.com/georgealways/lil-gui">lil-gui</a>を使います。
  118. lil-guiで色を調整するにはヘルパーが必要です。
  119. プロパティをlil-guiにCSSの16進数の形で表示します(例: <code class="notranslate" translate="no">#FF8844</code>)。
  120. ヘルパーは名前付きプロパティから色を取得し、16進数の文字列に変換してlil-guiに渡します。lil-guiがヘルパーのプロパティを設定し、結果をライトの色に戻します。</p>
  121. <p>これがヘルパーです。</p>
  122. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class ColorGUIHelper {
  123. constructor(object, prop) {
  124. this.object = object;
  125. this.prop = prop;
  126. }
  127. get value() {
  128. return `#${this.object[this.prop].getHexString()}`;
  129. }
  130. set value(hexString) {
  131. this.object[this.prop].set(hexString);
  132. }
  133. }
  134. </pre>
  135. <p>lil-guiの設定は以下の通りです。</p>
  136. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  137. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  138. gui.add(light, 'intensity', 0, 2, 0.01);
  139. </pre>
  140. <p>これで以下のような結果になります。</p>
  141. <p></p><div translate="no" class="threejs_example_container notranslate">
  142. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-ambient.html"></iframe></div>
  143. <a class="threejs_center" href="/manual/examples/lights-ambient.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  144. </div>
  145. <p></p>
  146. <p>シーンをクリックしてドラッグして、カメラを<em>軌道</em>に乗せます。</p>
  147. <p>環境光源だけでは正しくライティング表現できてません。キューブと球体に陰影がなく形状が平面に見えます。
  148. 以下のように <a href="/docs/#api/ja/lights/AmbientLight"><code class="notranslate" translate="no">AmbientLight</code></a> はマテリアルの色とライトの色、ライトの強度(intensity)を掛けてます。</p>
  149. <pre class="prettyprint showlinemods notranslate notranslate" translate="no">color = materialColor * light.color * light.intensity;
  150. </pre><p>それだけで方向性がないです。
  151. この環境光源は100%均一でシーン内の全ての色を変える以外は<em>ライティング</em>としてはあまり役に立ちません。
  152. 環境光源は暗すぎない暗さを作る事ができます。</p>
  153. <h2 id="-hemispherelight-"><code class="notranslate" translate="no">HemisphereLight(半球光源)</code></h2>
  154. <p>コードを <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> に切り替えてみましょう。
  155. <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> は空と地面の色を取得し、その2色とマテリアルの色を掛け合わせます。</p>
  156. <p>これが新しいコードです。</p>
  157. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const color = 0xFFFFFF;
  158. +const skyColor = 0xB1E1FF; // light blue
  159. +const groundColor = 0xB97A20; // brownish orange
  160. const intensity = 1;
  161. -const light = new THREE.AmbientLight(color, intensity);
  162. +const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  163. scene.add(light);
  164. </pre>
  165. <p>lil-guiのコードを修正し、両方の色を編集してみましょう。</p>
  166. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  167. -gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  168. +gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('skyColor');
  169. +gui.addColor(new ColorGUIHelper(light, 'groundColor'), 'value').name('groundColor');
  170. gui.add(light, 'intensity', 0, 2, 0.01);
  171. </pre>
  172. <p>これが結果です。</p>
  173. <p></p><div translate="no" class="threejs_example_container notranslate">
  174. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-hemisphere.html"></iframe></div>
  175. <a class="threejs_center" href="/manual/examples/lights-hemisphere.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  176. </div>
  177. <p></p>
  178. <p>まだ正しくライティング表現ができてなく、キューブと球体が平面に見えます。
  179. 別のライトと組み合わせて使用される <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> は、空や地面の色に良い影響を与えます。
  180. この方法では他のライトと組み合わせて使うか、<a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> を代わりに使うのがベストです。</p>
  181. <h2 id="-directionallight-"><code class="notranslate" translate="no">DirectionalLight(平行光源)</code></h2>
  182. <p>コードを <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> に切り替えてみましょう。
  183. <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> は太陽を表すのによく使われます。</p>
  184. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  185. const intensity = 1;
  186. const light = new THREE.DirectionalLight(color, intensity);
  187. light.position.set(0, 10, 0);
  188. light.target.position.set(-5, 0, 0);
  189. scene.add(light);
  190. scene.add(light.target);
  191. </pre>
  192. <p>シーンに <code class="notranslate" translate="no">light</code> と <code class="notranslate" translate="no">light.target</code> を追加する必要があります。
  193. three.jsの <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> はターゲットの方向にライティングします。</p>
  194. <p>GUIに追加してlight.targetを動かせるようにしてみましょう。</p>
  195. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  196. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  197. gui.add(light, 'intensity', 0, 2, 0.01);
  198. gui.add(light.target.position, 'x', -10, 10);
  199. gui.add(light.target.position, 'z', -10, 10);
  200. gui.add(light.target.position, 'y', 0, 10);
  201. </pre>
  202. <p></p><div translate="no" class="threejs_example_container notranslate">
  203. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-directional.html"></iframe></div>
  204. <a class="threejs_center" href="/manual/examples/lights-directional.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  205. </div>
  206. <p></p>
  207. <p>なんだか見づらいですね。
  208. Three.jsにはシーンに追加できるヘルパーオブジェクトがたくさんあり、シーンの見えない部分を視覚化するのに役立ちます。
  209. 今回は <a href="/docs/#api/ja/helpers/DirectionalLightHelper"><code class="notranslate" translate="no">DirectionalLightHelper</code></a> を使い、ライトから平面までの線を描画します。
  210. lightをDirectionalLightHelperに渡してシーンに追加します。</p>
  211. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const helper = new THREE.DirectionalLightHelper(light);
  212. scene.add(helper);
  213. </pre>
  214. <p>ライトの位置とターゲットの両方を設定できるようにしておきましょう。
  215. <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>
  216. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function makeXYZGUI(gui, vector3, name, onChangeFn) {
  217. const folder = gui.addFolder(name);
  218. folder.add(vector3, 'x', -10, 10).onChange(onChangeFn);
  219. folder.add(vector3, 'y', 0, 10).onChange(onChangeFn);
  220. folder.add(vector3, 'z', -10, 10).onChange(onChangeFn);
  221. folder.open();
  222. }
  223. </pre>
  224. <p>変更時は常にヘルパーの <code class="notranslate" translate="no">update</code> 関数を呼び出す必要があります。
  225. そのため、lil-guiが値を更新時に <code class="notranslate" translate="no">onChangeFn</code> 関数を渡しています。</p>
  226. <p>makeXYZGUI関数はlight.positionとlight.target.positionの両方に使えます。</p>
  227. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+function updateLight() {
  228. + light.target.updateMatrixWorld();
  229. + helper.update();
  230. +}
  231. +updateLight();
  232. const gui = new GUI();
  233. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  234. gui.add(light, 'intensity', 0, 2, 0.01);
  235. +makeXYZGUI(gui, light.position, 'position', updateLight);
  236. +makeXYZGUI(gui, light.target.position, 'target', updateLight);
  237. </pre>
  238. <p>これでライトを動かす事ができるようになりました。</p>
  239. <p></p><div translate="no" class="threejs_example_container notranslate">
  240. <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>
  241. <a class="threejs_center" href="/manual/examples/lights-directional-w-helper.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  242. </div>
  243. <p></p>
  244. <p>カメラを軌道に乗せると見やすくなります。
  245. この平面は <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> を表しており、DirectionalLightが一方向からのライティングを計算します。
  246. 光の出所は<em>点</em>ではなく、平面を無限に照らす平行光線です。</p>
  247. <h2 id="-pointlight-"><code class="notranslate" translate="no">PointLight(点光源)</code></h2>
  248. <p><a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> はある点から全方向に光を放つライトです。
  249. コード変更しましょう。</p>
  250. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  251. -const intensity = 1;
  252. +const intensity = 150;
  253. -const light = new THREE.DirectionalLight(color, intensity);
  254. +const light = new THREE.PointLight(color, intensity);
  255. light.position.set(0, 10, 0);
  256. -light.target.position.set(-5, 0, 0);
  257. scene.add(light);
  258. -scene.add(light.target);
  259. </pre>
  260. <p><a href="/docs/#api/ja/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a> に切り替えます。</p>
  261. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const helper = new THREE.DirectionalLightHelper(light);
  262. +const helper = new THREE.PointLightHelper(light);
  263. scene.add(helper);
  264. </pre>
  265. <p>light.targetがないので <code class="notranslate" translate="no">onChange</code> 関数はもっとシンプルになります。</p>
  266. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateLight() {
  267. - light.target.updateMatrixWorld();
  268. helper.update();
  269. }
  270. -updateLight();
  271. </pre>
  272. <p><a href="/docs/#api/ja/helpers/PointLightHelper"><code class="notranslate" translate="no">PointLightHelper</code></a> には点がない事に注意して下さい。
  273. 小さなダイヤモンドのワイヤーフレームを描画します。
  274. 簡単に望む任意の形状にできて、ライト自体にメッシュを追加します。</p>
  275. <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>プロパティを持ちます。
  276. <code class="notranslate" translate="no">distance</code> が0ならば <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> は無限大に輝きます。
  277. <code class="notranslate" translate="no">distance</code> が0よりも大きい場合、ライトに向かってライトの全強度を照らし、ライトから離れた <code class="notranslate" translate="no">distance</code> では影響を受けないようにフェードアウトします。</p>
  278. <p>distanceを調整できるようにGUIを設定してみましょう。</p>
  279. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  280. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  281. gui.add(light, 'intensity', 0, 2, 0.01);
  282. +gui.add(light, 'distance', 0, 40).onChange(updateLight);
  283. makeXYZGUI(gui, light.position, 'position', updateLight);
  284. -makeXYZGUI(gui, light.target.position, 'target', updateLight);
  285. </pre>
  286. <p>これを試してみて下さい。</p>
  287. <p></p><div translate="no" class="threejs_example_container notranslate">
  288. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/lights-point.html"></iframe></div>
  289. <a class="threejs_center" href="/manual/examples/lights-point.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  290. </div>
  291. <p></p>
  292. <p><code class="notranslate" translate="no">distance</code> が &gt; 0 の時にライトがフェードアウトしている事に注目して下さい。</p>
  293. <h2 id="-spotlight-"><code class="notranslate" translate="no">SpotLight(集中光線)</code></h2>
  294. <p>集中光源は円錐体にライティングする時に効果的です。
  295. 実際は2つの円錐体があります。外側と内側の円錐体です。
  296. 内側と外側の円錐体の間では、ライトは強度のMAX値から0にフェードします。</p>
  297. <p><a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> を使うには、平行光源と同じようにターゲットが必要です。
  298. ライトの円錐体がターゲットに向かって照らされます。</p>
  299. <p>上記のヘルパーを使って <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> を修正します。</p>
  300. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const color = 0xFFFFFF;
  301. -const intensity = 1;
  302. +const intensity = 150;
  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>シーンにライトを追加するたびに、Three.jsのレンダリング速度が遅くなる事に注意して下さい。</p>
  413. <p>次は<a href="cameras.html">カメラの扱い方</a>についてです。</p>
  414. <p><canvas id="c"></canvas></p>
  415. <script type="module" src="../resources/threejs-lights.js"></script>
  416. </div>
  417. </div>
  418. </div>
  419. <script src="../resources/prettify.js"></script>
  420. <script src="../resources/lesson.js"></script>
  421. </body></html>