您可能需要在运行时移动光照探针。例如,如果通过额外加载多个场景来创建世界,然后将每个场景移动到不同位置,则需要移动光照探针及其相关的场景对象。
您可以使用 LightProbes API 在运行时移动光照探针。无法通过更新光照探针组的变换组件来移动光照探针,因为变换组件仅影响烘焙。
使用 API 移动光照探针时,只有位置会发生变化。光照探针中存储的烘焙数据保持不变。
请遵循以下步骤:
Scene 对象)将使用此克隆的数据。如果您将场景与包含光照探针的其他场景一起烘焙,则返回的数据包含所有烘焙场景中的光照探针。有关在多个场景中使用光照探针的更多信息,请参阅光照探针和场景加载。
Tetrahedralize API 可能需要很长时间。如果移动多个光照探针,最好在最后进行一次四面体化。
如果需要读取场景中光照探针的位置,但不能对其进行移动,可以改用 LightProbes.GetSharedLightProbesForScene API。
以下代码将当前场景中的光照探针移动到每帧的新位置。
将脚本附加到场景中的任何对象。运行项目时,请选择任何使用光照探针的对象,以便可以看到探针的移动。光照探针组对象不会移动。
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadSingleSceneAndMoveProbes : MonoBehaviour
{
void Update()
{
// Get a copy of the Light Probes in the current scene
LightProbes lightProbes = LightProbes.GetInstantiatedLightProbesForScene(SceneManager.GetActiveScene());
// Get the Light Probe positions
Vector3[] positions = lightProbes.GetPositionsSelf();
// Update the positions
for (int i = 0; i < positions.Length; i++)
{
positions[i].x = positions[i].x + 0.01f;
}
// Copy the new positions back to the Light Probes
lightProbes.SetPositionsSelf(positions, true);
// Tetrahedralize to update the data in the LightProbes object of the scene.
LightProbes.Tetrahedralize();
}
}