阴影映射(Shadow Mapping)

Shadow Mapping

阴影映射

Introduction

介绍

Shadow-mapping is a common technique for providing real time shadows to a 3D scene. It works by rendering a depth map texture from the light source's point of view. Then, when shading a pixel of a 3D model its depth value is compared to the depth map and is shaded darker if it is in shadow or lighter if it is lit by the light.

阴影映射是一种为3D场景提供实时阴影的常用技术。它通过从光源的角度渲染深度贴图纹理来工作。然后,当对3D模型的像素进行着色时,将其深度值与深度图进行比较,如果它在阴影中,则着色较深,如果它被灯光照亮,则着色得较浅。

Shadows add to the perceptual realism of a scene and makes it easier to judge the relative positions of objects.

阴影增加了场景的感知真实感,使判断物体的相对位置变得更加容易。

Notice how in the left scene in the image it is impossible to tell how far from the plane the objects are but in the right scene the shadows makes this easy.

请注意,在图像的左侧场景中,无法分辨物体离平面有多远,但在右侧场景中,阴影使这变得容易。

Qt provides support for shadow mapping for all our three light types, DirectionalLightPointLight and SpotLight. To activate shadows in your scene you need to first set the light to cast shadows by setting castsShadow to true. You can then control which Model's will cast and receive shadows by setting the castsShadows and receivesShadows to true or false.

​Qt为我们的所有三种灯光类型(Directional light、PointLight和SpotLight)提供阴影映射支持。要激活场景中的阴影,您需要首先通过将castsShadow设置为true来设置灯光以投射阴影。然后,可以通过将castsShadow和receivesShadow设置为true或false来控制哪些模型将投射和接收阴影。

Directional Lights

平行光源

The directional light emits light in one direction from an unidentifiable source located infinitely far away. This is similar to the way sunlight works in real life. A directional light has infinite range and does not diminish.

定向光从无限远的不可识别的光源沿一个方向发射光。这类似于阳光在现实生活中的工作方式。平行光具有无限的范围,不会减弱。

Cascading Shadow Maps

级联阴影映射

One problem with a DirectionalLight is that it renders the whole scene from the point of view of the light. This can lead to blocky looking shadows when the size of the shadowmap is not adequate. One option to get better rendering quality is to use Cascading Shadow Maps (CSM). Qt supports a version of CSM called Parallell Split Shadow Maps (PSSM). PSSM works by splitting the view frustum into several parts and rendering a shadowmap for each part.

​DirectionalLight的一个问题是,它从灯光的角度渲染整个场景。当阴影映射的大小不足时,这可能会导致块状阴影。获得更好渲染质量的一种选择是使用级联阴影映射(CSM)。Qt支持一个名为Parallell Split Shadow Maps(PSSM)的CSM版本。PSSM的工作原理是将视锥分割成几个部分,并为每个部分渲染阴影映射。

This picture shows an abstract image of a view frustum with PSSM splits. It has two splits ending up in three cascades.

此图显示了具有PSSM分割的视锥的抽象图像。它有两个分叉,最终形成三个瀑布。

This way you can get better shadowmap resolution closer to the camera where the visual quality is more noticeable and lower resolution further from the camera where the visual quality is less noticeable.

通过这种方式,可以在视觉质量更明显的相机附近获得更好的阴影映射分辨率,在视觉质量不太明显的相机远处获得更低的分辨率。

The above picture shows a shadowmap without any splits (left) and a shadowmap with 3 splits (right).

上图显示了一个没有任何分割的阴影贴图(左)和一个有3个分割的阴影映射(右)。

You can control the number of cascade splits by the csmNumSplits property and where the splits are by the csmSplit1csmSplit2 and csmSplit3 properties. To get nice looking transitions between the splits you can specify a certain amount of blending between them with the csmBlendRatio property.

​可以通过csmNumSplits属性控制级联拆分的数量,并通过csmSplit1、csmSplit2和csmSplit3属性控制拆分的位置。为了在分割之间获得美观的过渡,可以使用csmBlendRatio属性在它们之间指定一定量的混合。

The above picture shows a zoomed in view of a cascade seam with no blending active.

上图显示了未激活混合的级联接缝的放大视图。

Keep in mind that for every split you add the application has to render another shadowmap which will affect performance negatively. The size of the blend area will affect performance so keep it as small as possible.

请记住,对于添加的每个分割,应用程序都必须渲染另一个阴影映射,这将对性能产生负面影响。混合区域的大小会影响性能,因此请尽量减小混合区域。

Point Light

点光源

The PointLight can be described as a sphere, emitting light with equal strength in all directions from the center of the light up to a given radius. This is similar to the way a light bulb emits light.

​PointLight可以被描述为一个球体,从光的中心到给定的半径,在所有方向上以相等的强度发射光。这类似于灯泡发光的方式。

The PointLight renders its shadowmap into a cubemap which means that is does six render passes. This can be quite computationally expensive.

​PointLight将其阴影映射渲染为立方体贴图,这意味着它进行了六次渲染过程。这在计算上可能非常昂贵。

Spot Light

聚光灯

The SpotLight emits light towards one direction in a cone shape, which is defined by the coneAngle property. The light intensity diminishes when approaching the coneAngle. The angle at which the light intensity starts to diminish is defined by innerConeAngle. This is similar to how a flashlight or a spot-light emits light.

