Unity——URP填坑——主光源怎么来的?
看到URP中的选择主光源代码,对于每个相机而言,在影响这个相机的光照的所有光源中进行筛选出主光源,过程:首先主光源必须是平行光,然后如果在渲染的环境中设置过Sun的光源,那么就选这个光源为主光源(前提是影响这个相机渲染的,也就是经过Culling的),最后就以亮度最高的(intensity值最大的)为主光源。
static int GetMainLightIndex(UniversalRenderPipelineAsset settings, NativeArray<VisibleLight> visibleLights) { using var profScope = new ProfilingScope(null, Profiling.Pipeline.getMainLightIndex); int totalVisibleLights = visibleLights.Length; if (totalVisibleLights == 0 || settings.mainLightRenderingMode != LightRenderingMode.PerPixel) return -1; Light sunLight = RenderSettings.sun; int brightestDirectionalLightIndex = -1; float brightestLightIntensity = 0.0f; for (int i = 0; i < totalVisibleLights; ++i) { VisibleLight currVisibleLight = visibleLights[i]; Light currLight = currVisibleLight.light; // Particle system lights have the light property as null. We sort lights so all particles lights // come last. Therefore, if first light is particle light then all lights are particle lights. // In this case we either have no main light or already found it. if (currLight == null) break; if (currVisibleLight.lightType == LightType.Directional) { // Sun source needs be a directional light if (currLight == sunLight) return i; // In case no sun light is present we will return the brightest directional light if (currLight.intensity > brightestLightIntensity) { brightestLightIntensity = currLight.intensity; brightestDirectionalLightIndex = i; } } } return brightestDirectionalLightIndex; }