2
0

shadows.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. まだ読んでいない場合、そこから始めると良いかもしれません。
  34. この記事を読む前に、<a href="cameras.html">前回のカメラの記事</a>と<a href="lights.html">ライトの記事</a>も読んでおくと良いです。</p>
  35. <p>コンピュータ上での影の表現は複雑なトピックになります。
  36. three.jsで利用できる解決策も含め様々な解決策がありますが、どれもトレードオフがあります。</p>
  37. <p>Three.jsは <em>シャドウマップ</em> をデフォルトで使用してます。
  38. シャドウマップを機能させるには、<em>全てのライトにシャドウを落とし、光源に対して全てのオブジェクトもシャドウを落としてレンダリングします</em>。
  39. 急ぐ必要はないので <strong> もう一度読んでみて下さい! </strong></p>
  40. <p>つまり、20個のオブジェクトと5個のライトがあり、全てのオブジェクトとライトにシャドウを落としている場合、シーン全体が6回描画されます。
  41. 全てのオブジェクトがライト#1、ライト#2、ライト#3に描画され、最初の5回の描画からデータを使って実際のシーンが描画されます。</p>
  42. <p>さらに悪い事に点光源がシャドウを落としている場合、6回もシーン描画しなければならないのです。</p>
  43. <p>これらの理由からシャドウを生成するライトをたくさん持つよりも、他の解決策を見つけるのが一般的です。
  44. 一般的な解決策は複数ライトを持つ事ですが、ディレクショナルライトでシャドウを生成する方法があります。</p>
  45. <p>もう1つの解決策はライトマップやアンビエントオクルージョンマップを使用し、オフラインでライティングの効果を事前計算する方法もあります。
  46. 静的なライティングのヒントになりますが、少なくともそれは速いです。
  47. その両方に関しては別の記事で取り上げます。</p>
  48. <p>もう1つの解決策はフェイクシャドウです。
  49. 平面を作り影に似たグレースケールのテクスチャを入れて、オブジェクト下の地面の上に描画します。</p>
  50. <p>例えばこのテクスチャをフェイクシャドウしてみましょう。</p>
  51. <div class="threejs_center"><img src="../examples/resources/images/roundshadow.png"></div>
  52. <p><a href="cameras.html">前回の記事</a>のコードの一部を使用します。</p>
  53. <p>背景色を白に設定してみましょう。</p>
  54. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  55. +scene.background = new THREE.Color('white');
  56. </pre>
  57. <p>同じチェッカーボードの地面を使いますが、今回の地面には照明は必要ないので <a href="/docs/#api/ja/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> を使用します。</p>
  58. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const loader = new THREE.TextureLoader();
  59. {
  60. 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. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  69. const planeMat = new THREE.MeshBasicMaterial({
  70. map: texture,
  71. side: THREE.DoubleSide,
  72. });
  73. + planeMat.color.setRGB(1.5, 1.5, 1.5);
  74. const mesh = new THREE.Mesh(planeGeo, planeMat);
  75. mesh.rotation.x = Math.PI * -.5;
  76. scene.add(mesh);
  77. }
  78. </pre>
  79. <p>色が <code class="notranslate" translate="no">1.5, 1.5, 1.5</code> である事に注意して下さい。
  80. これにより、チェッカーボードのテクスチャの色がそれぞれ1.5倍になります。
  81. テクスチャの色は 0x808080 と 0xC0C0C0 でミディアムグレーとライトグレーなので、1.5を掛けると白とライトグレーのチェッカーボードになります。</p>
  82. <p>シャドウテクスチャを読み込んでみましょう。</p>
  83. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const shadowTexture = loader.load('resources/images/roundshadow.png');
  84. </pre>
  85. <p>各球体と関連するオブジェクトを保持する配列を作成します。</p>
  86. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereShadowBases = [];
  87. </pre>
  88. <p>そして、球体のジオメトリを作ります。</p>
  89. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereRadius = 1;
  90. const sphereWidthDivisions = 32;
  91. const sphereHeightDivisions = 16;
  92. const sphereGeo = new THREE.SphereGeometry(sphereRadius, sphereWidthDivisions, sphereHeightDivisions);
  93. </pre>
  94. <p>フェイクシャドウのための平面のジオメトリも作ります。</p>
  95. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 1;
  96. const shadowGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  97. </pre>
  98. <p>そして、たくさんの球体を作ります。
  99. 各球体に対して <a href="/docs/#api/ja/core/Object3D"><code class="notranslate" translate="no">THREE.Object3D</code></a> を作成し <code class="notranslate" translate="no">base</code> に格納しシャドウの平面と球体メッシュの両方をbaseの子にします。
  100. これでbaseを動かすと、球体とシャドウの両方が動きます。
  101. Zファイティングを防ぐためにシャドウを少し上にします。
  102. また、<code class="notranslate" translate="no">depthWrite</code> をfalseにしてシャドウがお互いに混乱しないようにします。
  103. この2つの問題は<a href="transparency.html">別の記事</a>で解説します。
  104. このシャドウは照明が不要なので <a href="/docs/#api/ja/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a> にします。</p>
  105. <p>各球体を異なる色相、ベース、球体メッシュ、シャドウのメッシュ、各球体のyの初期位置を保存します。</p>
  106. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const numSpheres = 15;
  107. for (let i = 0; i &lt; numSpheres; ++i) {
  108. // make a base for the shadow and the sphere
  109. // so they move together.
  110. const base = new THREE.Object3D();
  111. scene.add(base);
  112. // add the shadow to the base
  113. // note: we make a new material for each sphere
  114. // so we can set that sphere's material transparency
  115. // separately.
  116. const shadowMat = new THREE.MeshBasicMaterial({
  117. map: shadowTexture,
  118. transparent: true, // so we can see the ground
  119. depthWrite: false, // so we don't have to sort
  120. });
  121. const shadowMesh = new THREE.Mesh(shadowGeo, shadowMat);
  122. shadowMesh.position.y = 0.001; // so we're above the ground slightly
  123. shadowMesh.rotation.x = Math.PI * -.5;
  124. const shadowSize = sphereRadius * 4;
  125. shadowMesh.scale.set(shadowSize, shadowSize, shadowSize);
  126. base.add(shadowMesh);
  127. // add the sphere to the base
  128. const u = i / numSpheres; // goes from 0 to 1 as we iterate the spheres.
  129. const sphereMat = new THREE.MeshPhongMaterial();
  130. sphereMat.color.setHSL(u, 1, .75);
  131. const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
  132. sphereMesh.position.set(0, sphereRadius + 2, 0);
  133. base.add(sphereMesh);
  134. // remember all 3 plus the y position
  135. sphereShadowBases.push({base, sphereMesh, shadowMesh, y: sphereMesh.position.y});
  136. }
  137. </pre>
  138. <p>2つのライトを設定しました。
  139. 1つは <a href="/docs/#api/ja/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a> で強度2にしました。</p>
  140. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  141. const skyColor = 0xB1E1FF; // light blue
  142. const groundColor = 0xB97A20; // brownish orange
  143. const intensity = 2;
  144. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  145. scene.add(light);
  146. }
  147. </pre>
  148. <p>もう1つは <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> で球体はいくつかの定義を得られます。</p>
  149. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  150. const color = 0xFFFFFF;
  151. const intensity = 1;
  152. const light = new THREE.DirectionalLight(color, intensity);
  153. light.position.set(0, 10, 5);
  154. light.target.position.set(-5, 0, 0);
  155. scene.add(light);
  156. scene.add(light.target);
  157. }
  158. </pre>
  159. <p>そのままレンダリングしてますが、球体をアニメーション化してみましょう。
  160. それぞれの球体、シャドウ、baseのセットに対して、
  161. baseをxz平面内で移動させて <a href="/docs/#api/ja/math/Math.abs(Math.sin(time))"><code class="notranslate" translate="no">Math.abs(Math.sin(time))</code></a> で球体を上下に移動させると弾むようなアニメーションします。
  162. シャドウのマテリアルの不透明度を設定し、各球体が高くなるにつれてシャドウを薄くなるようにしています。</p>
  163. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  164. time *= 0.001; // convert to seconds
  165. ...
  166. sphereShadowBases.forEach((sphereShadowBase, ndx) =&gt; {
  167. const {base, sphereMesh, shadowMesh, y} = sphereShadowBase;
  168. // u is a value that goes from 0 to 1 as we iterate the spheres
  169. const u = ndx / sphereShadowBases.length;
  170. // compute a position for the base. This will move
  171. // both the sphere and its shadow
  172. const speed = time * .2;
  173. const angle = speed + u * Math.PI * 2 * (ndx % 1 ? 1 : -1);
  174. const radius = Math.sin(speed - ndx) * 10;
  175. base.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
  176. // yOff is a value that goes from 0 to 1
  177. const yOff = Math.abs(Math.sin(time * 2 + ndx));
  178. // move the sphere up and down
  179. sphereMesh.position.y = y + THREE.MathUtils.lerp(-2, 2, yOff);
  180. // fade the shadow as the sphere goes up
  181. shadowMesh.material.opacity = THREE.MathUtils.lerp(1, .25, yOff);
  182. });
  183. ...
  184. </pre>
  185. <p>そして、ここに15種類の跳ねるボールがあります。</p>
  186. <p></p><div translate="no" class="threejs_example_container notranslate">
  187. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-fake.html"></iframe></div>
  188. <a class="threejs_center" href="/manual/examples/shadows-fake.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  189. </div>
  190. <p></p>
  191. <p>全てのオブジェクトに丸や楕円形のシャドウを使用するのが一般的です。
  192. 異なる形状のシャドウのテクスチャを使用できます。
  193. シャドウをハードエッジでギザギザにしてもいいかもしれません。
  194. このタイプのシャドウを使った良い例が<a href="https://www.google.com/search?tbm=isch&amp;q=animal+crossing+pocket+camp+screenshots">どうぶつの森 ポケットキャンプ</a>です。
  195. それぞれのキャラクターがシンプルな丸いシャドウになっており、レンダリングコストが低く効果的です。
  196. <a href="https://www.google.com/search?q=monument+valley+screenshots&amp;tbm=isch">モニュメントバレー</a>では、メインキャラクターにもこのシャドウが使われているようです。</p>
  197. <p>そこでシャドウマップに移りますが、シャドウを落とす事ができるライトが3つあります。
  198. <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> と <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> です。</p>
  199. <p>まずは、<a href="lights.html">ライトの記事</a>のヘルパーの例を参考に <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> を使ってみましょう。</p>
  200. <p>最初にレンダラーのシャドウを有効にします。</p>
  201. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  202. +renderer.shadowMap.enabled = true;
  203. </pre>
  204. <p>そして、シャドウを落とすためにライトのcastShadowを有効にします。</p>
  205. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const light = new THREE.DirectionalLight(color, intensity);
  206. +light.castShadow = true;
  207. </pre>
  208. <p>シーン内の各メッシュを見て、シャドウを落とすか受け取るか決めます。</p>
  209. <p>下敷きになっているものはあまり気にせず、平面(地面)はシャドウだけを受けるようにしましょう。</p>
  210. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(planeGeo, planeMat);
  211. mesh.receiveShadow = true;
  212. </pre>
  213. <p>立方体と球体はシャドウを落とし受け取るようにしましょう。</p>
  214. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  215. mesh.castShadow = true;
  216. mesh.receiveShadow = true;
  217. ...
  218. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  219. mesh.castShadow = true;
  220. mesh.receiveShadow = true;
  221. </pre>
  222. <p>これを実行してみます。</p>
  223. <p></p><div translate="no" class="threejs_example_container notranslate">
  224. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light.html"></iframe></div>
  225. <a class="threejs_center" href="/manual/examples/shadows-directional-light.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  226. </div>
  227. <p></p>
  228. <p>何が起こったのでしょうか?
  229. なぜ影の一部が欠けているのでしょうか?</p>
  230. <p>これはシャドウマップは光の視点でシーンをレンダリングし作成されるからです。
  231. この場合、<a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> にカメラがあり、ターゲットを見ています。
  232. <a href="cameras.html">以前取り上げたカメラと同じように</a>
  233. ライトのシャドウカメラは影がレンダリングされ、内部の領域を定義します。
  234. 上記の例ではその面積が小さすぎます。</p>
  235. <p>その領域を可視化するために、ライトのシャドウカメラを取得して <a href="/docs/#api/ja/helpers/CameraHelper"><code class="notranslate" translate="no">CameraHelper</code></a> をシーンに追加します。</p>
  236. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  237. scene.add(cameraHelper);
  238. </pre>
  239. <p>これでシャドウが落とされ受け取れる領域が見えるようになりました。</p>
  240. <p></p><div translate="no" class="threejs_example_container notranslate">
  241. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light-with-camera-helper.html"></iframe></div>
  242. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-helper.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  243. </div>
  244. <p></p>
  245. <p>ターゲットのX値を前後に調整すると、ライトのシャドウカメラボックスの中にあるものだけが影を描画する場所が明確になります。</p>
  246. <p>ライトのシャドウカメラを調整するとその箱の大きさを調整できます。</p>
  247. <p>ライトのシャドウカメラボックスを調整するためのGUIを追加してみましょう。
  248. <code class="notranslate" translate="no">DirectionLight</code> は全ての光が平行な方向に進むので、<a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> はシャドウカメラに <a href="/docs/#api/ja/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> を使います。
  249. <a href="cameras.html">以前のカメラの記事</a>で <a href="/docs/#api/ja/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> がどのように動作するかを説明しました。</p>
  250. <p><a href="/docs/#api/ja/cameras/OrthographicCamera"><code class="notranslate" translate="no">OrthographicCamera</code></a> は、<code class="notranslate" translate="no">left</code>、<code class="notranslate" translate="no">right</code>、<code class="notranslate" translate="no">top</code>、<code class="notranslate" translate="no">bottom</code>、<code class="notranslate" translate="no">near</code>、<code class="notranslate" translate="no">far</code>、<code class="notranslate" translate="no">zoom</code> プロパティでその箱、または <em>錐台の視点</em> を定義してる事を思い出して下さい。</p>
  251. <p>ここでもlil-guiのヘルパークラスを作ってみましょう。
  252. オブジェクトと2つのプロパティを渡す <code class="notranslate" translate="no">DimensionGUIHelper</code> を作ります。
  253. lil-guiが調整できるプロパティを追加し、2つのプロパティの正と負の値を設定します。
  254. これを使い <code class="notranslate" translate="no">left</code> と <code class="notranslate" translate="no">right</code> を <code class="notranslate" translate="no">width</code> に、<code class="notranslate" translate="no">up</code> と <code class="notranslate" translate="no">down</code> を <code class="notranslate" translate="no">height</code> に設定します。</p>
  255. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class DimensionGUIHelper {
  256. constructor(obj, minProp, maxProp) {
  257. this.obj = obj;
  258. this.minProp = minProp;
  259. this.maxProp = maxProp;
  260. }
  261. get value() {
  262. return this.obj[this.maxProp] * 2;
  263. }
  264. set value(v) {
  265. this.obj[this.maxProp] = v / 2;
  266. this.obj[this.minProp] = v / -2;
  267. }
  268. }
  269. </pre>
  270. <p><a href="cameras.html">カメラの記事</a>で作成した <code class="notranslate" translate="no">MinMaxGUIHelper</code> を使い <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> を調整します。</p>
  271. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  272. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  273. gui.add(light, 'intensity', 0, 2, 0.01);
  274. +{
  275. + const folder = gui.addFolder('Shadow Camera');
  276. + folder.open();
  277. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'left', 'right'), 'value', 1, 100)
  278. + .name('width')
  279. + .onChange(updateCamera);
  280. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'bottom', 'top'), 'value', 1, 100)
  281. + .name('height')
  282. + .onChange(updateCamera);
  283. + const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  284. + folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  285. + folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  286. + folder.add(light.shadow.camera, 'zoom', 0.01, 1.5, 0.01).onChange(updateCamera);
  287. +}
  288. </pre>
  289. <p>何か値が変更された時は <code class="notranslate" translate="no">updateCamera</code> 関数を呼び出すようにします。
  290. ライトやヘルパー、ライトのシャドウカメラやカメラのヘルパーを更新するupdateCamera関数を書いてみましょう。</p>
  291. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateCamera() {
  292. // update the light target's matrixWorld because it's needed by the helper
  293. light.target.updateMatrixWorld();
  294. helper.update();
  295. // update the light's shadow camera's projection matrix
  296. light.shadow.camera.updateProjectionMatrix();
  297. // and now update the camera helper we're using to show the light's shadow camera
  298. cameraHelper.update();
  299. }
  300. updateCamera();
  301. </pre>
  302. <p>これでライトのシャドウカメラにGUIを追加したので値を変更できます。</p>
  303. <p></p><div translate="no" class="threejs_example_container notranslate">
  304. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light-with-camera-gui.html"></iframe></div>
  305. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-gui.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  306. </div>
  307. <p></p>
  308. <p><code class="notranslate" translate="no">width</code> と <code class="notranslate" translate="no">height</code> を30ぐらいにすると、シャドウが正しく描画されこのシーンでシャドウにする設定が完全にカバーできました。</p>
  309. <p>しかし、ここで疑問が湧いてきます。
  310. なぜ <code class="notranslate" translate="no">width</code> と <code class="notranslate" translate="no">height</code> に巨大な数値を設定して全てをカバーしないのでしょうか?
  311. <code class="notranslate" translate="no">width</code> と <code class="notranslate" translate="no">height</code> を100にすると、以下のようなものが表示されます。</p>
  312. <div class="threejs_center"><img src="../resources/images/low-res-shadow-map.png" style="width: 369px"></div>
  313. <p>この低解像度のシャドウはどうなっているでしょうか!?</p>
  314. <p>この問題はシャドウに関連した設定を意識する必要があります。
  315. シャドウマップとはシャドウが描かれるテクスチャです。
  316. このテクスチャはサイズがあります。
  317. 上記で設定したシャドウカメラの領域はその大きさになっています。
  318. つまり、設定した面積が大きいほどシャドウのブロックが多くなります。</p>
  319. <p>シャドウマップのテクスチャの解像度は <code class="notranslate" translate="no">light.shadow.mapSize.width</code> と <code class="notranslate" translate="no">light.shadow.mapSize.height</code> で設定できます。
  320. デフォルトは512 x 512です。
  321. 大きくするほどメモリを消費し計算が遅くなるので、できるだけ小さく設定しシーンを動作させたいです。
  322. ライトのシャドウカメラ領域も同様です。
  323. 小さくすると影の見栄えが良くなるので、面積を小さくしてシーンをカバーしましょう。
  324. 各ユーザーのコンピューターには、利用可能な最大テクスチャサイズがある事に注意して下さい。
  325. <a href="/docs/#api/ja/renderers/WebGLRenderer#capabilities"><code class="notranslate" translate="no">renderer.capabilities.maxTextureSize</code></a>で利用可能な最大テクスチャサイズがわかります。</p>
  326. <!--
  327. Ok but what about `near` and `far` I hear you thinking. Can we set `near` to 0.00001 and far to `100000000`
  328. -->
  329. <p><a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> に切り替えると、ライトのシャドウカメラは <a href="/docs/#api/ja/cameras/PerspectiveCamera"><code class="notranslate" translate="no">PerspectiveCamera</code></a> になります。
  330. <a href="/docs/#api/ja/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> のシャドウカメラの設定を手動で行えます。
  331. ただ、<a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> のシャドウカメラは <a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> 自身によって制御されます。
  332. シャドウカメラの <code class="notranslate" translate="no">fov</code> は <a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> の <code class="notranslate" translate="no">angle</code> に接続しています。
  333. <code class="notranslate" translate="no">aspect</code> はシャドウマップのサイズによって自動的に設定されます。</p>
  334. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.DirectionalLight(color, intensity);
  335. +const light = new THREE.SpotLight(color, intensity);
  336. </pre>
  337. <p><a href="lights.html">ライトの記事</a>にあった <code class="notranslate" translate="no">penumbra</code> と <code class="notranslate" translate="no">angle</code> の設定を元に戻しました。</p>
  338. <p></p><div translate="no" class="threejs_example_container notranslate">
  339. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-spot-light-with-camera-gui.html"></iframe></div>
  340. <a class="threejs_center" href="/manual/examples/shadows-spot-light-with-camera-gui.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  341. </div>
  342. <p></p>
  343. <!--
  344. You can notice, just like the last example if we set the angle high
  345. then the shadow map, the texture is spread over a very large area and
  346. the resolution of our shadows gets really low.
  347. div class="threejs_center"><img src="../resources/images/low-res-shadow-map-spotlight.png" style="width: 344px"></div>
  348. You can increase the size of the shadow map as mentioned above. You can
  349. also blur the result
  350. <div translate="no" class="threejs_example_container notranslate">
  351. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-spot-light-with-shadow-radius"></iframe></div>
  352. <a class="threejs_center" href="/manual/examples/shadows-spot-light-with-shadow-radius" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  353. </div>
  354. -->
  355. <p>そして最後に <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> でシャドウをつけます。
  356. <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> は全方向に光を放つので関連する設定は <code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> だけです。
  357. それ以外の場合、<a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> のシャドウは、効果的な6つの <a href="/docs/#api/ja/lights/SpotLight"><code class="notranslate" translate="no">SpotLight</code></a> のシャドウになります。
  358. これは <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> のシャドウの描画が非常に遅くなります。</p>
  359. <p>シーンの周りに箱を置いて、壁や天井にシャドウが見えるようにしてみましょう。
  360. マテリアルの <code class="notranslate" translate="no">side</code> プロパティを <code class="notranslate" translate="no">THREE.BackSide</code> に設定します。
  361. これで箱の外側ではなく内側をレンダリングしています。
  362. 床のようにシャドウを受けるように設定します。
  363. また、箱の底が床より少し下になるように箱の位置を設定し、床と箱がズレないようにします。</p>
  364. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  365. const cubeSize = 30;
  366. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  367. const cubeMat = new THREE.MeshPhongMaterial({
  368. color: '#CCC',
  369. side: THREE.BackSide,
  370. });
  371. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  372. mesh.receiveShadow = true;
  373. mesh.position.set(0, cubeSize / 2 - 0.1, 0);
  374. scene.add(mesh);
  375. }
  376. </pre>
  377. <p>そして、ライトを <a href="/docs/#api/ja/lights/PointLight"><code class="notranslate" translate="no">PointLight</code></a> に切り替えます。</p>
  378. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.SpotLight(color, intensity);
  379. +const light = new THREE.PointLight(color, intensity);
  380. ....
  381. // so we can easily see where the point light is
  382. +const helper = new THREE.PointLightHelper(light);
  383. +scene.add(helper);
  384. </pre>
  385. <p></p><div translate="no" class="threejs_example_container notranslate">
  386. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-point-light.html"></iframe></div>
  387. <a class="threejs_center" href="/manual/examples/shadows-point-light.html" target="_blank">ここをクリックして別のウィンドウで開きます</a>
  388. </div>
  389. <p></p>
  390. <p>GUIの <code class="notranslate" translate="no">position</code> を使ってライトを移動させると、壁一面にシャドウが落ちます。
  391. また、<code class="notranslate" translate="no">near</code> と <code class="notranslate" translate="no">far</code> の設定を調整できます。
  392. <code class="notranslate" translate="no">near</code> よりも近い時にはシャドウを受け取らず、<code class="notranslate" translate="no">far</code> よりも遠い時には常にシャドウになっています。</p>
  393. <!--
  394. self shadow, shadow acne
  395. -->
  396. </div>
  397. </div>
  398. </div>
  399. <script src="../resources/prettify.js"></script>
  400. <script src="../resources/lesson.js"></script>
  401. </body></html>