Skip to content

Commit

Permalink
feat!: Update flame_noise to use latest version of fast_noise (#3015)
Browse files Browse the repository at this point in the history
Update flame_noise to use the latest version of fast_noise, basically
replacing the Perlin-specific effect controller with a generic
`NoiseEffectController` that can take in any noise class (leveraging the
new Noise2 interface).

Just update from `PerlinNoiseEffectController` to
`NoiseEffectController` and provide the noise/parameters you want
directly into the `noise` field.

---------

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
  • Loading branch information
luanpotter and spydon committed Feb 4, 2024
1 parent 343bdca commit 2fd84c8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:flutter/services.dart';

class CameraFollowAndWorldBoundsExample extends FlameGame
with HasKeyboardHandlerComponents {
static const description = '''
static const description = '''
This example demonstrates camera following the player, but also obeying the
world bounds (which are set up to leave a small margin around the visible
part of the ground).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ This examples showcases how raycast APIs can be used to detect hits within certa
camera.viewfinder.add(
MoveEffect.by(
Vector2(5, 5),
PerlinNoiseEffectController(duration: 0.2, frequency: 400),
NoiseEffectController(
duration: 0.2,
noise: PerlinNoise(frequency: 400),
),
),
);
}
Expand Down
10 changes: 8 additions & 2 deletions examples/lib/stories/effects/move_effect_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,18 @@ class _MoveEffectWorld extends World {
[
MoveEffect.by(
Vector2(5, 0),
PerlinNoiseEffectController(duration: 1, frequency: 20),
NoiseEffectController(
duration: 1,
noise: PerlinNoise(frequency: 20),
),
),
MoveEffect.by(Vector2.zero(), LinearEffectController(2)),
MoveEffect.by(
Vector2(0, 10),
PerlinNoiseEffectController(duration: 1, frequency: 10),
NoiseEffectController(
duration: 1,
noise: PerlinNoise(frequency: 10),
),
),
],
infinite: true,
Expand Down
4 changes: 3 additions & 1 deletion packages/flame_noise/lib/src/effects.dart
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export 'effects/perlin_noise_effect_controller.dart';
export 'package:fast_noise/fast_noise.dart';

export 'effects/noise_effect_controller.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:fast_noise/fast_noise.dart';
import 'package:flame/effects.dart';
import 'package:flutter/animation.dart' show Curve, Curves;

/// Effect controller that oscillates around 0 following a noise curve.
/// Effect controller that oscillates around following a noise curve.
///
/// The [taperingCurve] describes how the effect fades out over time. The
/// curve that you supply will be flipped along the X axis, so that the effect
Expand All @@ -12,25 +12,21 @@ import 'package:flutter/animation.dart' show Curve, Curves;
/// example, putting into a `MoveEffect.by` will create a shake motion, where
/// the magnitude and the direction of shaking is controlled by the effect's
/// `offset`.
class PerlinNoiseEffectController extends DurationEffectController {
PerlinNoiseEffectController({
class NoiseEffectController extends DurationEffectController {
final Curve taperingCurve;
final Noise2 noise;

NoiseEffectController({
required double duration,
int octaves = 3,
double frequency = 0.05,
this.taperingCurve = Curves.easeInOutCubic,
int seed = 1337,
}) : assert(duration > 0, 'duration must be positive'),
assert(frequency > 0, 'frequency parameter must be positive'),
noise = PerlinNoise(seed: seed, octaves: octaves, frequency: frequency),
Noise2? noise,
}) : noise = noise ?? PerlinNoise(),
super(duration);

final Curve taperingCurve;
final PerlinNoise noise;

@override
double get progress {
final x = timer / duration;
final amplitude = taperingCurve.transform(1 - x);
return noise.getPerlin2(x, 1) * amplitude;
return noise.getNoise2(x, 1) * amplitude;
}
}
2 changes: 1 addition & 1 deletion packages/flame_noise/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ environment:
flutter: ">=3.13.0"

dependencies:
fast_noise: ^1.0.1
fast_noise: ^2.0.0
flame: ^1.14.0
flutter:
sdk: flutter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import 'package:test/test.dart';
void main() {
group('PerlinNoiseEffectController', () {
test('general properties', () {
final ec = PerlinNoiseEffectController(duration: 1, frequency: 12);
final ec = NoiseEffectController(
duration: 1,
noise: PerlinNoise(frequency: 12),
);
expect(ec.duration, 1.0);
expect(ec.taperingCurve, Curves.easeInOutCubic);
expect(ec.started, true);
Expand All @@ -16,7 +19,10 @@ void main() {
});

test('progression', () {
final ec = PerlinNoiseEffectController(duration: 1);
final ec = NoiseEffectController(
duration: 1,
noise: PerlinNoise(frequency: 0.05),
);
final observed = <double>[];
for (var t = 0.0; t < 1.0; t += 0.1) {
observed.add(ec.progress);
Expand All @@ -39,12 +45,8 @@ void main() {

test('errors', () {
expect(
() => PerlinNoiseEffectController(duration: 0, frequency: 1),
failsAssert('duration must be positive'),
);
expect(
() => PerlinNoiseEffectController(duration: 1, frequency: 0),
failsAssert('frequency parameter must be positive'),
() => NoiseEffectController(duration: -1),
failsAssert('Duration cannot be negative: -1.0'),
);
});
});
Expand Down

0 comments on commit 2fd84c8

Please sign in to comment.