custom-pixi-particles 4.10.0 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import { Behaviour } from './index';
|
|
2
2
|
import Particle from '../Particle';
|
|
3
|
+
import { Color } from '../util';
|
|
3
4
|
export default class SoundReactiveBehaviour extends Behaviour {
|
|
4
5
|
enabled: boolean;
|
|
5
6
|
priority: number;
|
|
6
7
|
isPlaying: boolean;
|
|
8
|
+
useColor: boolean;
|
|
9
|
+
useSize: boolean;
|
|
10
|
+
useVelocity: boolean;
|
|
11
|
+
useRotation: boolean;
|
|
12
|
+
useRandomColor: boolean;
|
|
13
|
+
beatColor: Color;
|
|
7
14
|
audioContext: AudioContext | null;
|
|
8
15
|
analyser: AnalyserNode | null;
|
|
9
16
|
frequencyData: Uint8Array | null;
|
|
10
17
|
amplitudeFactor: number;
|
|
11
18
|
frequencyFactor: number;
|
|
19
|
+
rotationFactor: number;
|
|
12
20
|
beatSensitivity: number;
|
|
13
21
|
init(): void;
|
|
14
22
|
apply(particle: Particle, deltaTime: number): void;
|
|
@@ -24,13 +32,24 @@ export default class SoundReactiveBehaviour extends Behaviour {
|
|
|
24
32
|
* Detects a beat based on amplitude and sensitivity.
|
|
25
33
|
*/
|
|
26
34
|
isBeatDetected(amplitude: number): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Generates a random RGBA color.
|
|
37
|
+
*/
|
|
38
|
+
getRandomColor(): Color;
|
|
27
39
|
getName(): string;
|
|
28
40
|
getProps(): {
|
|
29
41
|
enabled: boolean;
|
|
30
42
|
priority: number;
|
|
31
43
|
isPlaying: boolean;
|
|
44
|
+
useColor: boolean;
|
|
45
|
+
useVelocity: boolean;
|
|
46
|
+
useSize: boolean;
|
|
47
|
+
useRotation: boolean;
|
|
48
|
+
useRandomColor: boolean;
|
|
49
|
+
beatColor: Color;
|
|
32
50
|
amplitudeFactor: number;
|
|
33
51
|
frequencyFactor: number;
|
|
52
|
+
rotationFactor: number;
|
|
34
53
|
beatSensitivity: number;
|
|
35
54
|
name: string;
|
|
36
55
|
};
|
|
@@ -1,15 +1,23 @@
|
|
|
1
1
|
import { Behaviour, BehaviourNames } from './index';
|
|
2
|
+
import { Color } from '../util';
|
|
2
3
|
export default class SoundReactiveBehaviour extends Behaviour {
|
|
3
4
|
constructor() {
|
|
4
5
|
super(...arguments);
|
|
5
6
|
this.enabled = true;
|
|
6
7
|
this.priority = 0;
|
|
7
8
|
this.isPlaying = false;
|
|
9
|
+
this.useColor = true;
|
|
10
|
+
this.useSize = true;
|
|
11
|
+
this.useVelocity = true;
|
|
12
|
+
this.useRotation = true; // New property for rotation
|
|
13
|
+
this.useRandomColor = true; // New property for random colors
|
|
14
|
+
this.beatColor = new Color(255, 0, 0, 1); // Default beat color (red with full alpha)
|
|
8
15
|
this.audioContext = null; // Audio context for analysis
|
|
9
16
|
this.analyser = null; // Audio analyser node
|
|
10
17
|
this.frequencyData = null; // Frequency data array
|
|
11
18
|
this.amplitudeFactor = 0.1; // Scale factor for amplitude effects
|
|
12
19
|
this.frequencyFactor = 1; // Scale factor for frequency effects
|
|
20
|
+
this.rotationFactor = 0.05; // Scale factor for rotation effects
|
|
13
21
|
this.beatSensitivity = 1; // Sensitivity to detect beats
|
|
14
22
|
}
|
|
15
23
|
init() {
|
|
@@ -23,22 +31,35 @@ export default class SoundReactiveBehaviour extends Behaviour {
|
|
|
23
31
|
// Compute amplitude and frequency effects
|
|
24
32
|
const amplitude = this.getAmplitude();
|
|
25
33
|
const dominantFrequency = this.getDominantFrequency();
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
particle.velocity.x += dominantFrequency * this.frequencyFactor * deltaTime;
|
|
31
|
-
particle.velocity.y += dominantFrequency * this.frequencyFactor * deltaTime;
|
|
32
|
-
// Add beat reaction if sensitivity is high enough
|
|
33
|
-
if (this.isBeatDetected(amplitude)) {
|
|
34
|
-
particle.color.r = 255; // Flash red on beat
|
|
35
|
-
particle.color.g = 0;
|
|
36
|
-
particle.color.b = 0;
|
|
34
|
+
if (this.useSize) {
|
|
35
|
+
// Apply amplitude effect to size
|
|
36
|
+
particle.size.x += amplitude * this.amplitudeFactor * deltaTime;
|
|
37
|
+
particle.size.y += amplitude * this.amplitudeFactor * deltaTime;
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
particle.
|
|
41
|
-
particle.
|
|
39
|
+
if (this.useVelocity) {
|
|
40
|
+
// Apply frequency effect to velocity or position
|
|
41
|
+
particle.velocity.x += dominantFrequency * this.frequencyFactor * deltaTime;
|
|
42
|
+
particle.velocity.y += dominantFrequency * this.frequencyFactor * deltaTime;
|
|
43
|
+
}
|
|
44
|
+
if (this.useRotation) {
|
|
45
|
+
// Apply rotation effect based on dominant frequency
|
|
46
|
+
particle.rotation += dominantFrequency * this.rotationFactor * deltaTime;
|
|
47
|
+
}
|
|
48
|
+
if (this.useColor) {
|
|
49
|
+
// Add beat reaction with color and alpha logic
|
|
50
|
+
if (this.isBeatDetected(amplitude)) {
|
|
51
|
+
const color = this.useRandomColor ? this.getRandomColor() : this.beatColor;
|
|
52
|
+
particle.color.r = color.r;
|
|
53
|
+
particle.color.g = color.g;
|
|
54
|
+
particle.color.b = color.b;
|
|
55
|
+
particle.color.alpha = color.alpha; // Ensure alpha is applied
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
particle.color.r = Math.max(0, particle.color.r - 5); // Fade back to normal
|
|
59
|
+
particle.color.g = Math.max(0, particle.color.g - 5);
|
|
60
|
+
particle.color.b = Math.max(0, particle.color.b - 5);
|
|
61
|
+
particle.color.alpha = Math.max(0, particle.color.alpha - 0.05); // Gradually decrease alpha
|
|
62
|
+
}
|
|
42
63
|
}
|
|
43
64
|
}
|
|
44
65
|
/**
|
|
@@ -73,6 +94,17 @@ export default class SoundReactiveBehaviour extends Behaviour {
|
|
|
73
94
|
isBeatDetected(amplitude) {
|
|
74
95
|
return amplitude > this.beatSensitivity * 128; // Threshold for beat detection
|
|
75
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Generates a random RGBA color.
|
|
99
|
+
*/
|
|
100
|
+
getRandomColor() {
|
|
101
|
+
// Generate a random color if allTheSameColor is false
|
|
102
|
+
const r = Math.floor(Math.random() * 256);
|
|
103
|
+
const g = Math.floor(Math.random() * 256);
|
|
104
|
+
const b = Math.floor(Math.random() * 256);
|
|
105
|
+
const a = 1; // Ensure alpha is explicitly set
|
|
106
|
+
return new Color(r, g, b, a);
|
|
107
|
+
}
|
|
76
108
|
getName() {
|
|
77
109
|
return BehaviourNames.SOUND_REACTIVE_BEHAVIOUR;
|
|
78
110
|
}
|
|
@@ -81,8 +113,15 @@ export default class SoundReactiveBehaviour extends Behaviour {
|
|
|
81
113
|
enabled: this.enabled,
|
|
82
114
|
priority: this.priority,
|
|
83
115
|
isPlaying: this.isPlaying,
|
|
116
|
+
useColor: this.useColor,
|
|
117
|
+
useVelocity: this.useVelocity,
|
|
118
|
+
useSize: this.useSize,
|
|
119
|
+
useRotation: this.useRotation,
|
|
120
|
+
useRandomColor: this.useRandomColor, // Include random color toggle in props
|
|
121
|
+
beatColor: this.beatColor, // Include beat color with alpha in props
|
|
84
122
|
amplitudeFactor: this.amplitudeFactor,
|
|
85
123
|
frequencyFactor: this.frequencyFactor,
|
|
124
|
+
rotationFactor: this.rotationFactor,
|
|
86
125
|
beatSensitivity: this.beatSensitivity,
|
|
87
126
|
name: this.getName(),
|
|
88
127
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SoundReactiveBehaviour.js","sourceRoot":"","sources":["../../../src/lib/behaviour/SoundReactiveBehaviour.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"SoundReactiveBehaviour.js","sourceRoot":"","sources":["../../../src/lib/behaviour/SoundReactive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