- Home /
Is there a way to give a random UV to a particle trail shader?
Is there a way to give a random UV to a particle trail shader? Even if you make a random value with a custom vertex stream, trail does not inherit it. So far all the trail smoke has been collected, It doesn't help at all. Here is a similar question, but world position is meaningless. https://realtimevfx.com/t/random-value-for-particle-system-trails-material-in-unity/11183 I believe there is a simple solution.
Answer by ifurkend · Jun 02, 2020 at 03:16 AM
I am not quite sure what "WP" stands for in that RealTimeFX thread. But if you are willing to sacrifice one channel in vertex color for sending the initial randomized range to your particle trail material, the shader can be very straightforward like this.
Still you will need to create a separate uv for the alpha, otherwise the transparency of the trail texture will offset as well, undesirably. In this example shader I just do tex2D twice with different sets of uv, but you may also use the un-offset uv (xy) to calculate your texture transparency along the trail with functions like smoothstep, either in the vertex shader (faster) or fragment shader (slower), depending on how you weigh between performance and rendering fidelity.
Shader "Particles/Trail Random UV Offset by Vertex Color"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" "PreviewType"="Plane"}
LOD 100
Lighting Off
ZWrite Off
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float color : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.zw = o.uv.xy + v.color.rg; //Assuming you offset final uv with vertex color red and green.
return o;
o.color.r = v.color.a;
}
float4 frag (v2f i) : SV_Target
{
float4 col = tex2D(_MainTex, i.uv.xy);
float4 colOffset = tex2D(_MainTex, i.uv.zw);
return float4(colOffset.rgb, col.a * i.color.r);
}
ENDCG
}
}
}
Answer by kkrg001 · Jun 02, 2020 at 04:20 AM
Thank you for your polite sample. As you said, I think randomization is possible by discarding one vertex color. But in most cases it seems difficult. I think this problem existed 10 years ago when I first touched unity. It hasn't been resolved yet...
WP is probably the world position.
I won't call the lack of UV randomization for particle trails an "issue", the randomization itself would cause more unexpected issues. As for a bunch of plain smoke trails as shown in your screenshot, you hardly need an array of rainbow colors for all trails, so the lack of ability to change trail colors from the particle system with this shader does not have that much of impact. At least this is how I see in many commerical games with lot of gun smoke trails flying in the same scene.
If you want to change the color of the trails in the engine, you can do it with an additional material property of _TintColor (and even use the randomized vertex color to randomize the final color, not just the UV). Indeed you will need a new material for each color of trail material.
Your answer
![](https://koobas.hobune.stream/wayback/20220612235901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Emit Random Particles? 2 Answers
Unity 2D Particle System Interaction. 0 Answers
Glowing Particle 3 Answers
Finding Particle Information Upon Collision 2 Answers
Particle System not working. 1 Answer