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:距离衰减方向


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

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

相关推荐
叶帆8 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君8 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子9 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai3152475439 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光9 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光9 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯9 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案9 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔9 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技9 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d