paofan-讯岚社区
paofan的头像-讯岚社区
浙江超级版主
这家伙很懒,什么都没有写...

6天前
我已经用shader实现了正片叠底的效果,但是柔光的脚本一丢进去就报错。 Shader "Custom/SceneMultiplyBlend" { Properties { _MultiplyTex ("正片叠底图层", 2D) = "white" {} _MultiplyOpacity ("正片叠底不透明度", Range(0,1)) = 1.0 _MultiplyStrength ("正片叠底强度", Range(0,2)) = 1.0 _MultiplyScale ("正片叠底缩放", Range(0.1,2)) = 1 _MultiplyOffset ("正片叠底位置", Vector) = (0,0,0,0) } SubShader { Tags { "RenderType"="Transparent" "Queue"="Overlay" "IgnoreProjector"="True" } Blend DstColor Zero Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float4 color : COLOR; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; float4 color : COLOR; }; sampler2D _MultiplyTex; float4 _MultiplyTex_ST; float _MultiplyOpacity; float _MultiplyStrength; float _MultiplyScale; float4 _MultiplyOffset; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MultiplyTex); o.color = v.color; return o; } fixed4 frag (v2f i) : SV_Target { float2 multiplyUV = (i.uv - 0.5) * _MultiplyScale + 0.5 + _MultiplyOffset.xy; multiplyUV = saturate(multiplyUV); fixed4 multiplyColor = tex2D(_MultiplyTex, multiplyUV); fixed4 finalColor = fixed4(1, 1, 1, 1); finalColor.rgb = lerp(finalColor.rgb, multiplyColor.rgb * _MultiplyStrength, multiplyColor.a * _MultiplyOpacity); return finalColor * i.color; } ENDCG } } } 我想在unity中模仿ps一样的效果 这个是柔光的脚本 Shader "Custom/SceneSoftLightBlend" { Properties { _MainTex ("主纹理", 2D) = "white" {} _BlendTex ("柔光纹理", 2D) = "white" {} _BlendAmount ("柔光强度", Range(0,1)) = 1 _Opacity ("不透明度", Range(0,1)) = 1 } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True" } Cull Off Lighting Off ZWrite Off Blend One OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float2 texcoord : TEXCOORD0; }; sampler2D _MainTex; sampler2D _BlendTex; float4 _MainTex_ST; float4 _BlendTex_ST; fixed _BlendAmount; fixed _Opacity; // 柔光混合函数 fixed3 SoftLight(fixed3 base, fixed3 blend) { fixed3 result; UNITY_UNROLL for(int i = 0; i < 3; i++) { if(blend[i] <= 0.5) { result[i] = base[i] - (1 - 2 * blend[i]) * base[i] * (1 - base[i]); } else { fixed D = (base[i] <= 0.25) ? ((16 * base[i] - 12) * base[i] + 4) * base[i] : sqrt(base[i]); result[i] = base[i] + (2 * blend[i] - 1) * (D - base[i]); } } return result; } v2f vert(appdata_t IN) { v2f OUT; OUT.vertex = UnityObjectToClipPos(IN.vertex); OUT.texcoord = TRANSFORM_TEX(IN.texcoord, _MainTex); OUT.color = IN.color; return OUT; } fixed4 frag(v2f IN) : SV_Target { // 采样纹理 fixed4 mainColor = tex2D(_MainTex, IN.texcoord) * IN.color; fixed4 blendColor = tex2D(_BlendTex, IN.texcoord); // 应用柔光混合 fixed3 softLightColor = SoftLight(mainColor.rgb, blendColor.rgb); // 在原始颜色和柔光结果之间插值 fixed3 finalColor = lerp(mainColor.rgb, softLightColor, _BlendAmount * blendColor.a); // 处理透明度 fixed alpha = mainColor.a * _Opacity; // 预乘alpha finalColor *= alpha; return fixed4(finalColor, alpha); } ENDCG } } } 写不出来,这个柔光不能用的,进去以后底是白的,应该是希望一张图进去就能和背景计算,就像在ps里改图层效果变成柔光一样,问ai改了也还是这样。 不知道怎么用shader把一张图和它的背景图计算。