Skip to content

Commit

Permalink
particle fading near camera
Browse files Browse the repository at this point in the history
  • Loading branch information
LGhassen committed Aug 26, 2020
1 parent 932209b commit 8fc0f7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 10 additions & 6 deletions Assets/Shaders/GeometryCloudVolumeParticle.shader
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ Shader "EVE/GeometryCloudVolumeParticle" {

sampler2D _CameraDepthTexture;

// float4x4 _CameraToWorld;

struct appdata_t {
float4 vertex : POSITION;
fixed4 color : COLOR;
Expand All @@ -101,6 +99,7 @@ Shader "EVE/GeometryCloudVolumeParticle" {
float4 origin : TEXCOORD2;
float viewDirFade : TEXCOORD3;
float3 planetPos : TEXCOORD4;
float particleFade: TEXCOORD5;
};

//vertex function, called for every point on our hexSeg, each vertex corresponding to the origin of a particle/quad
Expand Down Expand Up @@ -148,6 +147,9 @@ Shader "EVE/GeometryCloudVolumeParticle" {
o.pos=mvCenter;
o.pos = o.color.a > (1.0/255.0) ? o.pos : float4(2.0, 2.0, 2.0, 1.0); //cull vertex if low alpha, pos outside clipspace

float fadeOut = (-mvCenter.z/mvCenter.w) * 0.004;
o.particleFade = smoothstep(0.0,1.0,fadeOut);

return o;
}

Expand All @@ -160,7 +162,7 @@ Shader "EVE/GeometryCloudVolumeParticle" {
float2 texcoordZY : TEXCOORD1;
float2 texcoordXZ : TEXCOORD2;
float2 texcoordXY : TEXCOORD3;
float2 uv : TEXCOORD4;
float3 uv : TEXCOORD4; //x and y UVs, z is particleFade
float4 projPos : TEXCOORD5;
float3 planetPos : TEXCOORD6;
float3 viewDirT : TEXCOORD7;
Expand Down Expand Up @@ -237,7 +239,7 @@ Shader "EVE/GeometryCloudVolumeParticle" {
tri.lightDirT = normalize(mul(rotation, _WorldSpaceLightPos0.xyz));
tri.viewDirT = normalize(mul(rotation, viewDir));

tri.uv = vertexUV; //quad UV
tri.uv = float3(vertexUV, originPoint.particleFade); //x and y quad UV, z particleFade
tri.localOrigin = originPoint.localOrigin;

return tri;
Expand Down Expand Up @@ -305,7 +307,7 @@ Shader "EVE/GeometryCloudVolumeParticle" {

//half3 normT = UnpackNormal(tex2D(_BumpMap, IN.uv));
half3 normT;
normT.xy = ((2*IN.uv)-1);
normT.xy = ((2*IN.uv.xy)-1);
normT.z = sqrt(1 - saturate(dot(normT.xy, normT.xy)));
//normT.xy = 2 * INV_PI*asin((2 * IN.uv) - 1) ;
//normT.xy = sin(PI*(IN.uv-.5));
Expand All @@ -318,10 +320,12 @@ Shader "EVE/GeometryCloudVolumeParticle" {
float depth = UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)));
depth = LinearEyeDepth (depth);
float partZ = IN.projPos.z;
float fade = saturate (_InvFade * (depth-partZ));
float fade = depth >= (0.99 * _ProjectionParams.z) ? 1.0 : saturate (_InvFade * (depth-partZ)); //fade near objects but don't fade on far plane (max depth value)
color.a *= fade;
#endif

color.a *= IN.uv.z; //particle fade as they approach camera

return color;
}
ENDCG
Expand Down
14 changes: 10 additions & 4 deletions Assets/Shaders/GeometryCloudVolumeParticleToTexture.shader
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {
float4 origin : TEXCOORD2;
float viewDirFade : TEXCOORD3;
float3 planetPos : TEXCOORD4;
float particleFade: TEXCOORD5;
};

//vertex function, called for every point on our hexSeg, each vertex corresponding to the origin of a particle/quad
Expand Down Expand Up @@ -147,6 +148,9 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {
o.pos=mvCenter;
o.pos = o.color.a > (1.0/255.0) ? o.pos : float4(2.0, 2.0, 2.0, 1.0); //cull vertex if low alpha, pos outside clipspace

float fadeOut = (-mvCenter.z/mvCenter.w) * 0.004;
o.particleFade = smoothstep(0.0,1.0,fadeOut);

return o;
}

Expand All @@ -159,7 +163,7 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {
float2 texcoordZY : TEXCOORD1;
float2 texcoordXZ : TEXCOORD2;
float2 texcoordXY : TEXCOORD3;
float2 uv : TEXCOORD4;
float3 uv : TEXCOORD4; //x and y UVs, z is particleFade
float4 projPos : TEXCOORD5;
float3 planetPos : TEXCOORD6;
float3 viewDirT : TEXCOORD7;
Expand Down Expand Up @@ -236,7 +240,7 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {
tri.lightDirT = normalize(mul(rotation, _WorldSpaceLightPos0.xyz));
tri.viewDirT = normalize(mul(rotation, viewDir));

tri.uv = vertexUV; //quad UV
tri.uv = float3(vertexUV, originPoint.particleFade); //x and y quad UV, z particleFade
tri.localOrigin = originPoint.localOrigin;

return tri;
Expand Down Expand Up @@ -304,7 +308,7 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {

//half3 normT = UnpackNormal(tex2D(_BumpMap, IN.uv));
half3 normT;
normT.xy = ((2*IN.uv)-1);
normT.xy = ((2*IN.uv.xy)-1);
normT.z = sqrt(1 - saturate(dot(normT.xy, normT.xy)));
//normT.xy = 2 * INV_PI*asin((2 * IN.uv) - 1) ;
//normT.xy = sin(PI*(IN.uv-.5));
Expand All @@ -318,10 +322,12 @@ Shader "EVE/GeometryCloudVolumeParticleToTexture" {
float depth = UNITY_SAMPLE_DEPTH(tex2Dlod(EVEDownscaledDepth, float4(IN.projPos.xy/IN.projPos.w,0.0,0.0))); //to be sure we are reading a single point/using point filtering
depth = LinearEyeDepth (depth);
float partZ = IN.projPos.z;
float fade = saturate (_InvFade * (depth-partZ));
float fade = depth >= (0.99 * _ProjectionParams.z) ? 1.0 : saturate (_InvFade * (depth-partZ)); //fade near objects but don't fade on far plane (max depth value)
color.a *= fade;
#endif

color.a *= IN.uv.z; //particle fade as they approach camera

return color;
}
ENDCG
Expand Down

0 comments on commit 8fc0f7a

Please sign in to comment.