光类型Light types

光类型属性定义所用光源的类型。The light type property defines which type of light source you're using. Direct3D 中有三种类型的光 - 点光、聚光和定向光。There are three types of lights in Direct3D - point lights, spotlights, and directional lights. 每种类型以不同的方式照亮场景中的对象,它们具有不同的计算开销水平。Each type illuminates objects in a scene differently, with varying levels of computational overhead.

点光Point Light

点光在场景中具有颜色和位置,但没有单一方向。Point lights have color and position within a scene, but no single direction. 它们向所有方向均匀发光,如下图所示。They give off light equally in all directions, as shown in the following illustration.

点光示意图

灯泡是点光的良好示例。A light bulb is a good example of a point light. 点光受衰减和范围的影响,以顶点为基础照亮网格。Point lights are affected by attenuation and range, and illuminate a mesh on a vertex-by-vertex basis. 在照明期间,Direct3D 使用点光在世界空间中的位置和被点亮的顶点的坐标来生成光的方向矢量和光行进的距离。During lighting, Direct3D uses the point light's position in world space and the coordinates of the vertex being lit to derive a vector for the direction of the light, and the distance that the light has traveled. 两者与顶点法线一起用于计算光对表面照明的贡献。Both are used, along with the vertex normal, to calculate the contribution of the light to the illumination of the surface.

定向光Directional Light

定向光只有颜色和方向,没有位置。Directional lights have only color and direction, not position. 它们发射平行光。They emit parallel light. 这意味着由定向光产生的所有光线在相同方向上穿过场景。This means that all light generated by directional lights travels through a scene in the same direction. 你可以将定向光想象成位于近乎无限远处的光源,如太阳。Imagine a directional light as a light source at near infinite distance, such as the sun. 定向光不受衰减或范围的影响,因此,你指定的方向和颜色是 Direct3D 计算顶点颜色时考虑的唯一因素。Directional lights are not affected by attenuation or range, so the direction and color you specify are the only factors considered when Direct3D calculates vertex colors. 由于照明因素较少,它们是可以使用的计算密集程度最低的光。Because of the small number of illumination factors, these are the least computationally intensive lights to use.

聚焦SpotLight

聚光具有颜色、位置和发光方向。Spotlights have color, position, and direction in which they emit light. 从聚光发射的光由明亮的内锥和较大的外锥组成,光强度在两者之间减弱,如下图所示。Light emitted from a spotlight is made up of a bright inner cone and a larger outer cone, with the light intensity diminishing between the two, as shown in the following illustration.

带有内锥和外锥的聚焦的示意图

聚光受减退、衰减和范围的影响。Spotlights are affected by falloff, attenuation, and range. 在计算场景中对象的照明效果时,需要考虑这些因素以及光到每个顶点的射程。These factors, as well as the distance light travels to each vertex, are figured in when computing lighting effects for objects in a scene. 由于需要为每个顶点计算这些效果,聚光在 Direct3D 中是计算耗时最长的光类型。Computing these effects for each vertex makes spotlights the most computationally time-consuming of all lights in Direct3D.

只有聚光使用 Falloff、Theta 和 Phi 值。Falloff, Theta, and Phi values are used only by spotlights. 这些值控制聚光对象内锥和外锥的大小,以及它们之间光的减弱程度。These values control how large or small a spotlight object's inner and outer cones are, and how light decreases between them.

Theta 是聚光内锥的弧度角,Phi 值是聚光外锥的角度。Theta is the radian angle of the spotlight's inner cone, and the Phi value is the angle for the outer cone of light. Falloff 控制光强度在内锥的外边缘和外锥的内边缘之间的减弱程度。Falloff controls how light intensity decreases between the outer edge of the inner cone and the inner edge of the outer cone. 大多数应用程序将 Falloff 设为 1.0,以在两个锥体之间营造均匀的衰减效果,但你可以根据需要设置其他的值。Most applications set Falloff to 1.0 to create falloff that occurs evenly between the two cones, but you can set other values as needed.

下图显示了这些值之间的关系,以及它们如何影响聚光的内锥和外锥。The following illustration shows the relationship between these values and how they can affect a spotlight's inner and outer cones of light.

