Skip to content

Commit

Permalink
add more module
Browse files Browse the repository at this point in the history
  • Loading branch information
SantyWang committed Sep 11, 2023
1 parent 8fda154 commit 4c02b9e
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 47 deletions.
6 changes: 3 additions & 3 deletions native/cocos/particle/CurveRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ struct OptimizedCurve {
float evaluate(float time) const;

private:
std::vector<float> lookUpTable{1.F, 1.F};
float minTime{0.F};
float length{1.F};
std::vector<float> lookUpTable{1.F, 1.F};
};

struct CurveRange {
Expand Down Expand Up @@ -80,10 +80,10 @@ struct CurveRange {

float evaluateSlow(float time, float alpha) const ;

float minScalar{1.F};
float scalar{1.F};
OptimizedCurve minCurve{};
OptimizedCurve curve{};
float minScalar{1.F};
float scalar{1.F};
CurveRangeMode mode{CurveRangeMode::CONSTANT};
};
}; // namespace cc
Expand Down
13 changes: 13 additions & 0 deletions native/cocos/particle/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ enum class RenderMode : uint8_t {
RIBBON
};

enum class NoiseQuality : uint8_t {
HIGH,
MEDIUM,
LOW
};

enum class NoiseAlgorithm : uint8_t {
VALUE,
PERLIN,
SIMPLEX_VALUE,
SIMPLEX,
};

