初学shader,想做一个受攻击闪白的功能
unity3d吧
全部回复
仅看楼主
level 1
迟来的雪 楼主
网上找了一个代码,发现使用后虽然可以闪白,但是无法通过Sprite Renderer给精灵叠加颜色了,求大佬解惑
Shader "Custom/BeAttackWhite"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("add color", Color) = (1,1,1,1)
_BeAttack("BeAttack",Int)=0
}
SubShader
{
Tags { "QUEUE" = "Transparent" "IGNOREPROJECTOR" = "true" "RenderType" = "Transparent"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
half4 vertex:POSITION;
float2 texcoord:TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f
{
half4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
int _BeAttack;//对外参数表示是否被攻击了
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_TARGET
{
fixed4 tex = tex2D(_MainTex, i.uv);
if (_BeAttack == 1) {//是否被攻击
if (tex.rgba.a > 0)
{
tex.rgb = tex.rgb + _Color.rgb;
}
}
return tex;
}
ENDCG
}
}
}
2025年02月24日 13点02分 1
level 9
tex.rgb = tex.rgb + _Color.rgb;这样写之后color是(1,1,1,1),得到的就是白光,丢失了tex颜色
试试 tex.rgb = tex.rgb * 0.5 + _Color.rgb * 0.5;
https://blog.csdn.net/perfect2011/article/details/139271387
2025年02月27日 02点02分 2
多谢,已发现问题在于没有把顶点颜色参与计算
2025年02月27日 03点02分
1