- Home /
Fade particle opacity according to distance from camera?
Hi All I'm working on iOS (base pro, though not iOS pro for the time being) - I want to ramp the opacity of particles down to zero as they approach camera. Is there a simple way to do this? It appears that soft particles don't work on iOS - - so this would be a pretty good workaround for my needs.
You could go about appropriating this shader to your needs, since it fades transparent materials out as they get closer to the camera.
Answer by mgc90403 · Jan 27, 2014 at 03:06 AM
Thank you for posting the link.
This appears to work great (after ferreting out a phantom typo on my part. ;)
This is a long shot but what was the typo? I can't get past the errors it's giving me : /
'vert': function return value missing semantics at line 50 (on d3d11_9x) 'frag': input parameter 'i' missing semantics at line 62 (on d3d11_9x)
Shader "Particles/Additive Clipsafe" {
Properties {
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_$$anonymous$$ainTex ("Particle Texture", 2D) = "white" {}
_FadeDistance ("Fade Start Distance", float) = 0.5
}
SubShader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha One
AlphaTest Greater .01
Color$$anonymous$$ask RGB
Lighting Off
ZWrite Off
Fog { Color (0,0,0,0) }
Pass {
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform float4 _$$anonymous$$ainTex_ST,
_TintColor;
uniform float _FadeDistance;
struct appdata_vert {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float4 color : COLOR;
};
uniform sampler2D _$$anonymous$$ainTex;
struct v2f {
V2F_POS_FOG;
float2 uv;
float4 color;
};
v2f vert (appdata_vert v) {
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
float4 viewPos = mul(glstate.matrix.modelview[0], v.vertex);
float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
alpha = $$anonymous$$(alpha, 1);
o.color = float4(v.color.rgb, v.color.a*alpha);
o.color *= _TintColor*2;
return o;
}
float4 frag (v2f i) : COLOR {
half4 texcol = tex2D( _$$anonymous$$ainTex, i.uv );
return texcol*i.color;
}
ENDCG
}
}
Fallback "Particles/Additive"
}
Hi o/ I just found this post and thought.. because I was in need for this kind of shader myself, of course i'll revive this two year old post to comment on your problem. The shader on the wiki is not compatible with Unity 5 (which I assume you used), so i've made the changes required to make it work.
Enjoy.
Shader "Particles/Additive Clipsafe" {
Properties {
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_$$anonymous$$ainTex ("Particle Texture", 2D) = "white" {}
_FadeDistance ("Fade Start Distance", float) = 0.5
}
SubShader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha One
AlphaTest Greater .01
Color$$anonymous$$ask RGB
Lighting Off
ZWrite Off
Fog { Color (0,0,0,0) }
Pass {
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform float4 _$$anonymous$$ainTex_ST,
_TintColor;
uniform float _FadeDistance;
struct appdata_vert {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float4 color : COLOR;
};
uniform sampler2D _$$anonymous$$ainTex;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
v2f vert (appdata_vert v) {
v2f o;
o.pos = mul (UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
float4 viewPos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$V, v.vertex);
float alpha = (-viewPos.z - _ProjectionParams.y)/_FadeDistance;
alpha = $$anonymous$$(alpha, 1);
o.color = float4(v.color.rgb, v.color.a*alpha);
o.color *= _TintColor*2;
return o;
}
float4 frag (v2f i) : COLOR {
half4 texcol = tex2D( _$$anonymous$$ainTex, i.uv );
return texcol*i.color;
}
ENDCG
}
}
Fallback "Particles/Additive"
}
Your answer
Follow this Question
Related Questions
Character with hair (mobile) 0 Answers
Particle Animator Opacity 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Problem rendering particles in ios 1 Answer
iOS Game freeze, when particles emitted for first time. 2 Answers