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; } 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; } 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 = 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 } 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 } } }