To troubleshoot residual color artifacts when overriding particle colors via MaterialPropertyBlock in Unity, especially when source textures have baked-in hues, follow these steps:
1. Diagnose the Source of Artifacts
- Check the Texture:
If the particle texture has non-neutral baked colors (e.g., red/orange), overriding with blue via _Color or _TintColor will result in multiplicative blending (e.g., red × blue = purple).
- Quick Test: Replace the texture with a grayscale/white texture in the material. If artifacts disappear, the original texture is the root cause.
2. Modify the Shader to Neutralize Texture Influence
If you can’t edit the source texture, adjust the shader to cancel out baked-in hues. Example for a custom shader:
// In the fragment shader (e.g., Particles/OverrideTint):
fixed4 frag (v2f i) : SV_Target {
fixed4 texColor = tex2D(_MainTex, i.uv);
// Desaturate texture before applying tint
float luminance = dot(texColor.rgb, float3(0.3, 0.59, 0.11));
texColor.rgb = lerp(texColor.rgb, luminance, _Desaturation); // 0 = original, 1 = grayscale
// Apply tint
texColor.rgb *= _Color.rgb;
return texColor * i.color;
}
- Expose
_Desaturation (range 0–1) in the material or via MaterialPropertyBlock to dynamically reduce texture color influence.
3. Use Additive/Multiply Blend Modes Strategically
- Blend Mode:
- For Additive (
Blend SrcAlpha One), darken your tint (e.g., RGB (0, 0, 0.5)) to avoid oversaturation.
- For Alpha Blend (
Blend SrcAlpha OneMinusSrcAlpha), ensure your tint’s alpha doesn’t clash with the texture’s alpha channel.
4. Leverage Texture Alpha for Masking
If the texture’s alpha channel defines the shape (e.g., a flame), use it to mask out color regions:
texColor.rgb = _Color.rgb * texColor.a; // Ignore texture RGB, use alpha for shape
5. Override All Relevant Shader Properties
Some shaders use multiple properties (e.g., _MainTex, _EmissionColor). Ensure you’re overriding all color-related properties:
MaterialPropertyBlock props = new MaterialPropertyBlock();
particleRenderer.GetPropertyBlock(props);
props.SetColor("_Color", targetColor);
props.SetColor("_EmissionColor", targetColor); // If emissive
props.SetFloat("_Desaturation", 1.0f); // Custom property
particleRenderer.SetPropertyBlock(props);
6. Test with Debug Colors
Temporarily set the shader to ignore the texture and use a flat color:
// Replace:
// fixed4 texColor = tex2D(_MainTex, i.uv);
// With:
fixed4 texColor = fixed4(1, 1, 1, 1);
If artifacts vanish, the texture is the culprit. If not, check other shader logic (e.g., vertex colors).
7. Handle Vertex Colors
If the particle system uses Vertex Color modules, ensure they aren’t conflicting:
// Disable vertex color influence in the shader:
// In shader, replace:
// return texColor * i.color;
// With:
return texColor * _Color;
8. Use a Custom Shader for Full Control
Create a dedicated particle shader that prioritizes your tint over texture colors:
Shader "Custom/Particles/ForceTint" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_TextureInfluence ("Texture Influence", Range(0, 1)) = 0 // 0 = ignore texture
}
SubShader {
Tags { "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _Color;
float _TextureInfluence;
v2f vert (appdata v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target {
fixed4 texColor = tex2D(_MainTex, i.uv);
// Blend between pure tint and texture-influenced tint
fixed3 finalColor = lerp(_Color.rgb, texColor.rgb * _Color.rgb, _TextureInfluence);
return fixed4(finalColor, texColor.a * _Color.a);
}
ENDCG
}
}
}
9. Script-Based Desaturation
Dynamically desaturate the texture at runtime using a compute shader or Texture2D.Apply(), but this is performance-heavy and not recommended for mobile.
Final Tips
- Avoid Over-Tinting: Use darker tints (e.g.,
RGB (0, 0, 0.3)) to minimize clashes with residual texture hues.
- Profile in Linear Space: Ensure your project uses Linear Color Space (not Gamma) for accurate blending.
- Batch Tinted Particles: If using
MaterialPropertyBlock, ensure renderers are batched for performance.
By decoupling the texture’s color influence and prioritizing your tint via shader logic, you can eliminate residual artifacts while maintaining runtime flexibility.