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
  • 可以看出经过该公式计算后,我们的灯光强度在灯光距离限制处刚好为零
  • 这样就实现了额外灯的距离衰减效果了
相关推荐
向宇it2 小时前
【unity小技巧】封装unity适合2D3D进行鼠标射线检测,获取鼠标位置信息检测工具类
游戏·3d·unity·游戏引擎
年少无知且疯狂7 小时前
【Unity】实现小地图
unity·游戏引擎
AA陈超7 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 #06-11:游戏后效果执行
c++·游戏·ue5·游戏引擎·虚幻
徐子竣12 小时前
[学习记录]Unity-Shader-常量缓冲区(CBUFFER)
学习·unity·游戏引擎
大飞pkz12 小时前
【Unity】Application类常见路径
unity·游戏引擎·游戏开发·application常用路径·数据路径·数据存储位置
向宇it12 小时前
【unity小技巧】封装一套 Unity 的植物生长系统
游戏·unity·c#·游戏引擎
星星火柴9361 天前
AscendantPath | 实现视觉小说的人物对话系统(二)
游戏·unity·游戏程序·版本为2022.3.57·配合github下载地址使用
死也不注释1 天前
【在Unity游戏开发中Dictionary、List介绍】
数据结构·游戏·unity·游戏引擎·list·游戏程序
沧海归城2 天前
Unity_XR控制手部动画
unity·游戏引擎·xr
★YUI★2 天前
学习游戏制作记录(冻结敌人时间与黑洞技能)7.30
学习·游戏·unity·c#