- Home /
Limit particle shader / discard from certain worldpos
Hi there, new to shader programming and struggeling to recreate a particle shader that does not go through walls. It's a looping visualisation of a circle that fades out reaching the end of its lifetime. If it reaches a wall I want it to discard every fragment that is beyond a certain x or y value, so beyond the wall (will do that with discard). My problem at the moment is that the shader I'm rebuilding on my own (to introduce properties to use discard) isn't looking like the one made with the built in unity shader and a sprite attached to it. Yellow: Built in "Particles Alpha Blended Shader" |Green: own shader based on an unlit shader.
As you can see, the edges of the green one aren't fading out as the one of the yellow one. I think the problem is that I don't get the current "color over lifetime" from the particle system as an input, but I don't know how I could get that.
My Code looks like this:
Shader "Unlit/TestShader"
{
Properties
{
_MainTex ("Base (RGB) Transparency (A)", 2D) = "" {}
}
SubShader
{
Tags { "RenderType"="TransparentCutOut" "Queue"="AlphaTest" }
Blend SrcAlpha OneMinusDstAlpha
AlphaToMask On
ZWrite Off
Cull off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 color : COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col * i.color;
}
ENDCG
}
}
}
Can someone point out to me what I'm doing wrong or how the solution could look like?
Have a nice Friday and take care.
Answer by WizzardH · May 10, 2021 at 01:12 PM
The problem has solved itself now. I've checked the shader code that Unity generated for the original shader and even though it has some confusing parts it one can still get the most basic out of it. So I replaced the subshader properties and now it works as beautifully as I want it to:
Properties
{
_TintColor ("Tint Color", Color) = (0.500000,0.500000,0.500000,0.500000)
_MainTex ("Particle Texture", 2D) = "white" { }
_InvFade ("Soft Particles Factor", Range(0.010000,3.000000)) = 1.000000
_LeftBoarder("Left", Float) = 1
_RightBoarder("Right", Float) = 1
_TopBoarder("Top", Float) = 1
_BottomBoarder("Bottom", Float) = 1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" "PreviewType"="Plane" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
ZWrite Off
Cull off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _TintColor;
float _LeftBoarder = 0;
float _RightBoarder = 0;
float _TopBoarder = 0;
float _BottomBoarder = 0;
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 color : COLOR;
float3 worldPos : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(UNITY_MATRIX_M, v.vertex.xyz); //object to world
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
if(i.worldPos.x < _LeftBoarder || i.worldPos.x > _RightBoarder)
return col * i.color * _TintColor;
return col * i.color;
}
ENDCG
}
}
Your answer
Follow this Question
Related Questions
Unity inbuilt distorsion shader? 1 Answer
My particles glow at night how can I stop the glowing 0 Answers
PS Custom Vertex Streams to Particle Trail 0 Answers
Shader tiling error 0 Answers
Particle system source code? 1 Answer