Light 灯光

优质
小牛编辑
126浏览
2023-12-01

Lights will bring personality and flavor to your game. You use lights to illuminate the scenes and objects to create the perfect visual mood. Lights can be used to simulate the sun, burning match light, flashlights, gun-fire, or explosions, just to name a few.

灯光将给你游戏带来个性和味道。你用灯光来照亮场景和对象 以至创造了完美视觉的气氛。灯光可以用来模拟太阳,燃烧的火柴,手电筒,枪火光,或爆炸,等等等等,在此就不一一列举。

Light 灯光
The Light Inspector 灯光检视面板

There are three types of lights in Unity 在Unity中有三种类型的灯:

  • Point lights shine from a location equally in all directions, like a light bulb.
    点光源从一个位置向四面八方发出光线,就像一盏灯。
  • Directional lights are placed infinitely far away and affect everything in the scene, like the sun.
    方向光源(平行光) 被放置在无穷远的地方,影响场景的所有物体,就像太阳
  • Spot lights shine from a point in a direction and only illuminate objects within a cone - like the headlights of a car.
    聚光灯 灯光从一点发出,只在一个方向按照一个锥形物体的范围照射,就像一辆汽车的车头灯。

Lights can also cast Shadows. Shadows are a Pro-only feature. Shadow properties can be adjusted on a per-light basis.

灯光也可以投射阴影。阴影是专业版的功能。阴影属性可以在每一个灯光的基础上进行调整。

