在Unity游戏开发中,光源是构建游戏世界视觉效果的关键元素。不同的光源类型能够营造出不同的氛围和效果,以下是几种常见光源及其特点的详细介绍。
点光源(Point Light)
特点
- 中心辐射:点光源从一个点向四周辐射光线。
- 距离衰减:光线强度随着距离的增加而减弱,可以通过衰减系数进行控制。
- 简单实现:易于实现,性能开销小。
适用场景
- 小范围照明:如室内场景、房间等。
- 创造中心焦点:如舞台灯光、篝火等。
代码示例
// 创建点光源
PointLight pointLight = new PointLight();
pointLight.color = Color.white;
pointLight.intensity = 5f;
pointLight.range = 10f;
pointLight.attenuation = new Vector3(0.1f, 0.05f, 0.02f);
// 添加到GameObject
GameObject lightObject = new GameObject("PointLight");
lightObject.AddComponent<Light>();
lightObject.GetComponent<Light>().type = LightType.Point;
lightObject.GetComponent<Light>().AddComponent(pointLight);
方向光源(Directional Light)
特点
- 无限延伸:光线从一个点无限延伸,模拟太阳光的效果。
- 无衰减:光线强度不随距离变化。
- 简单实现:易于实现,性能开销小。
适用场景
- 全局照明:如户外场景、日落等。
- 模拟天空光:如白天、夜晚等不同时间段。
代码示例
// 创建方向光源
DirectionalLight directionalLight = new DirectionalLight();
directionalLight.color = Color.white;
directionalLight.intensity = 1.5f;
// 添加到GameObject
GameObject lightObject = new GameObject("DirectionalLight");
lightObject.AddComponent<Light>();
lightObject.GetComponent<Light>().type = LightType.Directional;
lightObject.GetComponent<Light>().AddComponent(directionalLight);
聚光源(Spotlight)
特点
- 锥形照射:光线从一个点开始,形成一个锥形区域。
- 衰减与角度:可以设置内锥角和外锥角,控制光线照射的范围。
- 模拟聚光灯效果:如舞台灯光、探照灯等。
适用场景
- 聚焦特定区域:如舞台表演、特定场景的强调等。
- 模拟真实光源:如探照灯、车灯等。
代码示例
// 创建聚光源
SpotLight spotLight = new SpotLight();
spotLight.color = Color.white;
spotLight.intensity = 5f;
spotLight.innerAngle = 20f;
spotLight.outerAngle = 40f;
spotLight.range = 10f;
// 添加到GameObject
GameObject lightObject = new GameObject("Spotlight");
lightObject.AddComponent<Light>();
lightObject.GetComponent<Light>().type = LightType.Spotlight;
lightObject.GetComponent<Light>().AddComponent(spotLight);
环境光(Ambient Light)
特点
- 均匀照射:光线均匀地填充整个场景。
- 无阴影:不会产生阴影。
- 简单实现:易于实现,性能开销小。
适用场景
- 全局基础照明:如阴天、室内等。
- 补充光照:在其他光源不足的情况下提供基础照明。
代码示例
// 设置环境光
Camera.main.GetComponent<Light>().type = LightType.Ambient;
Camera.main.GetComponent<Light>().color = new Color(0.1f, 0.1f, 0.1f);
Camera.main.GetComponent<Light>().intensity = 0.5f;
反射光(Reflection Light)
特点
- 基于表面反射:光线根据场景中的表面反射。
- 增强真实感:使场景中的物体看起来更真实。
- 计算复杂:需要计算反射光的方向和强度。
适用场景
- 增强场景真实感:如水面、镜子等反射物体。
- 模拟复杂光照效果:如城市夜景等。
代码示例
// 添加反射探针
ReflectionProbe reflectionProbe = new ReflectionProbe();
reflectionProbe.size = ReflectionProbeSize.Medium;
GameObject probeObject = new GameObject("ReflectionProbe");
probeObject.AddComponent<ReflectionProbe>();
probeObject.AddComponent<Camera>();
probeObject.GetComponent<Camera>().cullingMask = 0;
probeObject.GetComponent<Camera>().ClearFlags = CameraClearFlags.Skybox;
probeObject.GetComponent<Camera>().background = Texture2D.whiteTexture;
probeObject.GetComponent<ReflectionProbe>().mode = ReflectionProbeMode.Default;
probeObject.AddComponent<Light>().type = LightType.Directional;
probeObject.GetComponent<Light>().color = Color.white;
probeObject.GetComponent<Light>().intensity = 1f;
probeObject.transform.position = new Vector3(0, 0, 0);
probeObject.transform.localScale = new Vector3(100, 100, 100);
通过上述介绍,我们可以看到Unity中光源的种类繁多,每种光源都有其独特的用途和效果。合理运用这些光源,可以大大提升游戏场景的真实感和表现力。
