Skip to content

Commit

Permalink
Fix force fields of GpuParticles
Browse files Browse the repository at this point in the history
  • Loading branch information
ueshita committed Feb 27, 2024
1 parent 627b0f3 commit 9a55c0b
Show file tree
Hide file tree
Showing 33 changed files with 7,896 additions and 7,633 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ struct ParticleData
uint Seed;
float LifeAge;
uint InheritColor;
uint2 DirectionSpeed;
uint Color;
uint Padding;
uint Direction;
uint2 Velocity;
float4x3 Transform;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void main(uint3 dtid : SV_DispatchThreadID)
particle.LifeAge = 0.0f;
particle.InheritColor = 0;
particle.Color = 0;
particle.Padding = 0;
particle.DirectionSpeed = PackFloat4(float4(0.0f, 0.0f, 0.0f, 0.0f));
particle.Direction = 0;
particle.Velocity = uint2(0, 0);
particle.Transform = float4x3(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);

Particles[particleID] = particle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void transformSprite(ParticleData particle, inout float3 position)
else if (paramData.ShapeData == 1) {
// DirectionalBillboard
float3 R, U, F;
U = UnpackFloat4(particle.DirectionSpeed).xyz;
U = normalize(UnpackNormalizedFloat3(particle.Direction));
F = constants.CameraFront;
R = normalize(cross(U, F));
U = normalize(cross(F, R));
Expand Down Expand Up @@ -84,7 +84,7 @@ void transformTrail(ParticleData particle, inout float3 position, inout float2 u
uint segmentID = min(vertexID / 2, trailLength);
if (segmentID == 0) {
trailPosition = particle.Transform[3];
trailDirection = normalize(UnpackFloat4(particle.DirectionSpeed).xyz);
trailDirection = normalize(UnpackNormalizedFloat3(particle.Direction));
}
else {
uint trailID = emitter.TrailHead + instanceID * paramData.ShapeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ void main(uint3 dtid : SV_DispatchThreadID)
particle.InheritColor = emitter.Color;
}
particle.Color = 0xFFFFFFFF;
particle.Padding = 0;

particle.Transform = TRSMatrix(position, float3(0.0, 0.0, 0.0), float3(1.0, 1.0, 1.0));
particle.DirectionSpeed = PackFloat4(float4(direction, speed));
particle.Direction = PackNormalizedFloat3(direction);
particle.Velocity = PackFloat4(float4(direction * speed, 0.0f));
Particles[particleID] = particle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ void main(uint3 dtid : SV_DispatchThreadID)
float4 angularVelocity = RandomFloat4Range(seed, paramData.AngularVelocity);

float3 position = particle.Transform[3];
float4 directionSpeed = UnpackFloat4(particle.DirectionSpeed);
float3 velocity = directionSpeed.xyz * directionSpeed.w;
float3 lastPosition = position;
float3 direction = normalize(UnpackNormalizedFloat3(particle.Direction));
float3 velocity = UnpackFloat4(particle.Velocity).xyz;

if (emitter.TrailSize > 0) {
uint trailID = emitter.TrailHead + dtid.x * paramData.ShapeData + emitter.TrailPhase;
TrailData trail;
trail.Position = position;
trail.Direction = PackNormalizedFloat3(directionSpeed.xyz);
trail.Direction = PackNormalizedFloat3(direction);
Trails[trailID] = trail;
}

Expand All @@ -79,18 +80,6 @@ void main(uint3 dtid : SV_DispatchThreadID)
// Gravity
velocity += paramData.Gravity * deltaTime;

// Vortex
if (paramData.VortexRotation != 0.0f || paramData.VortexAttraction != 0.0f) {
velocity += Vortex(paramData.VortexRotation, paramData.VortexAttraction,
paramData.VortexCenter, paramData.VortexAxis,
position, emitter.Transform) * deltaTime;
}
// Turbulence
if (paramData.TurbulencePower != 0.0f) {
float4 vfTexel = NoiseTex.SampleLevel(NoiseSamp, position * paramData.TurbulenceScale + 0.5f, 0);
velocity += (vfTexel.xyz * 2.0f - 1.0f) * paramData.TurbulencePower * deltaTime;
}

// Damping
float speed = length(velocity);
if (speed > 0.0f) {
Expand All @@ -101,10 +90,22 @@ void main(uint3 dtid : SV_DispatchThreadID)
// Move from velocity
position += velocity * deltaTime;

// Recalc direction and speed from velocity
directionSpeed.w = length(velocity);
if (directionSpeed.w > 0.0001f) {
directionSpeed.xyz = normalize(velocity);
// Vortex
if (paramData.VortexRotation != 0.0f || paramData.VortexAttraction != 0.0f) {
position += Vortex(paramData.VortexRotation, paramData.VortexAttraction,
paramData.VortexCenter, paramData.VortexAxis,
position, emitter.Transform) * deltaTime;
}
// Turbulence
if (paramData.TurbulencePower != 0.0f) {
float4 vfTexel = NoiseTex.SampleLevel(NoiseSamp, position * paramData.TurbulenceScale * 0.125f + 0.5f, 0);
position += (vfTexel.xyz * 2.0f - 1.0f) * paramData.TurbulencePower * deltaTime;
}

// Calc direction
float3 diff = position - lastPosition;
if (length(diff) > 0.0001f) {
direction = normalize(diff);
}

// Rotation (Euler)
Expand Down Expand Up @@ -154,7 +155,8 @@ void main(uint3 dtid : SV_DispatchThreadID)
color.a *= clamp((lifeTime - particle.LifeAge) / paramData.FadeOut, 0.0, 1.0);

particle.Transform = TRSMatrix(position, rotation, scale.xyz * scale.w * paramData.ShapeSize);
particle.DirectionSpeed = PackFloat4(directionSpeed);
particle.Velocity = PackFloat4(float4(velocity, 0.0));
particle.Direction = PackNormalizedFloat3(direction);
particle.Color = PackColor(color);
Particles[particleID] = particle;
}
Expand Down
Loading

0 comments on commit 9a55c0b

Please sign in to comment.