161 lines
7.0 KiB
Plaintext
161 lines
7.0 KiB
Plaintext
Shader "BSDF/URPGradient"
|
|
{
|
|
Properties
|
|
{
|
|
_BaseMap("纹理贴图", 2D) = "white" {}
|
|
_BaseColor("基础颜色", Color) = (1, 1, 1, 1)
|
|
_Metallic("金属度", Range(0, 1)) = 0.0
|
|
_Smoothness("光滑度", Range(0, 1)) = 0
|
|
_EmissionColor("自发光颜色", Color) = (0, 0, 0, 1)
|
|
_LightMap("光照贴图", 2D) = "white" {}
|
|
_MaxBoundsY("最大边界", Float) = 1
|
|
_MinBoundsY("最小边界", Float) = 0
|
|
}
|
|
SubShader
|
|
{
|
|
Tags {
|
|
"RenderPipeline" = "UniversalPipeline"
|
|
"RenderType" = "Opaque"
|
|
}
|
|
HLSLINCLUDE
|
|
#pragma multi_compile _MAIN_LIGHT_SHADOWS //主光源阴影
|
|
#pragma multi_compile _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE//主光源级联阴影
|
|
#pragma multi_compile _MAIN_LIGHT_SHADOWS _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 _MaxBoundsY;
|
|
float _MinBoundsY;
|
|
float4 _Colors[2]; //定义一个固定大小的颜色数组
|
|
CBUFFER_END
|
|
|
|
TEXTURE2D(_BaseMap);
|
|
SAMPLER(sampler_BaseMap);
|
|
TEXTURE2D(_LightMap);
|
|
SAMPLER(sampler_LightMap);
|
|
ENDHLSL
|
|
Pass{
|
|
Name "UniversalForward"
|
|
Tags {
|
|
"LightMode" = "UniversalForward"//光照模型向前渲染
|
|
}
|
|
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;//烘焙全局光照
|
|
}; */
|
|
float3 GetBaseColor(float y)
|
|
{
|
|
y = (_MaxBoundsY - y) / (_MaxBoundsY - _MinBoundsY); //将y值归一化到0-1之间
|
|
float3 color = lerp(_Colors[0].rgb, _Colors[1].rgb, y); //根据归一化的y值在颜色数组中插值
|
|
return color;
|
|
}
|
|
SurfaceData GetSurfaceData(Varyings input){
|
|
float4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv0);
|
|
float3 baseColor = GetBaseColor(input.positionWS.y);
|
|
SurfaceData surface;
|
|
surface.albedo = baseMap.rgb * baseColor;//漫反射颜色
|
|
surface.specular =float3(1, 1, 1);//镜面反射颜色
|
|
surface.metallic = _Metallic;//金属度
|
|
surface.smoothness = _Smoothness;//光滑度
|
|
surface.normalTS = float3(0, 0, 1);//法线
|
|
surface.emission = _EmissionColor.rgb;//自发光
|
|
surface.alpha = baseMap.a * _BaseColor.a;//透明度
|
|
surface.occlusion = 1.0;//环境光遮蔽
|
|
surface.clearCoatMask = 0.0;//清漆遮罩
|
|
surface.clearCoatSmoothness = 0.0;//清漆光滑度
|
|
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);
|
|
data.bakedGI = SampleSH(data.normalWS); // 球谐函数采样环境光//float3(0.5, 0.5, 0.5)
|
|
return data;
|
|
}
|
|
|
|
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);
|
|
float4 color = UniversalFragmentPBR(inputData, surface);
|
|
color.rgb += surface.emission;//自发光
|
|
color.rgb = MixFog(color.rgb, inputData.fogCoord);//混合雾效
|
|
/* Light mainLight = GetMainLight();
|
|
float3 normal = normalize(input.normalWS);
|
|
float3 lightDir = normalize(mainLight.direction);
|
|
float NdotL = saturate(dot(normal, lightDir));
|
|
float lambert = 0.5 + 0.5 * NdotL;
|
|
lambert *=pow(0.5 + 0.5 * NdotL, 2);
|
|
float3 headUpDir = normalize(_HeadUp);
|
|
float3 headForwardDir = normalize(_HeadForward);
|
|
float3 headRightDir = normalize(_HeadRight);
|
|
float4 basemap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv0);
|
|
float3 baseColor = _BaseColor.rgb;
|
|
float3 finalColor = baseColor * basemap.rgb * lambert; */
|
|
return float4(color.rgb, 1);
|
|
}
|
|
ENDHLSL
|
|
}
|
|
|
|
}
|
|
}
|