Shader "Spaventacorvi/Glitter/Glitter F - Bumped Specular" { Properties { _Color ("Main color", Color) = (0.7058823,0.7058823,0.7058823,1) [MainTexture] _BaseMap ("Base (RGB)", 2D) = "white" {} // 新增:饱和度控制开关(0=黑白,1=原色彩) _Saturation ("Saturation (0=Grayscale, 1=Color)", Range(0, 1)) = 1.0 // 核心修改:用Color参数替代三个Range参数,Alpha通道无意义(设为1) // 标准ITU-R BT.709灰度公式 float gray = dot(rgb, float3(0.2126, 0.7152, 0.0722)); _GrayWeights ("Grayscale Weights (R/G/B)", Color) = (0.2126, 0.7152, 0.0722, 1.0) wvwvww ("Shininess", Range(0, 1)) = 0.2 vvxwwx ("Fake light", Range(0, 0.1)) = 0.05 vwwvvx ("Reflection cubemap", Cube) = "_Skybox" {} xxwxww ("Reflection cubemap power", Range(0, 2)) = 0.5 xxwxwx ("Cubemap blur (0 - 5)", Range(0, 5)) = 0 _BumpMap ("Normalmap", 2D) = "bump" {} vwxwww ("Specular glitter", 2D) = "white" {} vxvvvw ("Specular power (0 - 5)", Range(0, 5)) = 1.5 wxxxvv ("Specular contrast (1 - 3)", Range(1, 3)) = 1 vvxxww ("Glitter map", 2D) = "white" {} vwxvww ("Glitter color", Color) = (1,1,1,1) vwwxwx ("Glitter power (0 - 10)", Range(0, 10)) = 2 vxwwww ("Glitter contrast (1 - 3)", Range(1, 3)) = 1.5 xvwvwx ("Glittery speed (0 - 1)", Range(0, 1)) = 0.5 wvvxxw ("Glittery & mask dots scale", Range(0.1, 8)) = 2.5 wvvxxv ("Mask", 2D) = "black" {} wvwwxv ("Mask adjust (0.5 - 1.5)", Range(0.5, 1.5)) = 1 [HideInInspector][NoScaleOffset]unity_Lightmaps("unity_Lightmaps", 2DArray) = "" {} [HideInInspector][NoScaleOffset]unity_LightmapsInd("unity_LightmapsInd", 2DArray) = "" {} [HideInInspector][NoScaleOffset]unity_ShadowMasks("unity_ShadowMasks", 2DArray) = "" {} } SubShader { Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "UniversalMaterialType" = "Lit" } Pass { Name "ForwardLit" Tags { "LightMode"="UniversalForward" } HLSLPROGRAM #pragma vertex vertex #pragma fragment fragment #pragma multi_compile_fwdbase_fullshadows #pragma multi_compile_fog #pragma exclude_renderers xbox360 ps3 #pragma target 3.0 #pragma glsl uniform float4 _LightColor0; uniform sampler2D wvvxxv; uniform float4 wvvxxv_ST; uniform float wvwvww; uniform sampler2D _BaseMap; uniform float4 _BaseMap_ST; uniform sampler2D vwxwww; uniform float4 vwxwww_ST; uniform float vxvvvw; uniform float vwwxwx; uniform float4 _Color; uniform samplerCUBE vwwvvx; uniform float xxwxww; uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST; uniform float vvxwwx; uniform float4 vwxvww; uniform float wvvxxw; uniform float xvwvwx; uniform sampler2D vvxxww; uniform float4 vvxxww_ST; uniform float vxwwww; uniform float wxxxvv; uniform float wvwwxv; uniform float xxwxwx; // 新增:声明饱和度变量 uniform float _Saturation; // 声明Color类型的灰度权重变量(float4) uniform float4 _GrayWeights; // Includes #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.universal/ShaderLibrary/ShaderGraphFunctions.hlsl" // 新增:RGB转灰度(黑白)的工具函数 float3 RGBToGrayscale(float3 rgb) { // 提取Color参数的R/G/B分量作为权重(忽略Alpha) float3 weights = _GrayWeights.rgb; // 可选:归一化权重,避免总和不为1导致亮度异常 float totalWeight = max(dot(weights, float3(1,1,1)), 0.0001); // 防止除0 weights = weights / totalWeight; // 计算灰度值 float gray = dot(rgb, weights); // 显式赋值给float3的三个分量,避免构造函数报错 return float3(gray, gray, gray); } // Structs struct Attributes { float4 vertex : POSITION; float4 normal : NORMAL; float4 tangent : TANGENT; float2 uv : TEXCOORD0; }; struct Varyings { float4 pos : SV_POSITION; float2 uv0 : TEXCOORD0; float3 posWorld : TEXCOORD1; half4 normalDir : TEXCOORD2; half4 tangentDir : TEXCOORD3; half4 bitangentDir : TEXCOORD4; half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light }; // void MainLightDirection_float(out float3 Direction) // { // #if SHADERGRAPH_PREVIEW // Direction = half3(-0.5, -0.5, 0); // #else // Direction = SHADERGRAPH_MAIN_LIGHT_DIRECTION(); // #endif // } // Vertex Shader Varyings vertex(Attributes IN) { Varyings OUT; VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.vertex.xyz); VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normal.xyz, IN.tangent); OUT.pos = positionInputs.positionCS; OUT.posWorld = positionInputs.positionWS; half3 viewDirWS = GetWorldSpaceViewDir(positionInputs.positionWS); half3 vertexLight = VertexLighting(positionInputs.positionWS, normalInputs.normalWS); OUT.normalDir = half4(normalInputs.normalWS, viewDirWS.x); OUT.tangentDir = half4(normalInputs.tangentWS, viewDirWS.y); OUT.bitangentDir = half4(normalInputs.bitangentWS, viewDirWS.z); OUT.fogFactorAndVertexLight = half4(0, vertexLight); OUT.uv0 = TRANSFORM_TEX(IN.uv, _BaseMap); return OUT; } // ========== 核心修复:适配所有URP版本的光源获取函数 ========== Light GetLightMeshRenderingLayer(float3 positionWS) { // 1. 兼容不同URP版本的主光源获取 Light mainLight; #if UNITY_URP_VERSION >= 110000 // URP 11.x+ 带参调用 mainLight = GetMainLight(positionWS); #else // URP 10.x及以下 无参调用 mainLight = GetMainLight(); #endif // 2. 检查主光源是否匹配当前渲染层 uint meshRenderingLayers = GetMeshRenderingLayer(); if (IsMatchingLightLayer(mainLight.layerMask, meshRenderingLayers)) { return mainLight; } // 3. 遍历附加光源,寻找匹配的光源 uint lightsCount = GetAdditionalLightsCount(); LIGHT_LOOP_BEGIN(lightsCount) Light additionalLight = GetAdditionalLight(lightIndex, positionWS); if (IsMatchingLightLayer(additionalLight.layerMask, meshRenderingLayers)) { return additionalLight; } LIGHT_LOOP_END // 4. 兜底:若没有匹配的光源,返回主光源(避免空值) return mainLight; } // Fragment Shader half4 fragment(Varyings i) : SV_Target { i.normalDir = normalize(i.normalDir); float3x3 tangentTransform = float3x3( i.tangentDir.xyz, i.bitangentDir.xyz, i.normalDir.xyz); float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz); float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap))); float3 normalLocal = _BumpMap_var.rgb; float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); float3 viewReflectDirection = reflect( -viewDirection, normalDirection ); // float3 lightDirection; //normalize(_WorldSpaceLightPos0.xyz); // MainLightDirection_float(lightDirection); Light light = GetLightMeshRenderingLayer(i.posWorld); float3 lightDirection = light.direction; float3 lightColor = light.color* light.distanceAttenuation; float3 halfDirection = normalize(viewDirection+lightDirection); float attenuation = 1; float3 attenColor = attenuation * lightColor; float specPow = exp2(wvwvww * 10.0+1.0); float NdotL = max(0, dot( normalDirection, lightDirection )); float xvxwww = 0.0; float2 vwwvxw = ((0.05*(xvwvwx - xvxwww)*mul(tangentTransform, viewDirection).xy + i.uv0).rg*((xvwvwx/2.0)+1.0)*wvvxxw); float4 wwvxvx = tex2D(vvxxww,TRANSFORM_TEX(vwwvxw, vvxxww)); float vxxxwx = 0.0; float vwvxwv_ang = 3.14; float vwvxwv_spd = 1.0; float vwvxwv_cos = cos(vwvxwv_spd*vwvxwv_ang); float vwvxwv_sin = sin(vwvxwv_spd*vwvxwv_ang); float2 vwvxwv_piv = float2(0.5,0.5); float2 vwvxwv = (mul((0.05*((-1*xvwvwx) - xvxwww)*mul(tangentTransform, viewDirection).xy + i.uv0).rg-vwvxwv_piv,float2x2( vwvxwv_cos, -vwvxwv_sin, vwvxwv_sin, vwvxwv_cos))+vwvxwv_piv); float2 wvxwxw = (vwvxwv*wvvxxw*(1.0-(xvwvwx/3.141592654))*wvwwxv); float4 xwvwvw = tex2D(vvxxww,TRANSFORM_TEX(wvxwxw, vvxxww)); float4 wvvxxv_var = tex2D(wvvxxv,TRANSFORM_TEX(i.uv0, wvvxxv)); float4 vwxwww_var = tex2D(vwxwww,TRANSFORM_TEX(i.uv0, vwxwww)); float3 wxvxxw = (lerp(pow(abs((vwwxwx*vwxvww.rgb)*wwvxvx.rgb),vxwwww),float3(vxxxwx,vxxxwx,vxxxwx),max((1.0 - xwvwvw.rgb),wvvxxv_var.rgb))+lerp(pow(abs(vwxwww_var.rgb*vxvvvw),wxxxvv),float3(vxxxwx,vxxxwx,vxxxwx),wvvxxv_var.rgb)); float3 specularColor = wxvxxw; float3 directSpecular = attenColor * pow(max(0,dot(halfDirection,normalDirection)),specPow)*specularColor; float3 specular = directSpecular; NdotL = max(0.0,dot( normalDirection, lightDirection )); float3 directDiffuse = max( 0.0, NdotL) * attenColor; float3 indirectDiffuse = float3(0,0,0); indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; float4 _BaseMap_var = tex2D(_BaseMap,TRANSFORM_TEX(i.uv0, _BaseMap)); float3 diffuseColor = _BaseMap_var.rgb; // 核心修改:饱和度控制(混合原色彩和灰度) float3 grayscaleColor = RGBToGrayscale(diffuseColor); diffuseColor = lerp(grayscaleColor, diffuseColor, _Saturation); diffuseColor = (diffuseColor*_Color.rgb); float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor; float3 emissive = ((wxvxxw*vvxwwx)+(texCUBElod(vwwvvx,float4(viewReflectDirection,xxwxwx)).rgb*xxwxww)); float3 finalColor = diffuse + specular + emissive; half4 finalRGBA = half4(finalColor,_BaseMap_var.a); clip(finalRGBA.a - 0.1); return finalRGBA; } ENDHLSL } } }