enum class ModuleStage : uint8_t {
EMITTER,
SPAWN,
Expand Down
4 changes: 0 additions & 4 deletions native/cocos/particle/ParticleSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ void ParticleSystem::updateEmitterState(float deltaTime) {
currentTime -= (int32_t)(currentTime * invDuration) * currentDuration;
_emitter.loopCount = (uint32_t)(currentTime * invDuration);
} else {
if (prevTime > currentDuration) {
prevTime = currentDuration;
}
currentTime = currentDuration;
_emitter.loopCount = 1;
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/particle/ParticleSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "particle/modules/Shape.h"
#include "particle/modules/CustomData.h"
#include "particle/modules/DragOverLifetime.h"
#include "particle/modules/InheritVelocity.h"
#include "particle/modules/InheritVelocityOverLifetime.h"
#include "particle/modules/LinearVelocityOverLifetime.h"
#include "particle/modules/OrbitalVelocityOverLifetime.h"
#include "particle/modules/LimitVelocityOverLifetime.h"
Expand Down
Empty file.
31 changes: 31 additions & 0 deletions native/cocos/particle/modules/CurlNoiseForceOverLifetime.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
#pragma once

#include "particle/ParticleSystemModule.h"
#include "particle/CurveRange.h"
#include "math/Vec3.h"

namespace cc {
class CurlNoiseForceOverLifetime : public ParticleSystemModule {
public:
enum {
MAX_NUM_OCTAVE = 4,
};

CurlNoiseForceOverLifetime() = default;
virtual ~CurlNoiseForceOverLifetime() override = default;

virtual void update(ModuleUpdateContext& context) const override;

private:
template <bool separateAxes, bool remap>
void updateForce(ModuleUpdateContext& context) const;

template<bool multiOctaves, NoiseQuality quality, NoiseAlgorithm algorithm>
Vec2 accumulateNoiseSum(Vec3 sample, float frequency);

CurveRange _strength[3]{1, 1, 1};
CurveRange _scrollSpeed{0};
CurveRange _remapCurve[3]{{1.F, OptimizedCurve::ZERO_TO_ONE}, {1.F, OptimizedCurve::ZERO_TO_ONE}, {1.F, OptimizedCurve::ZERO_TO_ONE}};
float _octaveMultiplier{0.5};
float _octaveScale{2.F};
float _frequency{0.5};
NoiseQuality _quality{NoiseQuality::HIGH};
NoiseAlgorithm _algorithm{NoiseAlgorithm::PERLIN};
bool _damping{true};
bool _separateAxes{false};
bool _remap{false};
uint8_t _octaves{1};

};
}
32 changes: 32 additions & 0 deletions native/cocos/particle/modules/EmissionByBurst.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#include "particle/modules/EmissionByBurst.h"
#include "math/MathUtil.h"

namespace cc {
void EmissionByBurst::update(ModuleUpdateContext& context) const {
if (context.emitter.loopedAge < 0) {
return;
}

}

void EmissionByBurst::updateEmissionInfo(ModuleUpdateContext& context, float prevLoopedAge, float loopedAge, float normalizedLoopAge, float prevNormalizedLoopAge, float subFrame) const {
float dt = loopedAge - prevLoopedAge;
for (size_t i = 0; i < _burstCount; i++) {
const Burst& burst = _bursts[i];
if (burst.time > loopedAge) continue;

float interval = std::max(burst.interval, math::EPSILON);
float count = dt / interval;
float emittedCycle = (prevLoopedAge - burst.time) / interval;
float toEmitCycle = (loopedAge - burst.time) / interval;
uint32_t newBurst = (int32_t)toEmitCycle - (int32_t)emittedCycle;

if (newBurst > 0) {
float burstInterval = dt / count;
float normalizedInterval = (normalizedLoopAge - prevNormalizedLoopAge) / count;
float remainder = toEmitCycle - (int32_t)toEmitCycle;
for (size_t cycle = 0; cycle < newBurst; cycle++) {
float interpStartNormalizedT = normalizedLoopAge - normalizedInterval * remainder;
float alpha = Rand::rand1(context.emitter.tickCount, context.emitter.randomSeed, 0x7277c);
float count = burst.count.evaluateSlow(interpStartNormalizedT, alpha);

float interpStartDt = remainder * burstInterval + (context.deltaTime - dt - subFrame);
context.emissionInfos.emplace_back(count, 0.F, 0.F, interpStartDt, interpStartNormalizedT);
}
}
}
}
}
2 changes: 2 additions & 0 deletions native/cocos/particle/modules/EmissionByBurst.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class EmissionByBurst : public ParticleSystemModule {
virtual void update(ModuleUpdateContext& context) const override;

private:
void updateEmissionInfo(ModuleUpdateContext& context, float prevLoopedAge, float loopedAge, float normalizedLoopAge, float prevNormalizedLoopAge, float subFrame) const;

Burst _bursts[MAX_BURST_COUNT]{};
uint32_t _burstCount{0};
};
Expand Down
3 changes: 2 additions & 1 deletion native/cocos/particle/modules/EmissionOverDistance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace cc {
void EmissionOverDistance::update(ModuleUpdateContext& context) const {
if (context.emitter.loopedAge < 0) {
if (context.emitter.normalizedLoopAge < 0.F || context.emitter.prevNormalizedLoopAge > 1.F) {
return;
}

if (context.emitter.loopedAge < context.emitter.prevLoopedAge) {
updateEmissionInfo(context, context.emitter.duration - context.emitter.prevLoopedAge, 1.F, context.emitter.prevNormalizedLoopAge);
updateEmissionInfo(context, context.emitter.loopedAge, context.emitter.normalizedLoopAge, 0, context.emitter.duration - context.emitter.prevLoopedAge);
Expand Down
4 changes: 3 additions & 1 deletion native/cocos/particle/modules/EmissionOverTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace cc {
void EmissionOverTime::update(ModuleUpdateContext& context) const {
if (context.emitter.loopedAge < 0) {
if (context.emitter.normalizedLoopAge < 0.F || context.emitter.prevNormalizedLoopAge > 1.F) {
return;
}

Expand All @@ -12,6 +12,8 @@ void EmissionOverTime::update(ModuleUpdateContext& context) const {
updateEmissionInfo(context, context.emitter.loopedAge, context.emitter.normalizedLoopAge, 0, context.emitter.duration - context.emitter.prevLoopedAge);
} else if (context.emitter.prevLoopedAge < 0) {
updateEmissionInfo(context, context.emitter.loopedAge, context.emitter.normalizedLoopAge, 0, -context.emitter.prevLoopedAge);
} else if (context.emitter.loopedAge > context.emitter.duration) {
updateEmissionInfo(context, context.emitter.loopedAge, context.emitter.normalizedLoopAge, 0, -context.emitter.prevLoopedAge);
} else {
updateEmissionInfo(context, context.emitter.loopedAge - context.emitter.prevLoopedAge, context.emitter.normalizedLoopAge, context.emitter.prevNormalizedLoopAge);
}
Expand Down
9 changes: 0 additions & 9 deletions native/cocos/particle/modules/InheritVelocity.h

This file was deleted.

36 changes: 36 additions & 0 deletions native/cocos/particle/modules/InheritVelocityOverLifetime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "particle/modules/InheritVelocityOverLifetime.h"

namespace cc {
void InheritVelocityOverLifetime::update(ModuleUpdateContext& context) const {
if (_multiplier.mode == CurveRangeMode::CONSTANT) {
updateVelocity<CurveRangeMode::CONSTANT>(context);
} else if (_multiplier.mode == CurveRangeMode::CURVE) {
updateVelocity<CurveRangeMode::CURVE>(context);
} else if (_multiplier.mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS>(context);
} else {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES>(context);
}
}

template <CurveRangeMode mode>
void InheritVelocityOverLifetime::updateVelocity(ModuleUpdateContext& context) const {
if (context.emitter.simulationSpace == CoordinateSpace::LOCAL) return;

ParticleVec3Array& dest = context.particles.velocity[context.particles.currentVelocityIndex];
Vec3 emitterVelocity = context.emitter.velocity;
for (uint32_t i = context.fromIndex; i < context.toIndex; i++) {
float alpha{1.F};
float time{1.F};
if constexpr (CurveRange::needsRandomAlpha<mode>()) {
alpha = Rand::rand1(context.particles.id.load(i), context.emitter.randomSeed, 0x6517bc);
}

if constexpr (CurveRange::needsTime<mode>()) {
time = context.particles.normalizedAge.load(i);
};
float scale = _multiplier.evaluate<mode>(time, alpha);
dest.store(i, dest.load(i) + emitterVelocity * scale);
}
}
}
19 changes: 19 additions & 0 deletions native/cocos/particle/modules/InheritVelocityOverLifetime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "particle/ParticleSystemModule.h"
#include "particle/CurveRange.h"

namespace cc {
class InheritVelocityOverLifetime : public ParticleSystemModule {
public:
InheritVelocityOverLifetime() = default;
virtual ~InheritVelocityOverLifetime() override = default;
virtual void update(ModuleUpdateContext& context) const override;

private:
template<CurveRangeMode mode>
void updateVelocity(ModuleUpdateContext& context) const;

CurveRange _multiplier{0};
};
}
35 changes: 16 additions & 19 deletions native/cocos/particle/modules/LinearVelocityOverLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@ namespace cc {
void LinearVelocityOverLifetime::update(ModuleUpdateContext& context) const {
bool needTransform = _space != context.emitter.simulationSpace;
if (needTransform) {
if (_velocity[0].mode == CurveRangeMode::CONSTANT) {
updateVelocity<CurveRangeMode::CONSTANT, true>(context);
} else if (_velocity[0].mode == CurveRangeMode::CURVE) {
updateVelocity<CurveRangeMode::CURVE, true>(context);
} else if (_velocity[0].mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS, true>(context);
} else {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES, true>(context);
}
updateTransform<true>(context);
} else {
if (_velocity[0].mode == CurveRangeMode::CONSTANT) {
updateVelocity<CurveRangeMode::CONSTANT, false>(context);
} else if (_velocity[0].mode == CurveRangeMode::CURVE) {
updateVelocity<CurveRangeMode::CURVE, false>(context);
} else if (_velocity[0].mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS, false>(context);
} else {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES, false>(context);
}
updateTransform<false>(context);
}
}

template <bool transform>
void LinearVelocityOverLifetime::updateTransform(ModuleUpdateContext& context) const {
if (_velocity[0].mode == CurveRangeMode::CONSTANT) {
updateVelocity<CurveRangeMode::CONSTANT, transform>(context);
} else if (_velocity[0].mode == CurveRangeMode::CURVE) {
updateVelocity<CurveRangeMode::CURVE, transform>(context);
} else if (_velocity[0].mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS, transform>(context);
} else {
updateVelocity<CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES, transform>(context);
}
}

Expand All @@ -36,7 +33,7 @@ void LinearVelocityOverLifetime::updateVelocity(ModuleUpdateContext& context) co
ParticleVec3Array& dest = context.particles.velocity[context.particles.currentVelocityIndex];
for (uint32_t i = context.fromIndex; i < context.toIndex; i++) {
Vec3 alpha{};
float time{};
float time{1.F};
if constexpr (CurveRange::needsRandomAlpha<mode>()) {
alpha = Rand::rand3(context.particles.id.load(i), context.emitter.randomSeed, 0x07287ab1);
}
Expand Down
3 changes: 3 additions & 0 deletions native/cocos/particle/modules/LinearVelocityOverLifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class LinearVelocityOverLifetime : public ParticleSystemModule {
virtual void update(ModuleUpdateContext& context) const override;

private:
template<bool transform>
void updateTransform(ModuleUpdateContext& context) const;

template<CurveRangeMode mode, bool transform>
void updateVelocity(ModuleUpdateContext& context) const;

Expand Down
79 changes: 79 additions & 0 deletions native/cocos/particle/modules/OrbitalVelocityOverLifetime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "particle/modules/OrbitalVelocityOverLifetime.h"

namespace cc {
void OrbitalVelocityOverLifetime::update(ModuleUpdateContext& context) const {
if (_orbital[0].mode == CurveRangeMode::CONSTANT) {
updateOrbital<CurveRangeMode::CONSTANT>(context);
} else if (_orbital[0].mode == CurveRangeMode::CURVE) {
updateOrbital<CurveRangeMode::CURVE>(context);
} else if (_orbital[0].mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateOrbital<CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS>(context);
} else {
updateOrbital<CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES>(context);
}
}

template <CurveRangeMode orbitalMode>
void OrbitalVelocityOverLifetime::updateOrbital(ModuleUpdateContext& context) const {
if (_offset[0].mode == CurveRangeMode::CONSTANT) {
updateOffset<orbitalMode, CurveRangeMode::CONSTANT>(context);
} else if (_offset[0].mode == CurveRangeMode::CURVE) {
updateOffset<orbitalMode, CurveRangeMode::CURVE>(context);
} else if (_offset[0].mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateOffset<orbitalMode, CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS>(context);
} else {
updateOffset<orbitalMode, CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES>(context);
}
}

template <CurveRangeMode orbitalMode, CurveRangeMode offsetMode>
void OrbitalVelocityOverLifetime::updateOffset(ModuleUpdateContext& context) const {
if (_radial.mode == CurveRangeMode::CONSTANT) {
updateVelocity<orbitalMode, offsetMode, CurveRangeMode::CONSTANT>(context);
} else if (_radial.mode == CurveRangeMode::CURVE) {
updateVelocity<orbitalMode, offsetMode, CurveRangeMode::CURVE>(context);
} else if (_radial.mode == CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS) {
updateVelocity<orbitalMode, offsetMode, CurveRangeMode::RANDOM_BETWEEN_TWO_CONSTANTS>(context);
} else {
updateVelocity<orbitalMode, offsetMode, CurveRangeMode::RANDOM_BETWEEN_TWO_CURVES>(context);
}
}

template <CurveRangeMode orbitalMode, CurveRangeMode offsetMode, CurveRangeMode radialMode>
void OrbitalVelocityOverLifetime::updateVelocity(ModuleUpdateContext& context) const {
for (size_t i = context.fromIndex; i < context.toIndex; i++) {
Vec3 alpha{};
float time{1.F};

if constexpr (CurveRange::needsTime<orbitalMode>() || CurveRange::needsTime<offsetMode>() || CurveRange::needsTime<radialMode>()) {
time = context.particles.normalizedAge.load(i);
}

if constexpr (CurveRange::needsRandomAlpha<orbitalMode>()) {
alpha = Rand::rand3(context.particles.id.load(i), context.emitter.randomSeed, 0xef728c);
}

Vec3 oritalVelocity {
_orbital[0].evaluate<orbitalMode>(time, alpha.x);
_orbital[1].evaluate<orbitalMode>(time, alpha.y);
_orbital[2].evaluate<orbitalMode>(time, alpha.z);
};

if constexpr (CurveRange::needsRandomAlpha<offsetMode>()) {
alpha = Rand::rand3(context.particles.id.load(i), context.emitter.randomSeed, 0x117ca);
}

Vec3 offset {
_offset[0].evaluate<offsetMode>(time, alpha.x);
_offset[1].evaluate<offsetMode>(time, alpha.y);
_offset[2].evaluate<offsetMode>(time, alpha.z);
};

if constexpr (CurveRange::needsRandomAlpha<radialMode>()) {
alpha.x = Rand::rand1(context.particles.id.load(i), context.emitter.randomSeed, 0x67182bc);
}

float radialVel = _radial.evaluate<radialMode>(time, alpha.x);
}
}
}
Loading

0 comments on commit 4c02b9e

Please sign in to comment.