Properties 属性

  • Type 类型 The current type of light object: 灯光对象的当前类型
  •     Directional 方向光 A light placed infinitely far away. It affects everything in the scene.
    灯光放在无穷远处。它影响着在场景里所有物体。
  •     Point 点光 A light that shines equally in all directions from its location, affecting all objects within its Range.
    灯光从它的位置各个方向发出光线,影响其范围内的所有对象。
  •     Spot 聚光 A light that shines everywhere within a cone defined by Spot Angle and Range. Only objects within this region are affected by the light.
    光线在 按照聚光灯的角度和范围所定义的一个圆锥区域 照射所有物体。只有在这个区域内的对象才会受到光线照射。
  • Range 范围 How far light is emitted from the center of the object. Point/Spot light only.
    光从物体的中心发射能到达多远?。只对点光源和聚光灯
  • Spot Angle 聚光灯角度 Determines the angle of the cone in degrees. Spot light only.
    灯光的聚光角度。只用于聚光灯
  • Color 颜色 The color of the light emitted. 光线的颜色.
  • Intensity 强度 Brightness of the light. Default value for a Point/Spot light is 1. Default value for a Directional light is 0.5
    光线的明亮程度,点光源/聚光灯 默认值是1。方向光源 默认值是0.5
  • Cookie The alpha channel of this texture is used as a mask that determines how bright the light is at different places. If the light is a Spot or a Directional light, this must be a 2D texture. If the light is a Point light, it must be a Cubemap.
    这个纹理的阿尔法(alpha)通道作为一个掩码,使光线在不同的地方有不同的亮度。如果灯光是聚光灯或方向光,这必须是一个2D纹理。如果灯光是一个点光源,它必须是一个立方图(Cubemap)。
  • Cookie Size Scales the projection of a Cookie. Directional light only.
    缩放Cookie投影。只用于方向光。
  • Shadow Type 阴影类型
    (Pro only) No, Hard or Soft shadows that will be cast by this light. Only applicable to desktop build targets. Soft shadows are more expensive.
    硬或软的阴影,将是由灯光所投射的。只适用于编译桌面目标程序(PC)。软阴影更为昂贵。(耗资源)
  •     Strength 硬度 The darkness of the shadows. Values are between 0 and 1. 阴影的黑暗程度。取值范围是在0和1之间。
  •     Resolution 分辨率 Detail level of the shadows. 阴影的细节水平。
  •     Bias 偏移 Offset used when comparing the pixel position in light space with the value from the shadow map. When the value is too small the surfaces start to self-shadow themselves ("shadow acne"), when too big - casters are disconnected from receivers ("peter panning").
    用于比较灯光空间的像素位置与阴影贴图的值比较的偏移量。当值太小,表面会产生self-shadow("影子痤疮"),-----就是物体的表面会有来自于自身的阴影,物体就像长了痘痘一样。当值太大,光源就会脱离了接收器("彼得平移")。
  •     Softness 柔化 Scales the penumbra region (the offset of blur samples). Directional light only.
    缩放半阴影区(模糊样本的偏移量)。只用于方向光
  •     Softness Fade 柔化淡出 Shadow softness fade based on the distance from the camera. Directional light only.
    阴影柔化是根据到相机距离进行淡出的。只用于方向光。
  • Draw Halo 绘制光晕 If checked, a spherical halo of light will be drawn with a radius equal to Range.
    如果勾选此项, 光线带有一定半径范围的球形光晕被绘制。
  • Flare 耀斑 Optional reference to the Flare that will be rendered at the light's position.
    可选的。参考耀斑(Flare) , 在光的位置渲染出来。
  • Render Mode 渲染模式 Importance of this light. This can affect lighting fidelity and performance, see Performance Considerations below. Options include:
    这灯光的重要性。这可能会影响照明的保真度和性能,请参阅下面的性能注意事项。包括:
  •     Auto 自动 The rendering method is determined at runtime depending on the brightness of nearby lights and current Quality Settings for desktop build target.
    为桌面构建目标渲染的方法是根据附近的灯光的亮度和当前的质量设置( Quality Settings)在运行时确定。
  •     Important 重要 This light is always rendered at per-pixel quality. Use this for very important effects only (e.g. headlights of a player's car).
    灯光是逐个像素渲染。只用在一些非常重要的效果(比如玩家的车的车头灯)。
  •     Not Important 不重要 This light is always rendered in a faster, vertex/object light mode.
    灯光总是以最快的速度渲染。顶点/对象光模式
  • Culling Mask 消隐遮罩 Use to selectively exclude groups of objects from being affected by the light; see Layers.
    有选择地使组对象不受光的效果影响;请看层(Layers.)。
  • Lightmapping 光照贴图 The Lightmapping mode: RealtimeOnly, Auto or BakedOnly; see the Dual Lightmaps description.
    光照贴图的模式:仅灯光实时计算(RealtimeOnly), 自动(Auto) or 仅烘焙( BakedOnly) 请看双光照贴图(Dual Lightmaps )

Details 细节

There are three basic light types in Unity. Each type can be customized to fit your needs.

在Unity中光有三种基本类型。各类型可以定制,以满足您的需要。

You can create a texture that contains an alpha channel and assign it to the Cookie variable of the light. The Cookie will be projected from the light. The Cookie's alpha mask modulates the light amount, creating light and dark spots on surfaces. They are a great way af adding lots of complexity or atmosphere to a scene.

你可以创建一个包含alpha通道的纹理,并将其赋予光线的Cookie变量。Cookie会被光线投影。cookie的Alpha遮罩调节光量,可以在表面产生亮点和黑点。他们是往场景加入大量复杂的东西或气氛的一种很好的方法。

All built-in shaders in Unity seamlessly work with any type of light. VertexLit shaders cannot display Cookies or Shadows, however.

Unity里所有的内置的着色器(built-in shaders)全都可用于任何类型的灯光。然而,VertexLit 着色器不能显示Cookies或者阴影。

In Unity Pro with a build target of webplayer or standalone, all Lights can optionally cast Shadows. This is done by selecting either Hard Shadows or Soft Shadows for the Shadow Type property of each individual Light. For more information about shadows, please read the Shadows page.

在Unity专业版,编译目标为网页版或单机版,所有的灯光可以选择性地投射阴影(Shadows)。这是通过选择每个灯光阴影类型( Shadow Type)属性为"硬阴影或软阴影来完成的。更多关于阴影的信息,请阅读阴影(Shadows )页。

Point Lights 点光源

Point Lights 点光源

Point lights shine out from a point in all directions. They are the most common lights in computer games - typically used for explosions, light bulbs, etc. They have an average cost on the graphics processor (though point light shadows are the most expensive).

点光源光线在某一点从各个方向发射。它们是电脑游戏中最常用的灯光——通常用于爆炸、灯泡、等。他们有一个图形处理器的平均成本(虽然点光源阴影是最昂贵的---最耗费资源的)。

Point Lights 点光源
Point Light 点光源

Point lights can have cookies - Cubemap texture with alpha channel. This Cubemap gets projected out in all directions.

点光源可以有cookies – 带有 alpha通道的立方图(Cubemap )纹理。立方图 (Cubemap)可在各个方向得出投影。

Point Light with a Cookie 带有Cookie的点光源
Point Light with a Cookie 带有Cookie的点光源

Spot Lights 聚光灯

Spot Lights 聚光灯

Spot lights only shine in one direction, in a cone. They are perfect for flashlights, car headlights or lamp posts. They are the most expensive on the graphics processor.

聚光灯只在一个方向上,在一个圆锥体范围发射光线。它们用作手电筒真是完美,汽车的车头灯或者灯柱。它们在图形处理器上是最耗费资源的。

Spot Lights 聚光灯
Spot Light 聚光灯

Spot lights can also have cookies - a texture projected down the cone of the light. This is good for creating an effect of light shining through the window. It is very important that the texture is black at the edges, has Border Mipmaps option on and its wrapping mode is set to Clamp. For more info on this, see Textures.

聚光灯同样也可以带有cookies - 这可以很好地创建光芒透过窗户的效果。纹理的边缘是黑色的,打开边框多层纹理(Border Mipmaps )选项和它的循环模式(wrapping mode)设置为强制拉伸(Clamp),这是非常重要的。想要得到更多关于这方面的信息,参见纹理(Textures.)

Spot Light with a Cookie 带有Cookie的聚光灯
Spot Light with a Cookie 带有Cookie的聚光灯

Directional Lights 方向光源

Directional Lights 方向光源

Directional lights are used mainly in outdoor scenes for sun & moonlight. The light affect all surfaces of objects in your scene. They are the least expensive on the graphics processor. Shadows from directional lights (for platforms that support shadows) are explained in depth on this page.

方向光主要用于模拟在室外场景的阳光与月光。方向光影响你的场景中的对象的所有表面。 他们在图形处理器中是最不耗费资源的。方向光产生的阴影(因为平台支持阴影)标识着在此页(this page)上的深度。

Directional Lights 方向光源
Directional Light 方向光源

When directional light has a cookie, it is projected down the center of the light's Z axis. The size of the cookie is controlled with Cookie Size property. Set the cookie texture's wrapping mode to Repeat in the Inspector.

当平行光有一个cookie,它是预测光的Z轴的中心。cookie大小由cookie的大小(Cookie Size)属性控制。在检视面板设置cookie贴图的循环模式(wrapping mode)为重复(Repeat)

方向光投影一个云状的cookie纹理
Directional Light projecting a cloud-like cookie texture
方向光投影一个云状的cookie纹理

A cookie is a great way to add some quick detail to large outdoor scenes. You can even slide the light slowly over the scene to give the impression of moving clouds.

Cookie是在大型户外场景中快速添加细节的好方法。你甚至可以将的灯光在场景中慢慢移动,给人的感觉就像云在动。

Performance considerations 性能考虑

Lights can be rendered in one of two methods: vertex lighting and pixel lighting. Vertex lighting only calculates the lighting at the vertices of the game models and interpolates the lighting over the surfaces of the models. Pixel lights are calculated at every screen pixel, and hence are much more expensive. Some older graphics cards only support vertex lighting.

灯光有两种渲染方式:顶点(vertex )光照和像素(pixel )光照。顶点光照只计算游戏模型的顶点照明和通过插值计算得出模型表面所有光线。灯光在屏幕每一个像素计算,因此很奢侈(耗费资源)。一些旧的显卡只支持顶点光照。

While pixel lighting is slower to render, it does allow some effects that are not possible with vertex lighting. Normal-mapping, light cookies and realtime shadows are only rendered for pixel lights. Spotlight shapes and Point light highlights are much better when rendered in pixel mode as well. The three light types above would look like this when rendered in vertex light mode:

像素光照渲染得慢,但它能够实现一些顶点光照不能实现的效果。正规映射(Normal-mapping),灯光cookies(light cookies) 和实时阴影(realtime shadows)只能由像素光照渲染。在像素光照模式下,聚光灯的形状和点光源亮点效果更好。当在顶点光照模式下渲染,以上三种光照类型的效果就像这样:

Performance considerations 性能考虑
Point light in Vertex lighting mode. 在顶点光照模式下的点光源

Performance considerations 性能考虑
Spot light in Vertex lighting mode. 在顶点光照模式下的聚光灯

Performance considerations 性能考虑
Directional light in Vertex lighting mode. 在顶点光照模式下的方向光

Lights have a big impact on rendering speed - therefore a tradeoff has to be made betwen lighting quality and game speed. Since pixel lights are much more expensive than vertex lights, Unity will only render the brightest lights at per-pixel quality. The actual number of pixel lights can be set in the Quality Settings for webplayer and standalone build targets.

灯光有对渲染速度有非常大的影响——因此必须权衡前后照明质量和游戏速度。由于像素光照比顶点光照奢侈得多(更耗费资源),Unity将只在最亮的光逐个像素渲染。网页版(webplayer)和单机版(standalone)编译目标中,像素灯的实际数量可以质量设置( Quality Settings )进行设置。

You can explicitly control if a light should be rendered as a vertex or pixel light using the Render Mode property. By default Unity will classify the light automatically based on how much the object is affected by the light.

光线以顶点光照或像素光照模式渲染。你可以通过渲染模式属性(Render Mode property)显式控制。默认情况下,Unity根据对象受到光影响的多少自动对光进行归类。

The actual lights that are rendered as pixel lights are determined on an object-by-object case. This means:

实际灯光被渲染在object-by-object的情况下确定作为像素光渲染。这句话的意思是:

  • Huge objects with bright lights could use all the pixel lights (depending on the quality settings). If the player is far from these, nearby lights will be rendered as vertex lights. Therefore, it is better to split huge objects up in a couple of small ones.
    带有明亮灯光的巨大物体,可以全用像素光照(根据质量设置)。如果玩家非常远,附近的灯光可以用顶点光照模式渲染。因此,巨大的物体应分割成几个小的物体这样更好。

See Optimizing Graphics performance on Desktop, iOS or Android page for more information.

在 台式机(Desktop),iOS 或Android 页面参见优化图形性能(Optimizing Graphics performance),获取更多信息。

Creating Cookies

For more information on creating cookies, please see the tutorial on how to create a Spot light cookie.

创建的Cookie的更多信息,请参阅关于如何创建一个射灯cookie的教程( tutorial on how to create a Spot light cookie)

Hints 提示

  • Spot lights with cookies can be extremely effective for making light coming in from windows.
    带cookies的聚光灯可以很有效的制作出光线从窗户射进来的效果。
  • Low-intensity point lights are good for providing depth to a scene.
    低强度点光源带来场景深度。
  • For maximum performance, use a VertexLit shader. This shader only does per-vertex lighting, giving a much higher throughput on low-end cards.
    为了获得最佳性能,使用VertexLit 着色。这个着色器(shader)只进行顶点光照,在低端卡提供高得多的吞吐量。