Shader "Custom/AdvancedFresnelTextureColor" { Properties { [Header(Texture Settings)] [Toggle(_TEXTURE_ENABLED)] _UseTexture("Use Texture", Float) = 0 _FresnelTex("Fresnel Texture", 2D) = "white" {} _TextureIntensity("Texture Intensity", Range(0, 5)) = 1 [HDR] _TextureColor("Texture Color", Color) = (1,1,1,1) [Header(Texture Blend Mode)] [Enum(Multiply,0,Add,1,Overlay,2)] _BlendMode("Blend Mode", Float) = 0 [Header(Fresnel Settings)] [HDR] _FresnelColor("Fresnel Color", Color) = (1,0.5,0,1) _FresnelPower("Fresnel Power", Range(0.1, 10)) = 5 _FresnelIntensity("Fresnel Intensity", Range(0, 5)) = 1 _FresnelBias("Fresnel Bias", Range(0, 1)) = 0.1 [Header(Rendering Settings)] [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 2 [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend", Float) = 1 [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend", Float) = 1 } SubShader { Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" } LOD 300 Pass { Name "ForwardLit" Tags { "LightMode" = "UniversalForward" } Blend [_SrcBlend] [_DstBlend] ZWrite Off Cull [_Cull] HLSLPROGRAM #pragma vertex vert #pragma fragment frag #pragma shader_feature _TEXTURE_ENABLED #pragma multi_compile_fog #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" struct Attributes { float4 positionOS : POSITION; float3 normalOS : NORMAL; float2 texcoord : TEXCOORD0; }; struct Varyings { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; float3 normalWS : TEXCOORD1; float3 viewDirWS : TEXCOORD2; float fogCoord : TEXCOORD3; }; // 材质属性 CBUFFER_START(UnityPerMaterial) // 贴图属性 float4 _FresnelTex_ST; float _TextureIntensity; float4 _TextureColor; float _BlendMode; // 菲尼尔属性 float4 _FresnelColor; float _FresnelPower; float _FresnelIntensity; float _FresnelBias; CBUFFER_END TEXTURE2D(_FresnelTex); SAMPLER(sampler_FresnelTex); // 叠加混合模式 half3 BlendOverlay(half3 base, half3 blend) { return half3( (base.r < 0.5) ? (2.0 * base.r * blend.r) : (1.0 - 2.0 * (1.0 - base.r) * (1.0 - blend.r)), (base.g < 0.5) ? (2.0 * base.g * blend.g) : (1.0 - 2.0 * (1.0 - base.g) * (1.0 - blend.g)), (base.b < 0.5) ? (2.0 * base.b * blend.b) : (1.0 - 2.0 * (1.0 - base.b) * (1.0 - blend.b)) ); } float CalculateFresnel(float3 normal, float3 viewDir, float power, float bias, float intensity) { float NdotV = saturate(dot(normalize(normal), normalize(viewDir))); float fresnel = pow(1.0 - NdotV, power); fresnel = saturate(bias + (1.0 - bias) * fresnel); return fresnel * intensity; } Varyings vert(Attributes input) { Varyings output; VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz); VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS); output.positionHCS = vertexInput.positionCS; output.uv = TRANSFORM_TEX(input.texcoord, _FresnelTex); output.normalWS = normalInput.normalWS; output.viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS); output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z); return output; } half4 frag(Varyings input) : SV_Target { // 计算菲尼尔效果 float fresnel = CalculateFresnel(input.normalWS, input.viewDirWS, _FresnelPower, _FresnelBias, _FresnelIntensity); half4 finalColor = _FresnelColor * fresnel; // 贴图效果 #ifdef _TEXTURE_ENABLED half4 textureColor = SAMPLE_TEXTURE2D(_FresnelTex, sampler_FresnelTex, input.uv); textureColor *= _TextureColor * _TextureIntensity; // 根据混合模式混合 if (_BlendMode == 0) // Multiply { finalColor.rgb *= textureColor.rgb; } else if (_BlendMode == 1) // Add { finalColor.rgb += textureColor.rgb * fresnel; } else if (_BlendMode == 2) // Overlay { finalColor.rgb = BlendOverlay(finalColor.rgb, textureColor.rgb); } finalColor.a *= textureColor.a; #endif // 应用雾效 finalColor.rgb = MixFog(finalColor.rgb, input.fogCoord); return finalColor; } ENDHLSL } } FallBack "Universal Render Pipeline/Unlit" }