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

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-26 14:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-26 14:58:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-26 14:58:01       82 阅读
  4. Python语言-面向对象

    2024-01-26 14:58:01       91 阅读

热门阅读

  1. 第八章 使用 SQL Search - SQL 搜索示例

    2024-01-26 14:58:01       49 阅读
  2. tmux原理及常用指令、快捷键介绍

    2024-01-26 14:58:01       59 阅读
  3. MySQL ORDER BY 实现原理

    2024-01-26 14:58:01       53 阅读
  4. 基础前端知识第一期:DIV 标签

    2024-01-26 14:58:01       54 阅读
  5. 大数据量分页优化,应对PageHelper

    2024-01-26 14:58:01       50 阅读
  6. 常见的前端打包构建工具有哪些

    2024-01-26 14:58:01       49 阅读
  7. 指针与引用的区别

    2024-01-26 14:58:01       53 阅读
  8. 前端同时上传json对象和MultipartFile文件

    2024-01-26 14:58:01       52 阅读
  9. React 表单、处理受控表单组件、非受控组件

    2024-01-26 14:58:01       45 阅读
  10. Nginx

    Nginx

    2024-01-26 14:58:01      36 阅读
  11. 前端demo: 将传入文件压缩到不超过指定大小(M)

    2024-01-26 14:58:01       53 阅读