Unity中URP下额外灯的距离衰减

文章目录


前言

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

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


一、额外灯的距离衰减

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

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=lightRangeSqr−fadeStartDistanceSqrlightRangeSqr−distanceToLightSqr

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

  • 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=lightRange∗lightRange
  • 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.8f∗0.8f∗lightRangeSqr;(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=lightRangeSqr−fadeStartDistanceSqrlightRangeSqr−distanceToLightSqr
  • 可以看出经过该公式计算后,我们的灯光强度在灯光距离限制处刚好为零
  • 这样就实现了额外灯的距离衰减效果了
相关推荐
SmalBox15 小时前
【光照】[漫反射diffuse]以UnityURP为例
unity·渲染
SmalBox1 天前
【光照】[自发光Emission]以UnityURP为例
unity·渲染
SmalBox3 天前
【光照】Unity中的[经验模型]
unity·渲染
萘柰奈3 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
Yasin Chen3 天前
Unity UI坐标说明
ui·unity
应用市场3 天前
无人机姿态控制系统详解与实现
游戏引擎·cocos2d
陈言必行3 天前
Unity 性能优化 之 编辑器创建资源优化( 工作流 | 场景 | 预制体)
unity·编辑器·游戏引擎
1uther3 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
死也不注释3 天前
【Unity UGUI 交互组件——Slider(7)】
unity·游戏引擎·交互
程序猿多布4 天前
XLua教程之热补丁技术
unity·c#·lua·xlua