Version: 6000.3
语言: 中文
在自定义 URP 着色器中使用光照
在自定义 URP 着色器中使用阴影

在自定义URP着色器中使用间接光照

在自定义通用中使用间接照明渲染管线(Render Pipeline) 获取场景内容并将其显示在屏幕上的一系列作。Unity 允许您从预构建的渲染管道中进行选择,或编写自己的渲染管道。更多信息
请参阅术语表
(URP)着色器在 GPU 上运行的程序。更多信息
请参阅术语表
,请按照下列步骤作:

  1. 添加一个#include行内HLSLPROGRAM块。有关特定的包含文件路径,请参阅本页上的部分。
  2. 使用以下部分中的任何方法。

在自定义URP着色器中使用反射探针

使用反射探针一种渲染组件,可捕获周围环境的各个方向的球面视图,就像相机一样。然后,捕获的图像将存储为立方体贴图,可供具有反射材质的对象使用。更多信息
请参阅术语表
天空盒(skybox) 一种特殊类型的材质,用于表示天空。通常是六面的。更多信息
请参阅术语表
,请使用以下命令#include线:

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/GlobalIllumination.hlsl"
方法 语法 描述
GlossyEnvironmentReflection half3 GlossyEnvironmentReflection(half3 reflectVector, float3 positionWS, half perceptualRoughness, half occlusion, float2 normalizedScreenSpaceUV) 如果对象位于反射探针体积内,则对反射探针进行采样。否则对天空盒进行采样。该函数支持反射探针混合
perceptualRoughness参数选择采样纹理的 MIP 级别。值越高,反射看起来越模糊。
occlusion参数是反射探针衰减因子。
normalizedScreenSpaceUVargument 是片段的规范化屏幕空间位置。

GlossyEnvironmentReflection函数具有以下更简单的重载,它不支持混合:

half3 GlossyEnvironmentReflection(half3 reflectVector, half perceptualRoughness, half occlusion)

自适应探头体积样本

要对自适应探针体积 (APV) 进行采样,请使用以下命令#include线:

#include "Packages/com.unity.render-pipelines.core/Runtime/Lighting/ProbeVolume/ProbeVolume.hlsl"
方法 语法 描述
EvaluateAdaptiveProbeVolume void EvaluateAdaptiveProbeVolume(in float3 posWS, in float3 normalWS, in float3 viewDir, in float2 positionSS, in uint renderingLayer, out float3 bakeDiffuseLighting) 自适应探针体积中采样光照。
positionSS参数是正在渲染的片段的屏幕空间位置。
renderingLayer参数是渲染层遮罩

采样光探头

要对光探针进行采样,请使用以下命令#include线:

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/AmbientProbe.hlsl"
方法 语法 描述
SampleSH real3 SampleSH(real3 normalWS) 样本光探针光探针存储有关光线如何穿过场景中的空间的信息。在给定空间中排列的光源探针集合可以改善移动对象的光照和该空间内的静态LOD场景。更多信息
请参阅术语表
.

以下URP着色器代码计算漫反射和镜面反射环境光源不来自任何特定方向的光源,在所有方向上为场景提供相等的光源。更多信息
请参阅术语表
使用GlossyEnvironmentReflection功能。

float3 CustomLightingAmbient(float3 BaseColor, 
                            float3 NormalWS, 
                            float Metallic, 
                            float Smoothness, 
                            float Reflectance, 
                            float AmbientOcclusion, 
                            float PositionWS, 
                            float2 screenspaceUV, 
                            float3 ViewDirWS)
{
    // Diffuse ambient lighting.
    // Sample the sky probe with high roughness and use a normal vector instead of
    // a reflection vector to approximate ambient light
    float3 DiffuseAmbient = GlossyEnvironmentReflection(NormalWS, PositionWS, 1, 1, screenspaceUV);
    // If the surface is metallic, set ambient to zero, otherwise multiply by BaseColor
    DiffuseAmbient *= lerp(BaseColor, float3(0,0,0), Metallic);

    // Specular ambient lighting - reflections
    // Surfaces are more reflective at glancing angles. Compute the fresnel value for reflectance.
    float Fresnel = pow((1 - dot(NormalWS, ViewDirWS)), 5);
    // Reflectance is 100% at the glancing angle
    Reflectance = lerp(Reflectance, 1, Fresnel);
    // If the object is metal, use the base color for reflectance instead.
    float3 ReflectionColor = lerp(float3(Reflectance, Reflectance, Reflectance), BaseColor, Metallic);
    // Compute the reflection vector and use it to sample the sky probe for reflections.
    float3 ReflectionVector = reflect(-ViewDirWS, NormalWS);
    float3 SpecularAmbient = GlossyEnvironmentReflection(ReflectionVector, PositionWS, 1 - Smoothness, 1, screenspaceUV);
    
    return (DiffuseAmbient + SpecularAmbient) * AmbientOcclusion;
}

着色器图自定义函数节点示例

着色器图表功能示例包含自定义光照模型的示例。

要了解如何使用GlossyEnvironmentReflection自定义函数节点中的函数:

  1. 在示例中场景场景包含游戏的环境和菜单。将每个唯一的场景文件视为一个独特的关卡。在每个场景中,你放置你的环境、障碍物和装饰品,基本上是将你的游戏设计和构建成碎片。更多信息
    请参阅术语表
    ,打开 CustomLightingSimple 着色器图表。

  2. 导航到 SampleReflectionProves(SampleReflectionProbes) 子图。

  3. 检查 URPReflectionProbe 节点。该节点包含使用GlossyEnvironmentReflection功能:

    #ifdef SHADERGRAPH_PREVIEW
        reflection = float3(0,0,0);
    #else
        reflection = GlossyEnvironmentReflection(reflectVector, positionWS, roughness, 1.0h, screenspaceUV);
    #endif
    

其他资源

在自定义 URP 着色器中使用光照
在自定义 URP 着色器中使用阴影