​聚光灯以锥形向一个方向发光,锥形由coneAngle属性定义。当接近圆锥角时,光强度会减弱。光强开始减弱的角度由innerConeAngle定义。这类似于手电筒或聚光灯发光的方式。

Unlike the PointLight, the SpotLight renders its shadowmap into a single depth map.

​与PointLight不同,SpotLight将其阴影贴图渲染为单个深度贴图。

Performance and scene tweaking

性能和场景调整

While Qt tries to provide sensible default values for the properties related to shadowmapping there is usually some need to tweak them to fit the specific scene. Especially if you have a scene that is much smaller or bigger than what is expected. The following section will go into more detail of how you can tweak the values to make the shadow map look good while still maintaining as good performance as possible.

虽然Qt试图为与阴影贴图相关的属性提供合理的默认值,但通常需要对其进行调整以适应特定场景。特别是如果你有一个比预期小得多或大得多的场景。下一节将更详细地介绍如何调整值,使阴影贴图看起来很好,同时尽可能保持良好的性能。

Shadow bias

阴影偏移

Shadow bias is a way to remove so-called shadow-acne which are false shadows that typically appear in certain patterns. Shadow bias offsets the shadow map depth texture in a way that shadows appear further from the shadowing object and this often fixes the shadow-acne. The draw back is that if you have too much shadow bias then you can get an effect called peter panning where the shadow is too far away from the shadowing object. It is also possible to reduce shadow acne by increasing the shadowmap resolution.

阴影偏移是一种去除所谓的阴影痤疮的方法,阴影痤疮是通常以特定模式出现的假阴影。阴影偏移会偏移阴影贴图深度纹理,使阴影看起来离阴影对象更远,这通常会修复阴影痤疮。缺点是,如果有太多的阴影偏差,那么可以在阴影离阴影对象太远的地方获得一种称为彼得平移的效果。还可以通过提高阴影贴图分辨率来减少阴影痤疮。

The above image shows a scene with 0 shadow bias (left) vs 10 shadow bias (right). The left scene has some false shadows on the top of the cones and cylinders.

上图显示了一个阴影偏移为0(左)和10(右)的场景。左侧场景在圆锥体和圆柱体的顶部有一些假阴影。

Shadowmap resolution

阴影图分辨率

The resolution/quality of the shadowmap is controlled by the shadowMapQuality property. A higher shadowmap quality decreases the blockiness of the shadows but is more expensive to render so set it as low as possible while still maintaining the needed visual quality.

​阴影贴图的分辨率/质量由shadowMapQuality属性控制。更高的阴影贴图质量会降低阴影的块状,但渲染成本更高,因此在保持所需视觉质量的同时将其设置得尽可能低。

The above image shows a scene with a low shadowmap resolution vs a high shadowmap resolution.

上图显示了一个低阴影贴图分辨率与高阴影贴图分辨率的场景。

Soft shadows quality

柔和阴影质量

Soft shadows are a way to approximate the way shadows look in real life where they fade from being harder to softer at the edges. The soft shadow quality is controlled by the softShadowQuality property. It supports hard shadows with no softness as well as percentage-close filtering (PCF) soft shadows of varying quality. The hard shadows are the cheapest to render and PCF gets more and more expensive the higher quality it is. To control the radius of the soft shadow use the pcfFactor property. The value of pcfFactor does not impact the rendering speed but the higher it is the higher the soft shadow quality needs to be to look good.

​柔和阴影是一种近似现实生活中阴影外观的方法,在现实生活中,阴影的边缘会从较硬变软。软阴影质量由softShadowQuality属性控制。它支持没有柔和度的硬阴影以及不同质量的百分比接近滤波(PCF)软阴影。硬阴影的渲染成本最低,PCF的质量越高,渲染成本就越高。要控制软阴影的半径,请使用pcfFactor属性。pcfFactor的值不会影响渲染速度,但它越高,软阴影质量就越高,看起来越好。

The above image shows a scene with a softShadowQuality Light.Hard shadow on the left and a scene with a softShadowQuality Light.PCF32 and pcfRadius 10 on the right.

​上图左侧显示了一个具有柔和ShadowQuality Light.Hard的阴影场景。右侧显示了一个具有柔和ShadowQuality Light.PCF32和pcfRadius为10的阴影场景。

Shadowmap far distance

阴影图远距离

The property shadowMapFar can be used to control the maximum distance of the shadow map. This property works slightly different for PointLight and SpotLight vs DirectionalLight.

​属性shadowMapFar可用于控制阴影贴图的最大距离。此属性对于PointLight和SpotLight与Directional Light的工作方式略有不同。

For PointLight/SpotLight it determines how big the bounding box of the rendered shadow should be, but for the DirectionalLight it defines how far from the scene's camera the shadow map should cover in the scene.

​对于PointLight/SpotLight,它确定渲染阴影的边界框应该有多大,但对于DirectionalLight,它定义了阴影贴图在场景中应覆盖距离场景相机多远的距离。

The above image shows the same scene with the same directional light but two different values for shadowMapFar.

​上图显示了具有相同平行光但shadowMapFar有两个不同值的同一场景。

See also Using Image-Based Lighting and Lightmaps and Global Illumination.

Lightmaps and Global IlluminationInstancer Tool

​另请参见使用基于图像的照明和光照贴图以及全局照明。
光照贴图和全局照明实例化器工具

© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值