Unity中URP下获取每一个额外灯数据

文章目录


前言

在上一篇文章中,我们知道了URP下是怎么获取额外灯数量的。

在这篇文章中,我们来了解一下怎么获取每一盏额外灯的数据。


一、我们先来看一下 SimpleLit 中的调用

  • SimpleLit中调用了 GetAdditionalLight
  • 该函数有三个重载,我们分别称为1号、2号、3号重载,方便后面区分
  • 3号调用了2号

读源码后,可知,Unity在获取额外灯信息前。获取了额外灯索引。

所以,我们也分成两步来分析:获取额外灯索引、获取索引对应的额外灯信息。


二、获取额外灯索引

这里我们主要只是看一下,在Shader种,并不会去做修改。

  • 正常情况下,我们通过 GetPerObjectLightIndex 函数获取额外灯索引
  • GetPerObjectLightIndex:主要分为3种情况。

1、非移动平台

2、非GLES平台

3、大多数平台


三、获取额外灯数据

  • 获取额外灯的数据,使用了 GetAdditionalPerObjectLight 函数

  • 这是该函数的主要内容,我们接下来,分析一下该函数干了什么

    // Fills a light struct given a perObjectLightIndex
    Light GetAdditionalPerObjectLight(int perObjectLightIndex, float3 positionWS)
    {
    // Abstraction over Light input constants
    #if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
    float4 lightPositionWS = _AdditionalLightsBuffer[perObjectLightIndex].position;
    half3 color = _AdditionalLightsBuffer[perObjectLightIndex].color.rgb;
    half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation;
    half4 spotDirection = _AdditionalLightsBuffer[perObjectLightIndex].spotDirection;
    uint lightLayerMask = _AdditionalLightsBuffer[perObjectLightIndex].layerMask;
    #else
    float4 lightPositionWS = _AdditionalLightsPosition[perObjectLightIndex];
    half3 color = _AdditionalLightsColor[perObjectLightIndex].rgb;
    half4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[perObjectLightIndex];
    half4 spotDirection = _AdditionalLightsSpotDir[perObjectLightIndex];
    uint lightLayerMask = asuint(_AdditionalLightsLayerMasks[perObjectLightIndex]);
    #endif

    复制代码
      // 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);
    
      half3 lightDirection = half3(lightVector * rsqrt(distanceSqr));
      // full-float precision required on some platforms
      float attenuation = DistanceAttenuation(distanceSqr, distanceAndSpotAttenuation.xy) * AngleAttenuation(spotDirection.xyz, lightDirection, distanceAndSpotAttenuation.zw);
    
      Light light;
      light.direction = lightDirection;
      light.distanceAttenuation = attenuation;
      light.shadowAttenuation = 1.0; // This value can later be overridden in GetAdditionalLight(uint i, float3 positionWS, half4 shadowMask)
      light.color = color;
      light.layerMask = lightLayerMask;
    
      return light;

    }

  • 按照是否是移动平台,根据之前的计算的索引 获取到了额外光的主要数据

lightPositionWS:额外灯在世界空间下的坐标

color:额外灯颜色

distanceAndSpotAttenuation:距离衰减

spotDirection:距离衰减方向


有了这写数据,就可以计算额外灯的方向、距离衰减和角度衰减了。

我们在之后的文章中,来分析额外灯方向、距离衰减和角度衰减是怎么计算的。

相关推荐
胜天半子_王二_王半仙3 小时前
godot源码编译
游戏引擎·godot
Thomas_YXQ3 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
Tandy12356_4 小时前
Godot开发2D冒险游戏——第二节:主角光环整起来!
游戏引擎·godot
星火撩猿11 小时前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot
sky_smile_Allen12 小时前
[Unity]-[UI]-[Prefab] 关于Unity UGUI 的布局及组件讲解
ui·unity·游戏引擎
虾球xz14 小时前
游戏引擎学习第244天: 完成异步纹理下载
c++·学习·游戏引擎
太妃糖耶17 小时前
URP-利用矩阵在Shader中实现物体的平移和缩放
unity·矩阵
Magnum Lehar20 小时前
ApophisZerg游戏引擎项目目录展示
人工智能·vscode·编辑器·游戏引擎
Tandy12356_1 天前
Godot开发2D冒险游戏——第一节:主角登场!
python·游戏引擎·godot
是阿根1 天前
unity使用iTextSharp生成PDF文件
unity·c#·游戏引擎