6.4.1逐顶点光照
Shader "Custom/Chap6.4.1/lambert vertex Shader"
{
Properties {
// 声明一个Color类型的属性
_Color ("Color Tint", Color) = (1.0,1.0,1.0,1.0)
_Diffuse("Diffuse", Color) = (1.0,1.0,1.0,1.0)
}
SubShader {
pass
{
// 定义光照模式
// Tags { "LightMode"="ForwardBase" }
// urp 中定义 RenderPipeline 和 lightmode 定义如下
Tags { "RenderPipeline" = "UniversalRenderPipeline" "RenderType" = "Opaqua" "LightMode" = "UniversalForward"}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// 使用光照内置文件
// #include "Lighting.cginc"
// #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
// 使用urp光照文件
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
// 将材质属性声明在一个名为UnityPerMaterial的CBUFFER块中,这样才能保证该Shader能够兼容SRP的批处理
CBUFFER_START(UnityPerMaterial)
// 需要定义一个与 properties 中定义的 _Color 相符的变量来接收
float4 _Color;
float4 _Diffuse;
CBUFFER_END
struct a2v {
float4 vertex: POSITION; // 顶点坐标
float3 normal: NORMAL; // 法线
float4 texcoord: TEXCOORD0; // 第一套纹理坐标
};
struct v2f {
// SV_POSITION语义告诉Unity, pos里包含了顶点在裁剪空间中的位置信息
float4 pos: SV_POSITION;
// COLORO语义可以用于存储颜色信息
float3 color: COLOR0;
};
v2f vert(a2v v) {
v2f o;
// 裁剪坐标 - urp 方式
o.pos = TransformObjectToHClip(v.vertex);
// urp中环境光
half3 ambient = _GlossyEnvironmentColor.xyz;
// Urp 中法向量变换成世界坐标
float3 worldNormal = TransformObjectToWorldNormal(v.normal);
// URP 中获取灯光数据
Light worldLight = GetMainLight();
half3 worldLightDir = TransformObjectToWorldDir(worldLight.direction);
// URP内部兰伯特漫反射计算函数
half3 diffuse = LightingLambert(worldLight.color.rgb, worldLightDir, worldNormal) * _Diffuse.rgb;
// 颜色 = 环境光 + 漫反射
o.color = ambient + diffuse;
return o;
}
float4 frag(v2f i): SV_Target {
float3 c = i.color;
// 将插值后的i.color显示到屏幕上
c *= _Color.rgb;
return float4(c, 1.0);
}
ENDHLSL
}
}
}
// Light GetMainLight()
// {
// Light light;
// light.direction = _MainLightPosition.xyz;
// // unity_LightData.z is 1 when not culled by the culling mask, otherwise 0.
// light.distanceAttenuation = unity_LightData.z;
// #if defined(LIGHTMAP_ON) || defined(_MIXED_LIGHTING_SUBTRACTIVE)
// // unity_ProbesOcclusion.x is the mixed light probe occlusion data
// light.distanceAttenuation *= unity_ProbesOcclusion.x;
// #endif
// light.shadowAttenuation = 1.0;
// light.color = _MainLightColor.rgb;
// return light;
// }