環境光源Ambient lighting

環境光線為場景提供定值光線。Ambient lighting provides constant lighting for a scene. 它同等地照亮所有物件端點,因為其不取決於任何其他照明因素,例如頂點標準、光源方向、光源位置、範圍或衰減。It lights all object vertices the same because it is not dependent on any other lighting factors such as vertex normals, light direction, light position, range, or attenuation. 環境光線於所有方向皆為定值,且為物件的所有像素以相同方式上色。Ambient lighting is constant in all directions and it colors all pixels of an object the same. 加以計算很快速,但是會讓物件看起來很平板且不真實。It is fast to calculate but leaves objects looking flat and unrealistic.

環境光線是最快速的照明類型,但產生的結果最不逼真。Ambient lighting is the fastest type of lighting but it produces the least realistic results. Direct3D 包含單一全域環境光線屬性,您可加以使用而不需建立任何光線。Direct3D contains a single global ambient light property that you can use without creating any light. 或者,您可以將任何光線物件設為提供環境光線。Alternatively, you can set any light object to provide ambient lighting.

場景的環境光線由下列方程式描述。The ambient lighting for a scene is described by the following equation.

環境光源 = C ₐ * [ g ₐ + Sum (Atten * 找i出 * ai) ]Ambient Lighting = Cₐ*[Gₐ + sum(Atteni*Spoti*Lai)]

其中:Where:

參數Parameter 預設值Default value 類型Type 說明Description
CₐCₐ (0,0,0,0)(0,0,0,0) D3DCOLORVALUED3DCOLORVALUE 材料環境色彩Material ambient color
GₐGₐ (0,0,0,0)(0,0,0,0) D3DCOLORVALUED3DCOLORVALUE 全域環境色彩Global ambient color
AtteniAtteni (0,0,0,0)(0,0,0,0) D3DCOLORVALUED3DCOLORVALUE 第 i 個光線的光衰減。Light attenuation of the ith light. 參閱衰減和聚光燈係數See Attenuation and spotlight factor.
Spoti (0,0,0,0)(0,0,0,0) D3DVECTORD3DVECTOR 第 i 個光線的聚光燈係數。Spotlight factor of the ith light. 參閱衰減和聚光燈係數See Attenuation and spotlight factor.
Sumsum N/AN/A N/AN/A 環境光線的加總Sum of the ambient light
LaiLai (0,0,0,0)(0,0,0,0) D3DVECTORD3DVECTOR 第 i 個光線的環境光線色彩Light ambient color of the ith light

 

Cₐ 的值為︰The value for Cₐ is either:

  • 頂點 color1 (如果 AMBIENTMATERIALSOURCE = D3DMCS _ color1)和第一個頂點色彩是在頂點宣告中提供。vertex color1, if AMBIENTMATERIALSOURCE = D3DMCS_COLOR1, and the first vertex color is supplied in the vertex declaration.
  • 頂點 color2 (如果 AMBIENTMATERIALSOURCE = D3DMCS _ color2),而第二個頂點色彩則是在頂點宣告中提供。vertex color2, if AMBIENTMATERIALSOURCE = D3DMCS_COLOR2, and the second vertex color is supplied in vertex declaration.
  • 材料環境色彩。material ambient color.

注意   如果使用任一個 AMBIENTMATERIALSOURCE 選項,而且未提供頂點色彩,則會使用材質環境色彩。Note   If either AMBIENTMATERIALSOURCE option is used, and the vertex color is not provided, then the material ambient color is used.

 

若要使用材料環境色彩,請使用下方範例程式碼所示的 SetMaterial。To use the material ambient color, use SetMaterial as shown in the example code below.

Gₐ 是全域環境色彩。Gₐ is the global ambient color. 它是使用 Graphicsdevice (D3DRS _ 環境) 來設定。It is set using SetRenderState(D3DRS_AMBIENT). Direct3D 場景中有一個全域環境色彩。There is one global ambient color in a Direct3D scene. 此參數與 Direct3D 光線物件無關聯。This parameter is not associated with a Direct3D light object.

Lai 是場景中第 i 個光線的環境色彩。Lai is the ambient color of the ith light in the scene. 每個 Direct3D 光線都有一組屬性,其中一項是環境色彩。Each Direct3D light has a set of properties, one of which is the ambient color. sum(Lai) 這項字詞是場景中所有環境色彩的加總。The term, sum(Lai) is a sum of all the ambient colors in the scene.

例子Example

在此範例中,物件使用場景環境光線和材料環境色彩上色。In this example, the object is colored using the scene ambient light and a material ambient color.

#define GRAY_COLOR  0x00bfbfbf

Ambient.r = 0.75f;
Ambient.g = 0.0f;
Ambient.b = 0.0f;
Ambient.a = 0.0f;

根據方程式,為物件端點產生的色彩是材質色彩和淺色的組合。According to the equation, the resulting color for the object vertices is a combination of the material color and the light color.

下方兩個圖例顯示材質色彩 (灰色),以及淺色 (鮮紅)。The following two illustrations show the material color, which is gray, and the light color, which is bright red.

灰球體圖例紅色球體圖例

下圖顯示產生的場景。The resulting scene is shown in the following illustration. 場景中唯一的物件為球體。The only object in the scene is a sphere. 環境光線使用相同色彩照亮所有物件端點。Ambient light lights all object vertices with the same color. 其不取決於頂點標準或光線方向。It is not dependent on the vertex normal or the light direction. 如此一來,球體看起來像是 2D 圓形,因為物件表面周圍的陰影並無差別。As a result, the sphere looks like a 2D circle because there is no difference in shading around the surface of the object.

使用環境光線的球體圖例

若要讓物件看起來更逼真,請在環境光線之外套用光源擴散或反射光源。To give objects a more realistic look, apply diffuse or specular lighting in addition to ambient lighting.

相關主題Related topics

光源的數學計算Mathematics of lighting