You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D中如何实时获取玩家对象所在位置的光照强度?

实时获取玩家位置光照强度的替代方案(Unity)

方法1:直接计算定向光/点光源的实时影响

如果场景以定向光(如太阳光)或点光源为主,可以通过向量计算和射线检测直接获取玩家位置的受光强度:

public Light mainLight;
public Transform playerTransform;

void Update()
{
    // 根据光源类型计算光照方向
    Vector3 lightDir = mainLight.type == LightType.Directional 
        ? -mainLight.transform.forward 
        : (playerTransform.position - mainLight.transform.position).normalized;
    
    // 用玩家向上向量模拟表面法线,可根据实际模型调整
    Vector3 surfaceNormal = Vector3.up;
    float dot = Vector3.Dot(surfaceNormal, lightDir);
    float baseIntensity = Mathf.Max(0, dot) * mainLight.intensity;

    // 射线检测判断是否处于阴影中
    if (Physics.Raycast(playerTransform.position, lightDir, out RaycastHit hit, 100f))
    {
        if (hit.collider.gameObject != mainLight.gameObject)
        {
            baseIntensity *= 0.15f; // 阴影下强度衰减比例可自定义
        }
    }

    // 根据光照强度计算心理健康衰减间隔x
    float x = Mathf.Lerp(1f, 10f, baseIntensity / mainLight.intensity);
}

方法2:静态场景采样光照贴图

如果场景是静态烘焙的,可以直接采样玩家位置对应的光照贴图亮度:

public Renderer staticGroundRenderer; // 玩家脚下的静态地面Renderer
public Transform playerTransform;

void Update()
{
    // 将玩家世界坐标转换为地面本地坐标,获取UV值
    Vector3 groundAlignedPos = playerTransform.position;
    groundAlignedPos.y = staticGroundRenderer.bounds.min.y;
    Vector2 uv = staticGroundRenderer.worldToLocalMatrix.MultiplyPoint(groundAlignedPos);
    uv.x %= 1f;
    uv.y %= 1f;

    // 采样光照贴图
    if (staticGroundRenderer.lightmapIndex >= 0)
    {
        Texture2D lightmap = LightmapSettings.lightmaps[staticGroundRenderer.lightmapIndex].lightmapColor;
        Color lightColor = lightmap.GetPixelBilinear(uv.x, uv.y);
        float lightIntensity = lightColor.grayscale; // 灰度值对应亮度

        float x = Mathf.Lerp(1.5f, 8f, lightIntensity);
    }
}

方法3:自定义范围光源检测

给玩家挂载小型触发器,实时检测范围内的所有光源并计算总强度:

public float lightDetectionRadius = 6f;
private List<Light> nearbyLights = new List<Light>();

void Start()
{
    SphereCollider detector = gameObject.AddComponent<SphereCollider>();
    detector.radius = lightDetectionRadius;
    detector.isTrigger = true;
}

void OnTriggerEnter(Collider other)
{
    Light light = other.GetComponent<Light>();
    if (light != null) nearbyLights.Add(light);
}

void OnTriggerExit(Collider other)
{
    Light light = other.GetComponent<Light>();
    if (light != null && nearbyLights.Contains(light)) nearbyLights.Remove(light);
}

void Update()
{
    float totalIntensity = RenderSettings.ambientIntensity; // 基础环境光强度

    foreach (Light light in nearbyLights)
    {
        if (light == null) continue;
        
        // 计算距离衰减(平方反比模型)
        float distance = Vector3.Distance(transform.position, light.transform.position);
        float attenuation = 1f / (distance * distance);
        totalIntensity += light.intensity * attenuation;
    }

    float x = Mathf.Clamp(totalIntensity * 4f, 1f, 12f);
}

内容的提问来源于stack exchange,提问作者LenadESP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 05:22:18