Add SkParticleValue to allow further customization of curve behavior
All curves (and path affectors) are driven by an SkParticleValue. The
value can derive its value from the current defaults (age of particle
or effect), or explicitly choose the other one, a random value, or any
other particle value. Values can be range adjusted and support repeat,
clamp, and mirror tiling.
Also fixed some more issues related to resource path in the slide GUI.
Bug: skia:
Change-Id: I4755018d5b57ae2d5ec400d541055ca4fb542978
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/196760
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/modules/particles/src/SkParticleEffect.cpp b/modules/particles/src/SkParticleEffect.cpp
index d1f7b64..2b6a88e 100644
--- a/modules/particles/src/SkParticleEffect.cpp
+++ b/modules/particles/src/SkParticleEffect.cpp
@@ -64,8 +64,15 @@
this->setCapacity(fParams->fMaxCount);
}
+ float effectAge = static_cast<float>((now - fSpawnTime) / fParams->fEffectDuration);
+ effectAge = fLooping ? fmodf(effectAge, 1.0f) : SkTPin(effectAge, 0.0f, 1.0f);
+
SkParticleUpdateParams updateParams;
updateParams.fDeltaTime = deltaTime;
+ updateParams.fEffectAge = effectAge;
+
+ // During spawn, values that refer to kAge_Source get the *effect* age
+ updateParams.fAgeSource = SkParticleValue::kEffectAge_Source;
// Advance age for existing particles, and remove any that have reached their end of life
for (int i = 0; i < fCount; ++i) {
@@ -85,18 +92,12 @@
fSpawnRemainder = desired - numToSpawn;
numToSpawn = SkTPin(numToSpawn, 0, fParams->fMaxCount - fCount);
if (numToSpawn) {
- // This isn't "particle" t, it's effect t.
- float t = static_cast<float>((now - fSpawnTime) / fParams->fEffectDuration);
- t = fLooping ? fmodf(t, 1.0f) : SkTPin(t, 0.0f, 1.0f);
+ const int spawnBase = fCount;
for (int i = 0; i < numToSpawn; ++i) {
// Mutate our SkRandom so each particle definitely gets a different stable generator
fRandom.nextU();
-
- // Temporarily set our age to the *effect* age, so spawn affectors are driven by that
- fParticles[fCount].fAge = t;
- fParticles[fCount].fInvLifetime =
- sk_ieee_float_divide(1.0f, fParams->fLifetime.eval(t, fRandom));
+ fParticles[fCount].fAge = 0.0f;
fParticles[fCount].fPose.fPosition = { 0.0f, 0.0f };
fParticles[fCount].fPose.fHeading = { 0.0f, -1.0f };
fParticles[fCount].fPose.fScale = 1.0f;
@@ -104,19 +105,21 @@
fParticles[fCount].fVelocity.fAngular = 0.0f;
fParticles[fCount].fColor = { 1.0f, 1.0f, 1.0f, 1.0f };
fParticles[fCount].fFrame = 0.0f;
-
fParticles[fCount].fRandom = fStableRandoms[fCount] = fRandom;
fCount++;
}
- // Apply spawn affectors, then reset our age to 0 (the *particle* age)
+ // Apply spawn affectors
for (auto affector : fParams->fSpawnAffectors) {
if (affector) {
- affector->apply(updateParams, fParticles + (fCount - numToSpawn), numToSpawn);
+ affector->apply(updateParams, fParticles + spawnBase, numToSpawn);
}
}
- for (int i = fCount - numToSpawn; i < fCount; ++i) {
- fParticles[i].fAge = 0.0f;
+
+ // Now compute particle lifetimes (so the curve can refer to spawn-computed source values)
+ for (int i = spawnBase; i < fCount; ++i) {
+ fParticles[i].fInvLifetime =
+ sk_ieee_float_divide(1.0f, fParams->fLifetime.eval(updateParams, fParticles[i]));
}
}
@@ -125,6 +128,9 @@
fParticles[i].fRandom = fStableRandoms[i];
}
+ // During update, values that refer to kAge_Source get the *particle* age
+ updateParams.fAgeSource = SkParticleValue::kParticleAge_Source;
+
// Apply update rules
for (auto affector : fParams->fUpdateAffectors) {
if (affector) {
@@ -163,3 +169,116 @@
fCapacity = capacity;
fCount = SkTMin(fCount, fCapacity);
}
+
+constexpr SkFieldVisitor::EnumStringMapping gValueSourceMapping[] = {
+ { SkParticleValue::kAge_Source, "Age" },
+ { SkParticleValue::kRandom_Source, "Random" },
+ { SkParticleValue::kParticleAge_Source, "ParticleAge" },
+ { SkParticleValue::kEffectAge_Source, "EffectAge" },
+ { SkParticleValue::kPositionX_Source, "PositionX" },
+ { SkParticleValue::kPositionY_Source, "PositionY" },
+ { SkParticleValue::kHeadingX_Source, "HeadingX" },
+ { SkParticleValue::kHeadingY_Source, "HeadingY" },
+ { SkParticleValue::kScale_Source, "Scale" },
+ { SkParticleValue::kVelocityX_Source, "VelocityX" },
+ { SkParticleValue::kVelocityY_Source, "VelocityY" },
+ { SkParticleValue::kRotation_Source, "Rotation" },
+ { SkParticleValue::kColorR_Source, "ColorR" },
+ { SkParticleValue::kColorG_Source, "ColorG" },
+ { SkParticleValue::kColorB_Source, "ColorB" },
+ { SkParticleValue::kColorA_Source, "ColorA" },
+ { SkParticleValue::kSpriteFrame_Source, "SpriteFrame" },
+};
+
+constexpr SkFieldVisitor::EnumStringMapping gValueTileModeMapping[] = {
+ { SkParticleValue::kClamp_TileMode, "Clamp" },
+ { SkParticleValue::kRepeat_TileMode, "Repeat" },
+ { SkParticleValue::kMirror_TileMode, "Mirror" },
+};
+
+static bool source_needs_frame(int source) {
+ switch (source) {
+ case SkParticleValue::kHeadingX_Source:
+ case SkParticleValue::kHeadingY_Source:
+ case SkParticleValue::kVelocityX_Source:
+ case SkParticleValue::kVelocityY_Source:
+ return true;
+ default:
+ return false;
+ }
+}
+
+void SkParticleValue::visitFields(SkFieldVisitor* v) {
+ v->visit("Source", fSource, gValueSourceMapping, SK_ARRAY_COUNT(gValueSourceMapping));
+ if (source_needs_frame(fSource)) {
+ v->visit("Frame", fFrame, gParticleFrameMapping, SK_ARRAY_COUNT(gParticleFrameMapping));
+ }
+ v->visit("TileMode", fTileMode, gValueTileModeMapping, SK_ARRAY_COUNT(gValueTileModeMapping));
+ v->visit("Left", fLeft);
+ v->visit("Right", fRight);
+
+ // Re-compute cached evaluation parameters
+ fScale = sk_float_isfinite(1.0f / (fRight - fLeft)) ? 1.0f / (fRight - fLeft) : 0;
+ fBias = -fLeft * fScale;
+}
+
+float SkParticleValue::getSourceValue(const SkParticleUpdateParams& params,
+ SkParticleState& ps) const {
+ switch ((kAge_Source == fSource) ? params.fAgeSource : fSource) {
+ // Do all the simple (non-frame-dependent) sources first:
+ case kRandom_Source: return ps.fRandom.nextF();
+ case kParticleAge_Source: return ps.fAge;
+ case kEffectAge_Source: return params.fEffectAge;
+
+ case kPositionX_Source: return ps.fPose.fPosition.fX;
+ case kPositionY_Source: return ps.fPose.fPosition.fY;
+ case kScale_Source: return ps.fPose.fScale;
+ case kRotation_Source: return ps.fVelocity.fAngular;
+
+ case kColorR_Source: return ps.fColor.fR;
+ case kColorG_Source: return ps.fColor.fG;
+ case kColorB_Source: return ps.fColor.fB;
+ case kColorA_Source: return ps.fColor.fA;
+ case kSpriteFrame_Source: return ps.fFrame;
+ }
+
+ SkASSERT(source_needs_frame(fSource));
+ SkVector frameUp = ps.getFrameHeading(static_cast<SkParticleFrame>(fFrame));
+ SkVector frameRight = { -frameUp.fY, frameUp.fX };
+
+ switch (fSource) {
+ case kHeadingX_Source: return ps.fPose.fHeading.dot(frameRight);
+ case kHeadingY_Source: return ps.fPose.fHeading.dot(frameUp);
+ case kVelocityX_Source: return ps.fVelocity.fLinear.dot(frameRight);
+ case kVelocityY_Source: return ps.fVelocity.fLinear.dot(frameUp);
+ }
+
+ SkDEBUGFAIL("Unreachable");
+ return 0.0f;
+}
+
+float SkParticleValue::eval(const SkParticleUpdateParams& params, SkParticleState& ps) const {
+ float v = this->getSourceValue(params, ps);
+ v = (v * fScale) + fBias;
+
+ switch (fTileMode) {
+ case kClamp_TileMode:
+ v = SkTPin(v, 0.0f, 1.0f);
+ break;
+ case kRepeat_TileMode:
+ v = sk_float_mod(v, 1.0f);
+ if (v < 0) {
+ v += 1.0f;
+ }
+ break;
+ case kMirror_TileMode:
+ v = sk_float_mod(v, 2.0f);
+ if (v < 0) {
+ v += 2.0f;
+ }
+ v = 1.0f - sk_float_abs(v - 1.0f);
+ break;
+ }
+
+ return v;
+}