1
0

shadows.html 31 KB

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