说明 Phi 和 Theta 值与聚光锥体的关系的示意图

聚光发出的光锥包括两个部分:一个明亮的内锥和一个外锥。Spotlights emit a cone of light that has two parts: a bright inner cone and an outer cone. 光在内锥中最亮,在外锥之外不存在,光强度在这两个区域之间逐渐衰减。Light is brightest in the inner cone and isn't present outside the outer cone, with light intensity attenuating between the two areas. 这种类型的衰减通常被称为减退。This type of attenuation is commonly referred to as falloff.

顶点接收的光量取决于其在内锥或外锥中的位置。The amount of light a vertex receives is based on the vertex's location in the inner or outer cones. Direct3D 计算聚光方向矢量 (L) 和从光到顶点的矢量 (D) 的点积。Direct3D computes the dot product of the spotlight's direction vector (L) and the vector from the light to the vertex (D). 该值等于两个矢量之间角度的余弦,并用作顶点位置的指示器。它可以与光的锥角进行比较,以确定顶点可能位于内锥或外锥中的何处。This value is equal to the cosine of the angle between the two vectors, and serves as an indicator of the vertex's position that can be compared to the light's cone angles to determine where the vertex might lie in the inner or outer cones. 下图提供了这两个矢量之间关联的图形化表示。The following illustration provides a graphical representation of the association between these two vectors.

聚光方向矢量和从顶点到聚光的矢量的示意图

系统将此值与聚光的内锥角和外锥角的余弦值进行比较。The system compares this value to the cosine of the spotlight's inner and outer cone angles. 光的 Theta 和 Phi 值表示内锥和外锥的总锥角。The light's Theta and Phi values represent the total cone angles for the inner and outer cones. 当顶点远离照明中心(而不是在总锥角上)时会发生衰减现象,因此,运行时会先将这些锥角分成两半,然后再计算它们的余弦值。Because the attenuation occurs as the vertex becomes more distant from the center of illumination (rather than across the total cone angle), the runtime divides these cone angles in half before calculating their cosines.

如果矢量 L 和 D 的点积小于或等于外锥角的余弦值,则顶点位于外锥之外且不接收光线。If the dot product of vectors L and D is less than or equal to the cosine of the outer cone angle, the vertex lies beyond the outer cone and receives no light. 如果 L 和 D 的点积大于内锥角的余弦值,则顶点位于内锥中且接收最大的光量,此时需要考虑距离变化导致的衰减。If the dot product of L and D is greater than the cosine of the inner cone angle, then the vertex is within the inner cone and receives the maximum amount of light, still considering attenuation over distance. 如果顶点位于两个区域之间的某处,则使用下面的公式计算减退。If the vertex is somewhere between the two regions, then falloff is calculated with the following equation.

发生减退后顶点处光强度的计算公式

其中:where:

  • If 是减退后的光强度If is light intensity after falloff
  • Alpha 是矢量 L 和 D 之间的角度Alpha is the angle between vectors L and D
  • Theta 是内锥角Theta is the inner cone angle
  • Phi 是外锥角Phi is the outer cone angle
  • p 是减退p is the falloff

此公式生成一个介于 0.0 和 1.0 之间的值,用于缩放光在顶点处的强度,从而营造减退效果。This formula generates a value between 0.0 and 1.0 that scales the light's intensity at the vertex to account for falloff. 此外,还需要根据顶点与光的距离应用衰减。Attenuation as a factor of the vertex's distance from the light is also applied. 下图显示了不同的减退值对减退曲线的影响。The following graph shows how different falloff values can affect the falloff curve.

光强度与从顶点到光的距离的示意图

各种减退值对实际照明的影响很微小,将不为 1.0 的减退值生成减退曲线会产生较小的性能损失。The effect of various falloff values on the actual lighting is subtle, and a small performance penalty is incurred by shaping the falloff curve with falloff values other than 1.0. 出于上述原因,此值通常设为 1.0。For these reasons, this value is typically set to 1.0.

相关主题Related topics

灯和材料Lights and materials