1
- import { Directive , effect , input , untracked } from '@angular/core' ;
1
+ import { computed , Directive , effect , input } from '@angular/core' ;
2
2
import { beforeRender , injectStore } from 'angular-three' ;
3
+ import { CameraShake } from 'angular-three-soba/vanilla-exports' ;
3
4
import { mergeInputs } from 'ngxtension/inject-inputs' ;
4
5
import * as THREE from 'three' ;
5
- import { SimplexNoise } from 'three-stdlib' ;
6
6
7
7
interface ControlsProto {
8
8
update ( ) : void ;
@@ -11,19 +11,7 @@ interface ControlsProto {
11
11
removeEventListener : ( event : string , callback : ( event : any ) => void ) => void ;
12
12
}
13
13
14
- export interface NgtsCameraShakeOptions {
15
- intensity : number ;
16
- decay ?: boolean ;
17
- decayRate : number ;
18
- maxYaw : number ;
19
- maxPitch : number ;
20
- maxRoll : number ;
21
- yawFrequency : number ;
22
- pitchFrequency : number ;
23
- rollFrequency : number ;
24
- }
25
-
26
- const defaultOptions : NgtsCameraShakeOptions = {
14
+ const defaultOptions : Partial < Omit < CameraShake , 'object' | 'initialRotation' | 'updateInitialRotation' | 'update' > > = {
27
15
intensity : 1 ,
28
16
decayRate : 0.65 ,
29
17
maxYaw : 0.1 ,
@@ -34,59 +22,38 @@ const defaultOptions: NgtsCameraShakeOptions = {
34
22
rollFrequency : 0.1 ,
35
23
} ;
36
24
37
- @Directive ( { selector : 'ngts-camera-shake' } )
25
+ @Directive ( {
26
+ selector : 'ngts-camera-shake' ,
27
+ exportAs : 'cameraShake' ,
28
+ } )
38
29
export class NgtsCameraShake {
39
30
options = input ( defaultOptions , { transform : mergeInputs ( defaultOptions ) } ) ;
40
31
41
32
private store = injectStore ( ) ;
42
33
43
- private initialRotation = this . store . snapshot . camera . rotation . clone ( ) ;
44
- private intensityOption = untracked ( this . options ) . intensity ;
45
-
46
- get intensity ( ) {
47
- return this . intensityOption ;
48
- }
49
-
50
- set intensity ( val : number ) {
51
- this . intensityOption = Math . min ( 1 , Math . max ( 0 , val ) ) ;
52
- }
53
-
54
- private yawNoise = new SimplexNoise ( ) ;
55
- private pitchNoise = new SimplexNoise ( ) ;
56
- private rollNoise = new SimplexNoise ( ) ;
34
+ cameraShaker = computed ( ( ) => new CameraShake ( this . store . camera ( ) ) ) ;
57
35
58
36
constructor ( ) {
59
37
effect ( ( onCleanup ) => {
60
38
const defaultControls = this . store . controls ( ) as unknown as ControlsProto ;
61
39
if ( ! defaultControls ) return ;
62
- const camera = this . store . camera ( ) ;
63
40
64
- const callback = ( ) => void ( this . initialRotation = camera . rotation . clone ( ) ) ;
41
+ const cameraShaker = this . cameraShaker ( ) ;
42
+ const callback = ( ) => void cameraShaker . updateInitialRotation ( ) ;
43
+
65
44
defaultControls . addEventListener ( 'change' , callback ) ;
66
45
callback ( ) ;
67
46
68
47
onCleanup ( ( ) => void defaultControls . removeEventListener ( 'change' , callback ) ) ;
69
48
} ) ;
70
49
71
- beforeRender ( ( { delta, clock } ) => {
72
- const [
73
- { maxYaw, yawFrequency, maxPitch, pitchFrequency, maxRoll, rollFrequency, decay, decayRate } ,
74
- camera ,
75
- ] = [ this . options ( ) , this . store . snapshot . camera ] ;
76
- const shake = Math . pow ( this . intensity , 2 ) ;
77
- const yaw = maxYaw * shake * this . yawNoise . noise ( clock . elapsedTime * yawFrequency , 1 ) ;
78
- const pitch = maxPitch * shake * this . pitchNoise . noise ( clock . elapsedTime * pitchFrequency , 1 ) ;
79
- const roll = maxRoll * shake * this . rollNoise . noise ( clock . elapsedTime * rollFrequency , 1 ) ;
80
-
81
- camera . rotation . set (
82
- this . initialRotation . x + pitch ,
83
- this . initialRotation . y + yaw ,
84
- this . initialRotation . z + roll ,
85
- ) ;
50
+ effect ( ( ) => {
51
+ Object . assign ( this . cameraShaker ( ) , this . options ( ) ) ;
52
+ } ) ;
86
53
87
- if ( decay && this . intensity > 0 ) {
88
- this . intensity -= decayRate * delta ;
89
- }
54
+ beforeRender ( ( { delta , clock } ) => {
55
+ const cameraShaker = this . cameraShaker ( ) ;
56
+ cameraShaker . update ( delta , clock . elapsedTime ) ;
90
57
} ) ;
91
58
}
92
59
}
0 commit comments