Skip to content

Commit

Permalink
Add rotation when spawned of GpuParticles
Browse files Browse the repository at this point in the history
  • Loading branch information
ueshita committed Feb 15, 2024
1 parent 84a8fbc commit 27557dc
Show file tree
Hide file tree
Showing 58 changed files with 6,317 additions and 5,901 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ GpuParticles::ParamSet LoadGpuParticlesParameter(uint8_t*& pos, int32_t version)
paramSet.Basic.LifeTime = Read<std::array<float, 2>>(pos);

paramSet.EmitShape.Type = (GpuParticles::EmitShape)Read<uint8_t>(pos);
paramSet.EmitShape.RotationApplied = Read<uint8_t>(pos) != 0;
switch (paramSet.EmitShape.Type)
{
case GpuParticles::EmitShape::Point:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class GpuParticles : public ReferenceObject
};

EmitShape Type;
bool RotationApplied;
union
{
std::array<float4, 2> Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ GpuParticles::ParameterData GpuParticles::ToParamData(const ParamSet& paramSet)
ParameterData data{};
data.LifeTime = paramSet.Basic.LifeTime;
data.EmitShapeType = (uint)paramSet.EmitShape.Type;
data.EmitRotationApplied = (uint)paramSet.EmitShape.RotationApplied;

data.EmitShapeData[0] = paramSet.EmitShape.Data[0];
data.EmitShapeData[1] = paramSet.EmitShape.Data[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct ParameterData

float2 LifeTime;
uint EmitShapeType;
uint Padding1;
uint EmitRotationApplied;

float4 EmitShapeData[2];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,32 @@ void main(uint3 dtid : SV_DispatchThreadID)
float3 circleAxis = mul(float4(paramData.EmitShapeData[0].xyz, 0.0f), emitter.Transform).xyz;
float circleInner = paramData.EmitShapeData[1].x;
float circleOuter = paramData.EmitShapeData[1].y;
position += RandomCircle(seed, circleAxis, circleInner, circleOuter);
float circleRadius = sqrt(lerp(circleInner * circleInner, circleOuter * circleOuter, RandomFloat(seed)));
float3 circleDirection = RandomCircle(seed, circleAxis);
position += circleDirection * circleRadius;
if (paramData.EmitRotationApplied) {
direction = mul(direction, float3x3(cross(circleAxis, circleDirection), circleAxis, circleDirection));
}
} else if (paramData.EmitShapeType == 3) {
float sphereRadius = paramData.EmitShapeData[0].x;
position += RandomDirection(seed) * sphereRadius;
float3 sphereDirection = RandomDirection(seed);
position += sphereDirection * sphereRadius;
if (paramData.EmitRotationApplied) {
float3 sphereUp = float3(0.0f, 1.0f, 0.0f);
direction = mul(direction, float3x3(cross(sphereUp, sphereDirection), sphereUp, sphereDirection));
}
} else if (paramData.EmitShapeType == 4) {
float modelSize = paramData.EmitShapeData[0].y;
if (emitter.EmitPointCount > 0) {
uint emitIndex = RandomUint(seed) % emitter.EmitPointCount;
EmitPoint emitPoint = EmitPoints[emitIndex];
position += mul(float4(emitPoint.Position * modelSize, 0.0f), emitter.Transform).xyz;
float3 emitNormal = UnpackNormalizedFloat3(emitPoint.Normal);
float3 emitBinormal = UnpackNormalizedFloat3(emitPoint.Binormal);
float3 emitTangent = UnpackNormalizedFloat3(emitPoint.Tangent);
direction = mul(direction, float3x3(normalize(emitTangent), normalize(emitBinormal), normalize(emitNormal)));
if (paramData.EmitRotationApplied) {
float3 emitNormal = UnpackNormalizedFloat3(emitPoint.Normal);
float3 emitBinormal = UnpackNormalizedFloat3(emitPoint.Binormal);
float3 emitTangent = UnpackNormalizedFloat3(emitPoint.Tangent);
direction = mul(direction, float3x3(normalize(emitTangent), normalize(emitBinormal), normalize(emitNormal)));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,20 @@ float3 RandomDirection(inout uint seed)
return float3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
}

float3 RandomCircle(inout uint seed, float3 axis, float innerRadius, float outerRadius)
float3 RandomCircle(inout uint seed, float3 axis)
{
float theta = 2.0f * PI * RandomFloat(seed);
float3 randDir = float3(cos(theta), 0.0f, sin(theta));
float radius = sqrt(lerp(innerRadius * innerRadius, outerRadius * outerRadius, RandomFloat(seed)));
float3 direction = float3(cos(theta), 0.0f, sin(theta));

axis = normalize(axis);
if (abs(axis.y) != 1.0f) {
float3 up = float3(0.0f, 1.0f, 0.0f);
float3 right = normalize(cross(up, axis));
float3 front = cross(axis, right);
return mul(randDir, float3x3(right, axis, front)) * radius;
return mul(direction, float3x3(right, axis, front));
}
else {
return randDir * sign(axis.y) * radius;
return direction * sign(axis.y);
}
}

Expand Down
Loading

0 comments on commit 27557dc

Please sign in to comment.