shadows.html 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <!DOCTYPE html><html lang="zh"><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. <link rel="stylesheet" href="/manual/zh/lang.css">
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="lesson-title">
  25. <h1>阴影</h1>
  26. </div>
  27. <div class="lesson">
  28. <div class="lesson-main">
  29. <p>本文是 three.js 系列文章中的一部分。第一篇文章为 <a href="fundamentals.html">three.js 基础</a>。如果你是个新手,还没读过,请从那里开始。
  30. <a href="cameras.html">前一篇文章关于相机</a>,<a href="lights.html">再前一遍文章关于灯光</a>,这些文章都很重要。</p>
  31. <p>电脑中的阴影可以是一个很复杂的话题。有各种各样的解决方案,所有这些都有权衡,包括 three.js 中可用的解决方案。</p>
  32. <p>Three.js 默认使用<em>shadow maps(阴影贴图)</em>,阴影贴图的工作方式就是具有投射阴影的光能对所有能被投射阴影的物体从光源渲染阴影。<strong>请再读一遍,试着去理解并记住</strong></p>
  33. <p>换句话说,如果你有 20 个物体对象、5 个灯光,并且所有的物体都能被投射阴影,所有的光都能投射阴影,那么这个场景这个场景将会绘制 6 次。第一个灯光将会为所有的物体投影阴影,绘制场景。然后是第二个灯光绘制场景,然后是第三个灯光,以此类推。最后一次(即第六次)将通过前五个灯光渲染的数据,渲染出最终的实际场景。</p>
  34. <p>糟糕的是,如果你有一个能投射阴影点光源在这个场景中,那个这个场景将会为这个点光源再绘制 6 次。</p>
  35. <p>由于这些原因,除了寻找其他根本上的解决方案去解决一堆光源都能投射阴影的性能问题。一般还有常见的解决方案,就是允许多个光源,但只让一个光源能投射阴影。</p>
  36. <p>另一个解决方案就是使用光照贴图或者环境光贴图,预先计算离线照明的效果。这将导致静态光照,但是至少该方案渲染得非常快。在另一篇文章中将涵盖这两个解决方案。</p>
  37. <p>其他的解决方案是使用假的阴影。举个例子,创建一个平面,在平面上放一个近似阴影的灰度纹理,把它画在物体下面的地面上。</p>
  38. <p>这个例子我们将使用假阴影</p>
  39. <div class="threejs_center"><img src="../examples/resources/images/roundshadow.png"></div>
  40. <p>我们使用 <a href="cameras.html">前一篇文章</a>的代码.</p>
  41. <p>首先让我们将场景的背景颜色设置为白色</p>
  42. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const scene = new THREE.Scene();
  43. +scene.background = new THREE.Color("white");
  44. </pre>
  45. <p>然后我们将使用相同的棋盘格地面,因为这一次我们使用的是<a href="/docs/#api/zh/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>,所有我们不需要地面照明</p>
  46. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">+const loader = new THREE.TextureLoader();
  47. {
  48. const planeSize = 40;
  49. - const loader = new THREE.TextureLoader();
  50. const texture = loader.load('resources/images/checker.png');
  51. texture.wrapS = THREE.RepeatWrapping;
  52. texture.wrapT = THREE.RepeatWrapping;
  53. texture.magFilter = THREE.NearestFilter;
  54. const repeats = planeSize / 2;
  55. texture.repeat.set(repeats, repeats);
  56. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  57. const planeMat = new THREE.MeshBasicMaterial({
  58. map: texture,
  59. side: THREE.DoubleSide,
  60. });
  61. + planeMat.color.setRGB(1.5, 1.5, 1.5);
  62. const mesh = new THREE.Mesh(planeGeo, planeMat);
  63. mesh.rotation.x = Math.PI * -.5;
  64. scene.add(mesh);
  65. }
  66. </pre>
  67. <p>注意我们将颜色设置为<code class="notranslate" translate="no">1.5, 1.5, 1.5</code>,这将是棋盘纹理的颜色倍增 1.5,1.5,1.5。
  68. 也就是说纹理原本的颜色是 0x808080 和 0xC0C0C0,是灰色和浅灰色,现在灰色和浅灰色乘以 1.5 将得到白色和浅灰色的棋盘。</p>
  69. <p>现在让我们加载阴影贴图</p>
  70. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const shadowTexture = loader.load("resources/images/roundshadow.png");
  71. </pre>
  72. <p>并且创建一个数组来存放每个球体和它相关的对象</p>
  73. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereShadowBases = [];
  74. </pre>
  75. <p>现在我们创建一个球体</p>
  76. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const sphereRadius = 1;
  77. const sphereWidthDivisions = 32;
  78. const sphereHeightDivisions = 16;
  79. const sphereGeo = new THREE.SphereGeometry(
  80. sphereRadius,
  81. sphereWidthDivisions,
  82. sphereHeightDivisions
  83. );
  84. </pre>
  85. <p>然后创建一个假阴影的平面网格</p>
  86. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const planeSize = 1;
  87. const shadowGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  88. </pre>
  89. <p>现在我们将创建一堆球体,对于每个球体都将创建一个<code class="notranslate" translate="no">基础</code>的<a href="/docs/#api/zh/core/Object3D"><code class="notranslate" translate="no">THREE.Object3D</code></a>,并且我们将同时创建阴影平面网格和球体网格。这样,如果我们同时移动球体,阴影也一并移动,我们只需要将阴影稍微放置再地面上,防止 Z 轴阴影和地面重叠。
  90. 我们将<code class="notranslate" translate="no">depthWrite</code>属性设置为 false,这样使阴影之间不会彼此混淆。
  91. 我们将在<a href="transparency.html">另一篇文章</a>中讨论这两个问题。
  92. 因为阴影的材质是<a href="/docs/#api/zh/materials/MeshBasicMaterial"><code class="notranslate" translate="no">MeshBasicMaterial</code></a>,所以它并不需要照明</p>
  93. <p>我们将每个球体使用不同的色相,然后保存每个球体的基础、球体网格、阴影网格和初始 y 位置。</p>
  94. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const numSpheres = 15;
  95. for (let i = 0; i &lt; numSpheres; ++i) {
  96. // make a base for the shadow and the sphere
  97. // so they move together.
  98. const base = new THREE.Object3D();
  99. scene.add(base);
  100. // add the shadow to the base
  101. // note: we make a new material for each sphere
  102. // so we can set that sphere's material transparency
  103. // separately.
  104. const shadowMat = new THREE.MeshBasicMaterial({
  105. map: shadowTexture,
  106. transparent: true, // so we can see the ground
  107. depthWrite: false, // so we don't have to sort
  108. });
  109. const shadowMesh = new THREE.Mesh(shadowGeo, shadowMat);
  110. shadowMesh.position.y = 0.001; // so we're above the ground slightly
  111. shadowMesh.rotation.x = Math.PI * -0.5;
  112. const shadowSize = sphereRadius * 4;
  113. shadowMesh.scale.set(shadowSize, shadowSize, shadowSize);
  114. base.add(shadowMesh);
  115. // add the sphere to the base
  116. const u = i / numSpheres; // goes from 0 to 1 as we iterate the spheres.
  117. const sphereMat = new THREE.MeshPhongMaterial();
  118. sphereMat.color.setHSL(u, 1, 0.75);
  119. const sphereMesh = new THREE.Mesh(sphereGeo, sphereMat);
  120. sphereMesh.position.set(0, sphereRadius + 2, 0);
  121. base.add(sphereMesh);
  122. // remember all 3 plus the y position
  123. sphereShadowBases.push({
  124. base,
  125. sphereMesh,
  126. shadowMesh,
  127. y: sphereMesh.position.y,
  128. });
  129. }
  130. </pre>
  131. <p>我们设置两个光源,一个是<a href="/docs/#api/zh/lights/HemisphereLight"><code class="notranslate" translate="no">HemisphereLight</code></a>,将其光照强度设置为 2,让场景比较明亮。</p>
  132. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  133. const skyColor = 0xb1e1ff; // light blue
  134. const groundColor = 0xb97a20; // brownish orange
  135. const intensity = 2;
  136. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  137. scene.add(light);
  138. }
  139. </pre>
  140. <p>另一个是 <a href="/docs/#api/zh/lights/DirectionalLight"><code class="notranslate" translate="no">DirectionalLight</code></a> ,这将让球体看起来有些视觉的区别</p>
  141. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  142. const color = 0xffffff;
  143. const intensity = 1;
  144. const light = new THREE.DirectionalLight(color, intensity);
  145. light.position.set(0, 10, 5);
  146. light.target.position.set(-5, 0, 0);
  147. scene.add(light);
  148. scene.add(light.target);
  149. }
  150. </pre>
  151. <p>现在我们设置球体动画并将其渲染。
  152. 对于每个球体,阴影以及 base,让它们在 XZ 平面上移动。使用<a href="/docs/#api/zh/math/Math.abs(Math.sin(time))"><code class="notranslate" translate="no">Math.abs(Math.sin(time))</code></a>将球体上下移动,这样会带来一个类似弹性的动画。并且我们还设置了阴影材质的不透明度,与球体的高度相关。高度越高,阴影越模糊。</p>
  153. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function render(time) {
  154. time *= 0.001; // convert to seconds
  155. ...
  156. sphereShadowBases.forEach((sphereShadowBase, ndx) =&gt; {
  157. const {base, sphereMesh, shadowMesh, y} = sphereShadowBase;
  158. // u is a value that goes from 0 to 1 as we iterate the spheres
  159. const u = ndx / sphereShadowBases.length;
  160. // compute a position for the base. This will move
  161. // both the sphere and its shadow
  162. const speed = time * .2;
  163. const angle = speed + u * Math.PI * 2 * (ndx % 1 ? 1 : -1);
  164. const radius = Math.sin(speed - ndx) * 10;
  165. base.position.set(Math.cos(angle) * radius, 0, Math.sin(angle) * radius);
  166. // yOff is a value that goes from 0 to 1
  167. const yOff = Math.abs(Math.sin(time * 2 + ndx));
  168. // move the sphere up and down
  169. sphereMesh.position.y = y + THREE.MathUtils.lerp(-2, 2, yOff);
  170. // fade the shadow as the sphere goes up
  171. shadowMesh.material.opacity = THREE.MathUtils.lerp(1, .25, yOff);
  172. });
  173. ...
  174. </pre>
  175. <p>这里有 15 种弹跳球</p>
  176. <p></p><div translate="no" class="threejs_example_container notranslate">
  177. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-fake.html"></iframe></div>
  178. <a class="threejs_center" href="/manual/examples/shadows-fake.html" target="_blank">点击此处在新标签页中打开</a>
  179. </div>
  180. <p></p>
  181. <p>在某些应用程序中使用圆形或者椭圆的阴影也是很常见的。当然也可以使用不同形状的阴影纹理,也可以将阴影的边缘锐化。使用这种类型的阴影的例子是 <a href="https://www.google.com/search?tbm=isch&amp;q=animal+crossing+pocket+camp+screenshots">Animal Crossing Pocket Camp</a>,在其中你可以看到每个字符都有一个简单的原型阴影。这种方式很有效,也很方便。<a href="https://www.google.com/search?q=monument+valley+screenshots&amp;tbm=isch">Monument Valley 纪念碑谷</a>看起来似乎也使用这种阴影。</p>
  182. <p>因此,移动阴影贴图,有三种光可以投射阴影,分别为<code class="notranslate" translate="no">DirectionalLight 定向光</code>、 <code class="notranslate" translate="no">PointLight 点光源</code>、<code class="notranslate" translate="no">SpotLight 聚光灯</code>,</p>
  183. <p>让我们从 <code class="notranslate" translate="no">DirectionalLight 定向光</code> 开始。这里我们使用<a href="lights.html">关于灯光的文章</a>作为基础</p>
  184. <p>第一件事是设置渲染器中的阴影属性</p>
  185. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const renderer = new THREE.WebGLRenderer({ canvas });
  186. +renderer.shadowMap.enabled = true;
  187. </pre>
  188. <p>我们还需要设置光能投射阴影</p>
  189. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const light = new THREE.DirectionalLight(color, intensity);
  190. +light.castShadow = true;
  191. </pre>
  192. <p>在场景中的每个网格,我们都能设置它是否能投射阴影或被投射阴影。
  193. 这里我们只设置地面能被投射阴影,这样我们不需要关心地面投射阴影的问题。</p>
  194. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(planeGeo, planeMat);
  195. mesh.receiveShadow = true;
  196. </pre>
  197. <p>对于球体和立方体,我们需要设置他们都能投射阴影或者被投射阴影</p>
  198. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  199. mesh.castShadow = true;
  200. mesh.receiveShadow = true;
  201. ...
  202. const mesh = new THREE.Mesh(sphereGeo, sphereMat);
  203. mesh.castShadow = true;
  204. mesh.receiveShadow = true;
  205. </pre>
  206. <p>然后我们运行它</p>
  207. <p></p><div translate="no" class="threejs_example_container notranslate">
  208. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-directional-light.html"></iframe></div>
  209. <a class="threejs_center" href="/manual/examples/shadows-directional-light.html" target="_blank">点击此处在新标签页中打开</a>
  210. </div>
  211. <p></p>
  212. <p>发生了什么?为什么阴影的一部分不见了</p>
  213. <p>原因是阴影是通过光线的角度渲染场景之后生成的。在这种情况下,现在只有一个<code class="notranslate" translate="no">DirectionalLight 定向光</code>在照射这个球体,就像我们之前的文章<a href="cameras.html">关于相机</a>,光源的阴影相机决定了阴影投射的区域。在上面的例子中,该区域太小了。</p>
  214. <p>为了可视化该区域,我们可以通过<code class="notranslate" translate="no">CameraHelper 相机帮助类</code> 来获取光源的阴影相机。</p>
  215. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const cameraHelper = new THREE.CameraHelper(light.shadow.camera);
  216. scene.add(cameraHelper);
  217. </pre>
  218. <p>你现在可以看到光源的阴影相机可以投射的区域。</p>
  219. <p></p><div translate="no" class="threejs_example_container notranslate">
  220. <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>
  221. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-helper.html" target="_blank">点击此处在新标签页中打开</a>
  222. </div>
  223. <p></p>
  224. <p>我们来回调整相机的 x 坐标值。这样我们可以很清楚的看到:光源的阴影相机所包围的 box 才是能投射阴影的区域。</p>
  225. <p>我们可以通过调整光源的阴影相机来调整该盒子的大小。</p>
  226. <p>现在我们添加一些可以调整光源相关属性的 GUI 设置。
  227. 由于<code class="notranslate" translate="no">DirectionalLight 定向光</code>表现形式是光照一直都是平行方向移动的,所以我们在<code class="notranslate" translate="no">DirectionalLight 定向光</code>中使用<code class="notranslate" translate="no">OrthographicCamera 正交相机</code>为了观察阴影相机。
  228. 我们在 <a href="cameras.html">关于相机的文章</a>介绍了<code class="notranslate" translate="no">OrthographicCamera 正交相机</code>是如何工作的。</p>
  229. <p>回忆<code class="notranslate" translate="no">OrthographicCamera 正交相机</code>的定义和其视图的用法,以及其属性:<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></p>
  230. <p>我们再次为 lil-gui 创建一个<code class="notranslate" translate="no">DimensionGUIHelper</code>类。这个类的作用是响应式的通过一个属性来设置两个相关的属性。然后将其加入到 lil-gui 选项中。
  231. 我们根据<code class="notranslate" translate="no">width</code>的值设置 <code class="notranslate" translate="no">left</code>和<code class="notranslate" translate="no">right</code>,根据<code class="notranslate" translate="no">height</code>的值设置<code class="notranslate" translate="no">up</code> 和 <code class="notranslate" translate="no">down</code></p>
  232. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">class DimensionGUIHelper {
  233. constructor(obj, minProp, maxProp) {
  234. this.obj = obj;
  235. this.minProp = minProp;
  236. this.maxProp = maxProp;
  237. }
  238. get value() {
  239. return this.obj[this.maxProp] * 2;
  240. }
  241. set value(v) {
  242. this.obj[this.maxProp] = v / 2;
  243. this.obj[this.minProp] = v / -2;
  244. }
  245. }
  246. </pre>
  247. <p>我们也会使用在<a href="cameras.html">关于相机的文中</a>中创建的<code class="notranslate" translate="no">MinMaxGUIHelper</code>,他将负责<code class="notranslate" translate="no">near</code> and <code class="notranslate" translate="no">far</code>的变化</p>
  248. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">const gui = new GUI();
  249. gui.addColor(new ColorGUIHelper(light, 'color'), 'value').name('color');
  250. gui.add(light, 'intensity', 0, 2, 0.01);
  251. +{
  252. + const folder = gui.addFolder('Shadow Camera');
  253. + folder.open();
  254. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'left', 'right'), 'value', 1, 100)
  255. + .name('width')
  256. + .onChange(updateCamera);
  257. + folder.add(new DimensionGUIHelper(light.shadow.camera, 'bottom', 'top'), 'value', 1, 100)
  258. + .name('height')
  259. + .onChange(updateCamera);
  260. + const minMaxGUIHelper = new MinMaxGUIHelper(light.shadow.camera, 'near', 'far', 0.1);
  261. + folder.add(minMaxGUIHelper, 'min', 0.1, 50, 0.1).name('near').onChange(updateCamera);
  262. + folder.add(minMaxGUIHelper, 'max', 0.1, 50, 0.1).name('far').onChange(updateCamera);
  263. + folder.add(light.shadow.camera, 'zoom', 0.01, 1.5, 0.01).onChange(updateCamera);
  264. +}
  265. </pre>
  266. <p>我们需要让 GUI 在我们改变数据的时候调用<code class="notranslate" translate="no">updateCamera</code>方法
  267. 这个方法将更新光源,光源的帮助类以及光源的阴影相机和像是光的阴影相机的帮助类。</p>
  268. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">function updateCamera() {
  269. // update the light target's matrixWorld because it's needed by the helper
  270. light.target.updateMatrixWorld();
  271. helper.update();
  272. // update the light's shadow camera's projection matrix
  273. light.shadow.camera.updateProjectionMatrix();
  274. // and now update the camera helper we're using to show the light's shadow camera
  275. cameraHelper.update();
  276. }
  277. updateCamera();
  278. </pre>
  279. <p>现在我们给了光的阴影相机一个 GUI,我们可以尝试随意更改其中的值。</p>
  280. <p></p><div translate="no" class="threejs_example_container notranslate">
  281. <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>
  282. <a class="threejs_center" href="/manual/examples/shadows-directional-light-with-camera-gui.html" target="_blank">点击此处在新标签页中打开</a>
  283. </div>
  284. <p></p>
  285. <p>将<code class="notranslate" translate="no">width</code> 和 <code class="notranslate" translate="no">height</code>的值设置在 30 附近,我们将看到阴影是正常渲染的,因为这个区域都在阴影相机的投影范围内。</p>
  286. <p>这也带来一个疑问。为什么我们不将<code class="notranslate" translate="no">width</code> 和 <code class="notranslate" translate="no">height</code>设置成一个非常大的值,这样不就可以投影一切了?我们将它设置成 100 来看看会发生什么。</p>
  287. <div class="threejs_center"><img src="../resources/images/low-res-shadow-map.png" style="width: 369px"></div>
  288. <p>我们将看到一些块状的阴影!</p>
  289. <p>这个问题是另一个需要注意的阴影相关设置。被投射产生的阴影也是有纹理的,这些阴影的纹理也是有单位大小的。如果阴影相机的属性设置的越大,就意味着它能投射的区域也变得很大,就意味着投射的阴影会越来越块状。</p>
  290. <p>你可以设置<code class="notranslate" translate="no">light.shadow.mapSize.width</code>和 <code class="notranslate" translate="no">light.shadow.mapSize.height</code>来设置阴影的纹理分辨率。他们默认为 512X512。如果设置的很大,他们在计算时将占用更多的内存,并且变得很慢。为了获得更真实的阴影,应该尽量将值设置的最小。请注意在渲染器中该属性<a href="/docs/#api/zh/renderers/WebGLRenderer#capabilities"><code class="notranslate" translate="no">renderer.capabilities.maxTextureSize</code></a>对于每个用户都有最大纹理的上限值。</p>
  291. <p>当我们切换到 <code class="notranslate" translate="no">SpotLight 聚光灯</code>时,光源的阴影相机就变成了<code class="notranslate" translate="no">PerspectiveCamera透视相机</code> ,与<code class="notranslate" translate="no">DirectionalLight 定向光</code>的阴影相机不同,<code class="notranslate" translate="no">SpotLight 聚光灯</code>的阴影相机有其本身所控制,我们也可以手动设置大部分设置。<code class="notranslate" translate="no">SpotLight聚光灯</code>阴影相机的的<code class="notranslate" translate="no">fov</code>跟<code class="notranslate" translate="no">SpotLight聚光灯</code>的<code class="notranslate" translate="no">angle</code> 关联。<code class="notranslate" translate="no">aspect</code>属性是根据阴影映射自动设置大小的。</p>
  292. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.DirectionalLight(color, intensity);
  293. +const light = new THREE.SpotLight(color, intensity);
  294. </pre>
  295. <p>我们在<a href="lights.html">关于灯光的文章</a>中添加了<code class="notranslate" translate="no">penumbra</code> 和 <code class="notranslate" translate="no">angle</code>的相关介绍</p>
  296. <p></p><div translate="no" class="threejs_example_container notranslate">
  297. <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>
  298. <a class="threejs_center" href="/manual/examples/shadows-spot-light-with-camera-gui.html" target="_blank">点击此处在新标签页中打开</a>
  299. </div>
  300. <p></p>
  301. <p>最后,我们介绍 <code class="notranslate" translate="no">PointLight 聚光灯</code>的阴影投射。
  302. <code class="notranslate" translate="no">PointLight 聚光灯</code>是向四面八方发散的,所以唯一的设置只有<code class="notranslate" translate="no">near</code> 和 <code class="notranslate" translate="no">far</code>。实际上<code class="notranslate" translate="no">PointLight 聚光灯</code> 相当于 6 个面的<code class="notranslate" translate="no">SpotLight 点光源</code>组合而成。这意味着它的渲染速度要慢得多,相当于整个场景的阴影和渲染 6 次,每个方向(面)都需要渲染一次。</p>
  303. <p>我们将在场景中放置一个盒子,这样我们可以看到墙壁和天花板的阴影效果。我们设置材质的属性,只让它在盒子的内部渲染,就像地板一样。我们还设置它可以被投射阴影,并且将它的高度设置的比地板稍微低一点,防止 Z 轴渲染重合。</p>
  304. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">{
  305. const cubeSize = 30;
  306. const cubeGeo = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
  307. const cubeMat = new THREE.MeshPhongMaterial({
  308. color: "#CCC",
  309. side: THREE.BackSide,
  310. });
  311. const mesh = new THREE.Mesh(cubeGeo, cubeMat);
  312. mesh.receiveShadow = true;
  313. mesh.position.set(0, cubeSize / 2 - 0.1, 0);
  314. scene.add(mesh);
  315. }
  316. </pre>
  317. <p>当然我们也需要把光源切换成<code class="notranslate" translate="no">PointLight 聚光灯</code></p>
  318. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">-const light = new THREE.SpotLight(color, intensity);
  319. +const light = new THREE.PointLight(color, intensity);
  320. ....
  321. // so we can easily see where the point light is
  322. +const helper = new THREE.PointLightHelper(light);
  323. +scene.add(helper);
  324. </pre>
  325. <p></p><div translate="no" class="threejs_example_container notranslate">
  326. <div><iframe class="threejs_example notranslate" translate="no" style=" " src="/manual/examples/resources/editor.html?url=/manual/examples/shadows-point-light.html"></iframe></div>
  327. <a class="threejs_center" href="/manual/examples/shadows-point-light.html" target="_blank">点击此处在新标签页中打开</a>
  328. </div>
  329. <p></p>
  330. <p>使用 GUI 的<code class="notranslate" translate="no">position</code>来移动光源的位置,你就可以看到墙上阴影强度的改变。你还可以调整其他的设置,比如<code class="notranslate" translate="no">near</code> 和 <code class="notranslate" translate="no">far</code> ,<code class="notranslate" translate="no">near</code>代表最小的渲染阴影的距离,这只会渲染物体的距离大于其值的物体的阴影。 <code class="notranslate" translate="no">far</code> 这代表渲染比其值距离小的物体的阴影。</p>
  331. </div>
  332. </div>
  333. </div>
  334. <script src="../resources/prettify.js"></script>
  335. <script src="../resources/lesson.js"></script>
  336. </body></html>