函数 GetMainLight() 返回一个数据结构 Light
struct Light
{
half3 direction; // 颜色&强度
half3 color; // 方向
half distanceAttenuation; // 距离衰减
half shadowAttenuation; // 阴影衰减
};
Shader "Example/StandardLight" {
Properties {
_BaseColor("Base Color", Color)=(1, 1, 1, 1)
}
SubShader {
Tags {
"RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"
}
Pass {
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
half3 normal:NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
half3 worldNormal:TEXCOORD0;
};
Varyings vert(Attributes input)
{
Varyings output;
// 从对象空间到裁剪空间
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.worldNormal = TransformObjectToWorldNormal(input.normal);
return output;
}
half4 frag(Varyings input): SV_Target
{
Light mainLight = GetMainLight();
// saturate(value) 相当于 Mathf.Clamp01(value) 函数,限制值在 [0,1] 区间
float power = saturate(dot(mainLight.direction, input.worldNormal)); // 计算漫反射的强度
half4 output = _BaseColor * power * half4(mainLight.color, 1); //将表面颜色,漫反射强度和光源强度混合。
return output;
}
ENDHLSL
}
}
}
标签:half4,Varyings,Shader,half3,Unity,URP,worldNormal,input,output
From: https://www.cnblogs.com/kingBook/p/17160442.html