3
3
Injector ,
4
4
Signal ,
5
5
WritableSignal ,
6
- afterNextRender ,
7
6
computed ,
7
+ effect ,
8
8
inject ,
9
9
isSignal ,
10
10
signal ,
@@ -15,7 +15,6 @@ import { resolveRef } from 'angular-three';
15
15
import { NgtcPhysics } from 'angular-three-cannon' ;
16
16
import { NgtcDebug } from 'angular-three-cannon/debug' ;
17
17
import { assertInjector } from 'ngxtension/assert-injector' ;
18
- import { injectAutoEffect } from 'ngxtension/auto-effect' ;
19
18
import { DynamicDrawUsage , InstancedMesh , Object3D } from 'three' ;
20
19
import { NgtcArgFn , NgtcBodyPropsMap , NgtcBodyPublicApi , NgtcGetByIndex } from './types' ;
21
20
import { defaultTransformArgs , makeBodyApi , prepare , setupCollision } from './utils' ;
@@ -46,7 +45,6 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
46
45
throw new Error ( `[NGT Cannon] injectBody was called outside of <ngtc-physics>` ) ;
47
46
}
48
47
49
- const autoEffect = injectAutoEffect ( ) ;
50
48
const debug = inject ( NgtcDebug , { optional : true } ) ;
51
49
52
50
const transform = transformArgs ?? defaultTransformArgs [ type ] ;
@@ -61,73 +59,71 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
61
59
return makeBodyApi ( _body , worker ( ) , rest ) ;
62
60
} ) ;
63
61
64
- afterNextRender ( ( ) => {
65
- autoEffect ( ( ) => {
66
- const currentWorker = physics . api . worker ( ) ;
67
- if ( ! currentWorker ) return ;
62
+ effect ( ( onCleanup ) => {
63
+ const currentWorker = physics . api . worker ( ) ;
64
+ if ( ! currentWorker ) return ;
68
65
69
- const object = body ( ) ;
66
+ const object = body ( ) ;
70
67
71
- if ( ! isSignal ( ref ) && ! object ) {
72
- untracked ( ( ) => {
73
- ( bodyRef as WritableSignal < TObject | undefined > ) . set ( resolveRef ( ref ) ) ;
74
- } ) ;
75
- return ;
76
- }
68
+ if ( ! isSignal ( ref ) && ! object ) {
69
+ untracked ( ( ) => {
70
+ ( bodyRef as WritableSignal < TObject | undefined > ) . set ( resolveRef ( ref ) ) ;
71
+ } ) ;
72
+ return ;
73
+ }
77
74
78
- if ( ! object ) return ;
75
+ if ( ! object ) return ;
79
76
80
- const [ uuid , props ] = ( ( ) => {
81
- let uuids : string [ ] = [ ] ;
82
- let temp : Object3D ;
83
- if ( object instanceof InstancedMesh ) {
84
- object . instanceMatrix . setUsage ( DynamicDrawUsage ) ;
85
- uuids = new Array ( object . count ) . fill ( 0 ) . map ( ( _ , i ) => `${ object . uuid } /${ i } ` ) ;
86
- temp = new Object3D ( ) ;
87
- } else {
88
- uuids = [ object . uuid ] ;
89
- }
90
- return [
91
- uuids ,
92
- uuids . map ( ( id , index ) => {
93
- const props = getPropFn ( index ) ;
94
- if ( temp ) {
95
- prepare ( temp , props ) ;
96
- ( object as unknown as InstancedMesh ) . setMatrixAt ( index , temp . matrix ) ;
97
- ( object as unknown as InstancedMesh ) . instanceMatrix . needsUpdate = true ;
98
- } else {
99
- prepare ( object , props ) ;
100
- }
101
- physics . api . refs [ id ] = object ;
102
- debug ?. add ( id , props , type ) ;
103
- setupCollision ( physics . api . events , props , id ) ;
104
- // @ts -expect-error - if args is undefined, there's default
105
- return { ...props , args : transform ( props . args ) } ;
106
- } ) ,
107
- ] ;
108
- } ) ( ) ;
109
- // Register on mount, unregister on unmount
110
- currentWorker . addBodies ( {
111
- props : props . map ( ( { onCollide, onCollideBegin, onCollideEnd, ...serializableProps } ) => {
112
- return {
113
- onCollide : Boolean ( onCollide ) ,
114
- onCollideBegin : Boolean ( onCollideBegin ) ,
115
- onCollideEnd : Boolean ( onCollideEnd ) ,
116
- ...serializableProps ,
117
- } ;
77
+ const [ uuid , props ] = ( ( ) => {
78
+ let uuids : string [ ] = [ ] ;
79
+ let temp : Object3D ;
80
+ if ( object instanceof InstancedMesh ) {
81
+ object . instanceMatrix . setUsage ( DynamicDrawUsage ) ;
82
+ uuids = new Array ( object . count ) . fill ( 0 ) . map ( ( _ , i ) => `${ object . uuid } /${ i } ` ) ;
83
+ temp = new Object3D ( ) ;
84
+ } else {
85
+ uuids = [ object . uuid ] ;
86
+ }
87
+ return [
88
+ uuids ,
89
+ uuids . map ( ( id , index ) => {
90
+ const props = getPropFn ( index ) ;
91
+ if ( temp ) {
92
+ prepare ( temp , props ) ;
93
+ ( object as unknown as InstancedMesh ) . setMatrixAt ( index , temp . matrix ) ;
94
+ ( object as unknown as InstancedMesh ) . instanceMatrix . needsUpdate = true ;
95
+ } else {
96
+ prepare ( object , props ) ;
97
+ }
98
+ physics . api . refs [ id ] = object ;
99
+ debug ?. add ( id , props , type ) ;
100
+ setupCollision ( physics . api . events , props , id ) ;
101
+ // @ts -expect-error - if args is undefined, there's default
102
+ return { ...props , args : transform ( props . args ) } ;
118
103
} ) ,
119
- type,
120
- uuid,
121
- } ) ;
104
+ ] ;
105
+ } ) ( ) ;
106
+ // Register on mount, unregister on unmount
107
+ currentWorker . addBodies ( {
108
+ props : props . map ( ( { onCollide, onCollideBegin, onCollideEnd, ...serializableProps } ) => {
109
+ return {
110
+ onCollide : Boolean ( onCollide ) ,
111
+ onCollideBegin : Boolean ( onCollideBegin ) ,
112
+ onCollideEnd : Boolean ( onCollideEnd ) ,
113
+ ...serializableProps ,
114
+ } ;
115
+ } ) ,
116
+ type,
117
+ uuid,
118
+ } ) ;
122
119
123
- return ( ) => {
124
- uuid . forEach ( ( id ) => {
125
- delete physics . api . refs [ id ] ;
126
- debug ?. remove ( id ) ;
127
- delete physics . api . events [ id ] ;
128
- } ) ;
129
- currentWorker . removeBodies ( { uuid } ) ;
130
- } ;
120
+ onCleanup ( ( ) => {
121
+ uuid . forEach ( ( id ) => {
122
+ delete physics . api . refs [ id ] ;
123
+ debug ?. remove ( id ) ;
124
+ delete physics . api . events [ id ] ;
125
+ } ) ;
126
+ currentWorker . removeBodies ( { uuid } ) ;
131
127
} ) ;
132
128
} ) ;
133
129
0 commit comments