本文介绍: 在上一篇文章中,我们分析了额外灯的方向怎么计算。在这篇文章中,我们来分析一下额外灯的距离衰减。


前言

在上一篇文章中,我们分析了额外灯的方向怎么计算。

在这篇文章中,我们来分析一下额外灯的距离衰减。


一、额外灯的距离衰减

  • 在上一篇文章中,完成了额外灯方向计算后,来到了计算额外光的衰减部分
  • 衰减包括:距离衰减 和 角度衰减
  • 我们这篇文章主要分析 距离衰减

DistanceAttenuation(distanceSqr, distanceAndSpotAttenuation.xy)

在这里插入图片描述

二、DistanceAttenuation函数的传入参数

  • 调用

DistanceAttenuation1(distanceSqr, distanceAndSpotAttenuation.xy)

1、distanceSqr

  • 这个计算光线向量后的点积结果

// Directional lights store direction in lightPosition.xyz and have .w set to 0.0.
// This way the following code will work for both directional and punctual lights.
float3 lightVector = lightPositionWS.xyz – positionWS * lightPositionWS.w;
float distanceSqr = max(dot(lightVector, lightVector), HALF_MIN);

2、distanceAndSpotAttenuation

  • 该参数是通过内置变量获取的
    在这里插入图片描述
  • 该参数是通过C#脚本提前计算好的
    在这里插入图片描述

3、_AdditionalLightsAttenuation

  • 我们来看一下C#是怎么计算得出该参数的

在这里插入图片描述
在这里插入图片描述

  • 衰减默认值(平行光)
    在这里插入图片描述

  • k_DefaultLightAttenuation,默认值为(0,0,0,1)
    在这里插入图片描述

  • 非平行光下,会对衰减值进行修改
    在这里插入图片描述

  • 该函数对衰减值,做出了修改。使衰减值在灯光限制处刚好为0
    在这里插入图片描述

4、GetPunctualLightDistanceAttenuation函数

该函数为以下参数做出准备:

  • Unity使灯光限制处为0的公式:
  • s

    m

    o

    o

    t

    h

    F

    a

    c

    t

    o

    r

    =

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    d

    i

    s

    t

    a

    n

    c

    e

    T

    o

    L

    i

    g

    h

    t

    S

    q

    r

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    f

    a

    d

    e

    S

    t

    a

    r

    t

    D

    i

    s

    t

    a

    n

    c

    e

    S

    q

    r

    smoothFactor=frac{lightRangeSqr – distanceToLightSqr}{lightRangeSqr -fadeStartDistanceSqr}

    smoothFactor=lightRangeSqrfadeStartDistanceSqrlightRangeSqrdistanceToLightSqr

我们来看一下该公式实现了什么

  • l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    =

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    lightRangeSqr = lightRange * lightRange

    lightRangeSqr=lightRangelightRange

  • f

    a

    d

    e

    S

    t

    a

    r

    t

    D

    i

    s

    t

    a

    n

    c

    e

    S

    q

    r

    =

    0.8

    f

    0.8

    f

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    ;

    fadeStartDistanceSqr = 0.8f * 0.8f * lightRangeSqr;

    fadeStartDistanceSqr=0.8f0.8flightRangeSqr;(0.8指的是80%开始衰减)

  • d

    i

    s

    t

    a

    n

    c

    e

    T

    o

    L

    i

    g

    h

    t

    S

    q

    r

    :灯光的衰减距离的平方

    distanceToLightSqr:灯光的衰减距离的平方

    distanceToLightSqr:灯光的衰减距离的平方
    在这里插入图片描述


三、DistanceAttenuation函数的程序体

在这里插入图片描述

  • s

    m

    o

    o

    t

    h

    F

    a

    c

    t

    o

    r

    =

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    d

    i

    s

    t

    a

    n

    c

    e

    T

    o

    L

    i

    g

    h

    t

    S

    q

    r

    l

    i

    g

    h

    t

    R

    a

    n

    g

    e

    S

    q

    r

    f

    a

    d

    e

    S

    t

    a

    r

    t

    D

    i

    s

    t

    a

    n

    c

    e

    S

    q

    r

    smoothFactor=frac{lightRangeSqr – distanceToLightSqr}{lightRangeSqr -fadeStartDistanceSqr}

    smoothFactor=lightRangeSqrfadeStartDistanceSqrlightRangeSqrdistanceToLightSqr

  • 可以看出经过该公式计算后,我们的灯光强度在灯光距离限制处刚好为零
    请添加图片描述
  • 这样就实现了额外灯的距离衰减效果了
    请添加图片描述

原文地址:https://blog.csdn.net/qq_51603875/article/details/135847544

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_62183.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注