247 lines
11 KiB
Plaintext
247 lines
11 KiB
Plaintext
Shader "BSDF/StandardURPDouble"
|
|
{
|
|
Properties
|
|
{
|
|
_BaseMap("纹理贴图", 2D) = "white" {}
|
|
_LightMap("光照贴图", 2D) = "black" {}
|
|
_NormalMap("法线贴图", 2D) = "bump" {}
|
|
_BaseColor("基础颜色", Color) = (1, 1, 1, 1)
|
|
_Metallic("金属度", Range(0, 1)) = 0.0
|
|
_Specular("镜面反射颜色", Color) = (1, 1, 1, 1)
|
|
_Smoothness("光滑度", Range(0, 1)) = 0
|
|
_EmissionColor("自发光颜色", Color) = (0, 0, 0, 1)
|
|
_EmissionStrength("自发光强度", Float) = 0
|
|
_ClearCoatMask("清漆遮罩", Range(0, 1)) = 0
|
|
_ClearCoatSmoothness("清漆光滑度", Range(0, 1)) = 0
|
|
}
|
|
SubShader
|
|
{
|
|
Tags {
|
|
"RenderPipeline" = "UniversalPipeline"
|
|
"RenderType" = "Opaque"
|
|
}
|
|
HLSLINCLUDE
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS //主光源阴影
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE//主光源级联阴影
|
|
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_SCREEN//主光源屏幕空间阴影
|
|
#pragma multi_compile _ _ADDITIONAL_LIGHT_LAYERS//附加光源
|
|
#pragma multi_compile _ _ADDITIONAL_LIGHT_COOKIES//附加光源烘焙
|
|
#pragma multi_compile _ _ADDITIONAL_SCREEN_SPACE_SHADOWS//附加光源屏幕空间阴影
|
|
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS//附加光源
|
|
#pragma multi_compile _ _SHADOWS_SOFT_SHADOWS//软阴影
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
|
|
CBUFFER_START(UnityPerMaterial)
|
|
float4 _BaseColor;
|
|
float _Metallic;
|
|
float _Smoothness;
|
|
float4 _EmissionColor;
|
|
float _EmissionStrength;
|
|
float4 _Specular;
|
|
float _ClearCoatMask;
|
|
float _ClearCoatSmoothness;
|
|
CBUFFER_END
|
|
|
|
TEXTURE2D(_BaseMap);
|
|
SAMPLER(sampler_BaseMap);
|
|
TEXTURE2D(_LightMap);
|
|
SAMPLER(sampler_LightMap);
|
|
TEXTURE2D(_NormalMap);
|
|
SAMPLER(sampler_NormalMap);
|
|
ENDHLSL
|
|
Pass{
|
|
Name "UniversalForward"
|
|
Tags {
|
|
"LightMode" = "UniversalForward"//光照模型向前渲染
|
|
}
|
|
CULL Off//关闭剔除
|
|
HLSLPROGRAM //着色器
|
|
#pragma vertex MainVertexShader
|
|
#pragma fragment MainFragmentShader
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float2 uv0 : TEXCOORD0;
|
|
float3 normalOS : NORMAL;
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float4 positionCS : SV_POSITION;
|
|
float3 normalWS : TEXCOORD2;
|
|
float2 uv0 : TEXCOORD0;//语义相同用于传送数据
|
|
float3 positionWS : TEXCOORD1;
|
|
};
|
|
|
|
/* struct SurfaceData
|
|
{
|
|
float3 albedo;//漫反射颜色
|
|
float metallic;//金属度
|
|
float smoothness;//光滑度
|
|
float3 normalTS;//法线
|
|
//float occlusion;//遮挡
|
|
float3 emission;//自发光
|
|
float alpha;//透明度
|
|
}; */
|
|
|
|
/* struct InputData
|
|
{
|
|
float3 positionWS;//世界空间位置
|
|
float3 normalWS;//世界空间法线
|
|
float3 viewDirectionWS;//视线方向
|
|
float4 shadowCoord;//阴影坐标
|
|
float fogCoord;//雾坐标
|
|
float4 bakedGI;//烘焙全局光照
|
|
}; */
|
|
|
|
SurfaceData GetSurfaceData(Varyings input){
|
|
float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv0);
|
|
float3 baseColor = _BaseColor.rgb;
|
|
float4 normalMap = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, input.uv0);
|
|
SurfaceData surface;
|
|
surface.albedo = baseMap.rgb * baseColor;//漫反射颜色
|
|
surface.specular =_Specular.rgb;//镜面反射颜色
|
|
surface.metallic = _Metallic;//金属度
|
|
surface.smoothness = _Smoothness;//光滑度
|
|
surface.normalTS = UnpackNormal(normalMap);//法线
|
|
surface.emission = _EmissionColor.rgb;//自发光
|
|
surface.alpha = baseMap.a * _BaseColor.a;//透明度
|
|
surface.occlusion = 1.0;//环境光遮蔽
|
|
surface.clearCoatMask = _ClearCoatMask;//清漆遮罩
|
|
surface.clearCoatSmoothness = _ClearCoatSmoothness;//清漆光滑度
|
|
return surface;
|
|
}
|
|
|
|
InputData InitializeInputData(Varyings input){
|
|
InputData data;
|
|
data.positionWS = input.positionWS;
|
|
data.shadowCoord = TransformWorldToShadowCoord(input.positionWS);// 阴影坐标
|
|
data.fogCoord = ComputeFogFactor(input.positionWS);//雾坐标
|
|
data.normalWS = input.normalWS;//世界空间法线
|
|
data.viewDirectionWS = GetWorldSpaceNormalizeViewDir(input.positionWS);//视线方向
|
|
// 采样光照贴图
|
|
float2 lightmapUV = input.uv0 * unity_LightmapST.xy + unity_LightmapST.zw;
|
|
data.bakedGI = SAMPLE_TEXTURE2D(_LightMap, sampler_LightMap, lightmapUV).rgb; //SampleSH(data.normalWS); // 球谐函数采样环境光//float3(0.5, 0.5, 0.5)
|
|
//data.bakedGI = SampleSH(data.normalWS); // 球谐函数采样环境光//float3(0.5, 0.5, 0.5)
|
|
return data;
|
|
}
|
|
|
|
BRDFData SurfaceDataToBSDFData(SurfaceData surface, InputData inputData){
|
|
BRDFData bsdf;
|
|
bsdf.albedo = surface.albedo;//漫反射颜色
|
|
bsdf.specular = lerp(float3(0.04, 0.04, 0.04), surface.albedo, surface.metallic);//镜面反射颜色
|
|
bsdf.diffuse = surface.albedo * (1.0 - surface.metallic);//漫反射颜色
|
|
bsdf.reflectivity = ReflectivitySpecular(bsdf.specular);//反射率
|
|
bsdf.perceptualRoughness = 1.0 - surface.smoothness;//感知粗糙度
|
|
bsdf.roughness = bsdf.perceptualRoughness * bsdf.perceptualRoughness;//粗糙度
|
|
bsdf.roughness2 = bsdf.roughness * bsdf.roughness;//粗糙度平方
|
|
bsdf.grazingTerm =saturate(surface.smoothness+bsdf.reflectivity);//光滑度+反射率
|
|
bsdf.normalizationTerm = bsdf.roughness*4.0+2.0;//归一化项
|
|
bsdf.roughness2MinusOne = bsdf.roughness2 - 1.0;//粗糙度平方-1
|
|
return bsdf;
|
|
}
|
|
|
|
float3 BRDFToColor(BRDFData bsdf, InputData inputData,float3 positionWS){
|
|
float3 color = float3(0, 0, 0);
|
|
//主光源
|
|
float4 shadowCoord = TransformWorldToShadowCoord(positionWS);//阴影坐标
|
|
Light mainLight = GetMainLight(shadowCoord);
|
|
color += LightingPhysicallyBased(bsdf,mainLight, inputData.normalWS, inputData.viewDirectionWS);
|
|
//附加光源
|
|
for (int i = 0; i < GetAdditionalLightsCount(); ++i)
|
|
{
|
|
half4 shadowMask = CalculateShadowMask(inputData);
|
|
Light additionalLight = GetAdditionalLight(i, positionWS,shadowMask);
|
|
color += LightingPhysicallyBased(bsdf, additionalLight, inputData.normalWS, inputData.viewDirectionWS);
|
|
}
|
|
//环境光
|
|
color += inputData.bakedGI.rgb * bsdf.diffuse;//烘焙全局光照
|
|
return color;
|
|
}
|
|
|
|
Varyings MainVertexShader(Attributes input){
|
|
Varyings output;
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS);
|
|
output.positionCS = vertexInput.positionCS;
|
|
output.uv0 = input.uv0;
|
|
output.positionWS = vertexInput.positionWS;
|
|
output.normalWS = vertexNormalInput.normalWS;
|
|
return output;
|
|
}
|
|
float4 MainFragmentShader(Varyings input):SV_TARGET{
|
|
InputData inputData = InitializeInputData(input);
|
|
SurfaceData surface = GetSurfaceData(input);
|
|
BRDFData bsdf = SurfaceDataToBSDFData(surface, inputData);
|
|
float3 color = BRDFToColor(bsdf, inputData, inputData.positionWS);
|
|
color.rgb += surface.emission * _EmissionStrength;//自发光
|
|
color.rgb = MixFog(color.rgb, inputData.fogCoord);//混合雾效
|
|
return float4(color.rgb, 1);
|
|
}
|
|
ENDHLSL
|
|
}
|
|
Pass{
|
|
Name "ShadowCaster"
|
|
Tags {
|
|
"LightMode" = "ShadowCaster"//阴影投射
|
|
}
|
|
ZWrite On
|
|
ZTest LEqual
|
|
ColorMask 0
|
|
Cull Off
|
|
HLSLPROGRAM //着色器
|
|
#pragma multi_compile instancing
|
|
#pragma multi_compile DOTS INSTANCING ON
|
|
#pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW
|
|
|
|
#pragma vertex ShadowVS
|
|
#pragma fragment ShadowFS
|
|
|
|
float3 _LightDirection;
|
|
float3 _LightPosition;
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float3 normalOS : NORMAL;
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float4 positionCS : SV_POSITION;
|
|
};
|
|
//将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置
|
|
float4 GetShadowPositionHClip(Attributes input){
|
|
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
|
|
float3 normalWS = TransformObjectToWorldDir(input.normalOS);
|
|
#if _CASTING_PUNCTUAL_LIGHT_SHADOW
|
|
float3 lightDirectionWS = normalize(_LightPosition - positionWS);
|
|
#else
|
|
float3 lightDirectionWS = _LightDirection;
|
|
#endif
|
|
|
|
float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
|
|
|
|
#if UNITY_REVERSED_Z
|
|
positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE);
|
|
#else
|
|
positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE);
|
|
#endif
|
|
return positionCS;
|
|
}
|
|
Varyings ShadowVS(Attributes input){
|
|
Varyings output;
|
|
output.positionCS = GetShadowPositionHClip(input);
|
|
return output;
|
|
}
|
|
half4 ShadowFS(Varyings input):SV_TARGET{
|
|
return 0;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|